Thursday 20 November 2014

Textual description of firstImageUrl

How To Get Client Operating System Name IN ROR

In the world of increasing web sites in a fast pace , at times we need to get the clients OS details to execute some script in clients machine, or know what is the OS/Web browser/Client Details used by client.


When ever a client accesses your website and makes a request, Browser sends the details of the client in as an WEBHTTP OBJECT.


HTTP_USER_AGENT request header contains the information about client .


To get this information in rails we need to write following code .


we create a around filter/action to and paste it in application controller so that it keeps the track of every request.


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
Thanks to Santosh for writing this post .