1

I have a requirement where I have to find the operating system details of my web app's user. If the user is running on windows it should give "Windows 7 ... Blah blah " if Linux "Ubuntu.. blah..blah OR Fedora .. .. " if Debian "whichever" version of Debian.

Can anyone suggest how we can do it? I had a look at sys-uname gem. But it is letting me know just the server side information.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
AnkitG
  • 6,438
  • 7
  • 44
  • 72
  • 1
    possible duplicate of [Ruby - How can I find out on which system my program is running?](http://stackoverflow.com/questions/170956/ruby-how-can-i-find-out-on-which-system-my-program-is-running) – Andrew Marshall Jun 12 '12 at 03:38
  • Wait, are you asking what OS the user accessing your application is using? Or the one your application is actually running on (the server)? – Andrew Marshall Jun 12 '12 at 03:41
  • use `request.env['HTTP_USER_AGENT']`, @AndrewMarshall I think hes asking the client OS. Not the possible duplicate you stated. – uday Jun 12 '12 at 03:48
  • 1
    The client can lie about its User-Agent, so you can't really *rely* on it for anything. See http://www.quirksmode.org/js/support.html. – Todd A. Jacobs Jun 12 '12 at 18:12

1 Answers1

3

You have to use request.env['HTTP_USER_AGENT']

I found a sample code.

  def getBrowser(bt)
    rs=false
    ua=request.env['HTTP_USER_AGENT'].downcase
    isOpera = ua.index('opera') ? true : false
    isSafari = (ua =~ /webkit|khtml/) ? true : false
    isSafari3 = (ua.index('webkit/5')) ? true : false
    isGecko = (!isSafari and ua.index('gecko')) ? true : false
    isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false
    isIE = (!isOpera and ua.index('msie')) ? true : false
    isIE7 = (!isOpera and ua.index('msie 7')) ? true : false
    case bt
      when 0  #isKonqueror
        if ua.index('konqueror') then rs=true end
      when 1  #isOpera
        rs=isOpera
      when 2  #isSafari
        rs=isSafari
      when 3  #isSafari2
        rs=isSafari && !isSafari3
      when 4  #isSafari3
        rs=isSafari3
      when 5  #isIE
        rs=isIE
      when 6  #isIE6
        rs=isIE && !isIE7
      when 7  #isIE7
        rs=isIE7
      when 8  #isGecko
        rs=isGecko
      when 9  #isGecko2
        rs=isGecko && !isGecko3
      when 10 #isGecko3
        rs=isGecko3
      when 11 #isWindows
        if ua.index('windows') or ua.index('win32') then rs=true end
      when 12 #isMac
        if ua.index('macintosh') or ua.index('mac os x') then rs=true 
end
      when 13 #isAir
        if ua.index('adobeair') then rs=true end
      when 14 #isLinux
        if ua.index('linux') then rs=true end
      when 15 #isSecure
        s = request.env['SERVER_PROTOCOL'].downcase
        if s.index('https') then rs=true end
    end
    rs
  end

EDIT: Creating another action to determine the OS

def get_operating_system
  if request.env['HTTP_USER_AGENT'].downcase.match(/mac/i)
    "Mac"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/windows/i)
    "Windows"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/linux/i)
    "Linux"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/unix/i)
    "Unix"
  else
    "Unknown"
  end
end

Hope it helps!

uday
  • 8,544
  • 4
  • 30
  • 54
  • What is `bt` supposed to be? There are also gems to do this. – Andrew Marshall Jun 12 '12 at 04:45
  • Yeah, true I thought of giving him some code. btw you can add more value to the answer by mentioning some gems :) – uday Jun 12 '12 at 04:56
  • @uDaY Thanks for Reply. But this will give me some of the browser details only. The Details which i am trying to capture is: If application is accessed on Windows 7 Operating System irrespective of browser. It should give me 'Windows 7 Home Basic/Premium/Enterprise If Application is accessed on Ubuntu irrespective of browser it should give me 'Ubuntu 11.04' and so on. So it is mainly dealing with fetching the Client side/Client Machine details. – AnkitG Jun 12 '12 at 17:32
  • @AndrewMarshall I am looking for "what OS the user accessing your application is using" not the server on which my application is running. – AnkitG Jun 12 '12 at 17:36
  • @AnkitGupta, see my **edit** you can create your custom actions based on the `request.env['HTTP_USER_AGENT']` – uday Jun 12 '12 at 18:15
  • @uDaY: The Edit is giving me the generic Details of whether Windows, Linux, Or Mac. I am looking for a exact details like "Windows 7 Home premium", "Windows XP", "Windows NT", "Fedora","Powerbook". These Details cannot be hardcoded and I have to find them on runtime. – AnkitG Jun 13 '12 at 03:08
  • You can customize it at your will & desire. – uday Jun 14 '12 at 18:10
  • what are the gems that do this? – Leahcim Jan 27 '13 at 07:48