The Rails browser keeps a "common" for all information

I am developing an application in rails 5. The application works in both development and production environments. I am using the browser gem to get platform, browser and device information. However, it only returns "general" information for all browser, platform, and device information. If I use a query operation, that is, - request.env['HTTP_USER_AGENT']

or request.remote_ip

. I am getting the information I expect (yes, I know that IP information is not being returned through the browser. I only enabled it to "show" the browser and connection information). I ran a package update to install the latest browser and other gems.

I have a gem browser declared in Gemfile:

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

      

To the browser README.md to avoid having to make a declaration in the controller.

Below is a code snippet for using the browser (also in the browser's README.md ). connection is an object in my application to record the received 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 am using nginx and puma for production and development. The behavior is the same regardless of whether it is a production or production environment. Also in development I use nginx or workaround and go directly to puma "generic" is the information being reported.

The app uses HTTP 1.1 and is <!DOCTYPE html>

advertised for view (s).

Below is the result 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

      

+3


source to share


1 answer


You seem to be passing "Some User Agent"

as the first argument. Here's the post User-Agent

sent by the browser:

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

      

You also wrote:

In the browser, README.md to avoid having to do the declaration in the controller.

README

says (my attention):



If you want to use the browser in your Rails application, but don't want to taint your controller, use the following line in your Gemfile:

"Taint" means adding methods to all controllers automatically, not dropping the declaration. If this is the intended behavior, then feel free to leave the code as it is. However, if you want to have a method browser

available in all controller actions without additional configuration, put:

gem 'browser'

      

in Gemfile

. It should be automatically included Browser::ActionController

in the base controller using a method browser

defined like this:

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

      

+1


source







All Articles