Class: BrowserDetails

Inherits:
Object
  • Object
show all
Includes:
Hatchet
Defined in:
lib/browser_details.rb,
lib/browser_details/railtie.rb,
lib/browser_details/version.rb

Overview

Public: Middleware for logging the browser details of each request.

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ BrowserDetails

Public: Creates a new instance.

app - The application this middleware is wrapping.



38
39
40
# File 'lib/browser_details.rb', line 38

def initialize(app)
  @app = app
end

Class Method Details

.message(request) ⇒ Object

Public: Creates a browser details message for a request.

request - The Rack::Request to extract brower details from.

Returns a String of browser details for the request.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/browser_details.rb', line 68

def self.message(request)
  message = []

  # Add the user agent details to the message if present.
  if request.user_agent
    agent = UserAgent.parse(request.user_agent)

    agent_details = [agent.browser]
    agent_details << 'Mobile' if agent.mobile?
    agent_details << agent.version
    agent_details << "(#{agent.platform}, #{agent.os})"

    message << agent_details.join(' ')
  end

  # Add whether Javascript is enabled or not if it is possible to tell.
  if request.xhr?
    # AJAX request - JS probably enabled.
    message << 'JS enabled'
  elsif request['utf8']
    # Have a utf8 parameter - check if changed by JS.
    message << if request['utf8'] == ''
      # Value unchanged - JS was not executed and therefore disabled.
      'JS disabled'
    else
      # Value changed - JS was executed and therefore enabled.
      'JS enabled'
    end
  end

  message.join(', ')
end

Instance Method Details

#call(env) ⇒ Object

Public: Log the browser details if possible and then forward the request on to the wrapped application.

env - The environment of the request.

Returns the result generated by the wrapped application.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/browser_details.rb', line 49

def call(env)
  request = Rack::Request.new(env)
  message = self.class.message(request)

  # Log a message if any details were gathered.
  unless message.empty?
    log_message(env, message)
  end

  # Delegate to the application we are wrapping.
  @app.call(env)
end