Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails browser gem is returing 'generic' for all reported information

I am developing an application in rails 5. The application is up and running in both development and production environments. I am using the browser gem to obtain platform, browser, and device specific information. However, it is only returning 'generic' for all browser, platform, and device information. If I use the request operation, i.e. - request.env['HTTP_USER_AGENT'] or request.remote_ip. I get the expected information (yes, I know the IP information is not returned via browser. I included it just to 'show' the browser and connection information is available). I have run bundle update to ensure that the latest version of the browser and other gem(s) are installed.

I have the browser gem declared in Gemfile:

gem 'browser', require: 'browser/browser'

Per the browser README.md to avoid needing to make a declaration in a controller.

The following is the code snippet for how browser is being used (also per the browser README.md). connection is an object within my application for recording the obtained browser information.

    browser = Browser.new("Some User Agent", accept_language: "en-us")

    connection.browser_name = browser.name
    connection.browser_full_version = browser.full_version
    connection.browser_device_name = browser.device.name
    connection.browser_platform_name = browser.platform.name
    connection.browser_platform_version = browser.platform.version

I'm using nginx and puma for both production and development. The behavior is identical whether it is the production or development environment. Also, in development whether I use nginx or bypass and go directly to puma 'generic' is the reported information.

The application is using HTTP 1.1 and <!DOCTYPE html> is declared for the view(s).

The following is the output obtained from:

puts request.env['HTTP_USER_AGENT']

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0

puts browser

unknown generic generic0 other

puts browser.name

Generic Browser

puts browser.inspect

#<Browser::Generic:0x007fe24aa41710 @ua="Some User Agent", @accept_language=[#<Browser::AcceptLanguage:0x007fe24aa415d0 @part="en-us", @quality=1.0>], @platform=#<Browser::Platform:0x007fe24aa33d90 @ua="Some User Agent", @subject=#<Browser::Platform::Other:0x007fe24aa33b88 @ua="Some User Agent">>, @device=#<Browser::Device:0x007fe24aa41008 @ua="Some User Agent", @subject=#<Browser::Device::Unknown:0x007fe24aa40d38 @ua="Some User Agent">, @platform=#<Browser::Platform:0x007fe24aa32da0 @ua="Some User Agent", @subject=#<Browser::Platform::Other:0x007fe24aa32b98 @ua="Some User Agent">>>>

puts browser.to_yaml

--- !ruby/object:Browser::Generic
ua: Some User Agent
accept_language:
- !ruby/object:Browser::AcceptLanguage
  part: en-us
  quality: 1.0
platform: !ruby/object:Browser::Platform
  ua: Some User Agent
  subject: !ruby/object:Browser::Platform::Other
    ua: Some User Agent
device: !ruby/object:Browser::Device
  ua: Some User Agent
  subject: !ruby/object:Browser::Device::Unknown
    ua: Some User Agent
platform: !ruby/object:Browser::Platform
  ua: Some User Agent
  subject: !ruby/object:Browser::Platform::Other
    ua: Some User Agent

--- !ruby/object:Browser::Platform
ua: Some User Agent
like image 422
unclewaltyk Avatar asked Apr 30 '17 21:04

unclewaltyk


1 Answers

It seems you're passing "Some User Agent" as the first argument. This is where User-Agent sent by the browser should be passed:

browser = Browser.new(request.headers['User-Agent'], accept_language: request.headers["Accept-Language"])

You also wrote:

Per the browser README.md to avoid needing to make a declaration in a controller.

README says (emphasis mine):

If you want to use Browser on your Rails app but don't want to taint your controller, use the following line on your Gemfile:

"Taint" means automatically adding methods to all controllers, not avoiding a declaration. If this is the intended behavior then feel free to leave the code as it is. However, if you'd like to have a browser method available in all controller actions without having to do extra setup then put:

gem 'browser'

in Gemfile. It should automatically include Browser::ActionController into the base controller with the browser method defined like this:

def browser
  @browser ||= Browser.new(
    request.headers["User-Agent"],
    accept_language: request.headers["Accept-Language"]
  )
end
like image 104
Greg Navis Avatar answered Oct 13 '22 20:10

Greg Navis