browser_details 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -5,6 +5,26 @@ used to make a request.
5
5
 
6
6
  When possible this includes whether the browser has Javascript enabled or not.
7
7
 
8
+ ### Before
9
+
10
+ Started GET "/" for 127.0.0.1 at 2012-12-26 21:25:14 +0000
11
+ Processing by HomeController#index as HTML
12
+ ...
13
+ Started POST "/posts" for 127.0.0.1 at 2012-12-26 21:25:19 +0000
14
+ Processing by PostsController#create as HTML
15
+ ...
16
+
17
+ ### After
18
+
19
+ Started GET "/" for 127.0.0.1 at 2012-12-26 21:25:14 +0000
20
+ Chrome 23.0.1271.95 (Macintosh)
21
+ Processing by HomeController#index as HTML
22
+ ...
23
+ Started POST "/posts" for 127.0.0.1 at 2012-12-26 21:25:19 +0000
24
+ Chrome 23.0.1271.95 (Macintosh), JS enabled
25
+ Processing by PostsController#create as HTML
26
+ ...
27
+
8
28
  ## Installation
9
29
 
10
30
  Add this line to your application's Gemfile:
@@ -3,27 +3,57 @@
3
3
  require "browser_details/version"
4
4
  require "useragent"
5
5
 
6
+ # Public: Middleware for logging the browser details of each request.
7
+ #
6
8
  class BrowserDetails
7
9
 
10
+ # Set up the log_message method.
8
11
  if defined?(Hatchet)
12
+ # If Hatchet is defined include it and define a method for its logger.
9
13
  include Hatchet
10
14
 
11
- LOGGER = :hatchet
15
+ def log_message(env, message)
16
+ log.info(message)
17
+ end
12
18
  elsif defined?(Rails)
13
- LOGGER = :rails
19
+ # If Rails is defined define a method for its logger.
20
+ def log_message(env, message)
21
+ Rails.logger.info(message)
22
+ end
14
23
  else
15
- LOGGER = :rack
24
+ # Otherwise check if the env includes a logger and if so log to that.
25
+ def log_message(env, message)
26
+ if env['rack.logger']
27
+ env['rack.logger'].info(message)
28
+ end
29
+ end
16
30
  end
17
31
 
32
+ # Make whatever log_message method that was defined private.
33
+ private :log_message
34
+
35
+ # Public: Creates a new instance.
36
+ #
37
+ # app - The application this middleware is wrapping.
38
+ #
18
39
  def initialize(app)
19
40
  @app = app
20
41
  end
21
42
 
43
+ # Public: Log the browser details if possible and then forward the request on
44
+ # to the wrapped application.
45
+ #
46
+ # env - The environment of the request.
47
+ #
48
+ # Returns the result generated by the wrapped application.
49
+ #
22
50
  def call(env)
51
+ request = Rack::Request.new(env)
23
52
  message = []
24
53
 
25
- if env['HTTP_USER_AGENT']
26
- agent = UserAgent.parse(env['HTTP_USER_AGENT'])
54
+ # Add the user agent details to the message if present.
55
+ if request.user_agent
56
+ agent = UserAgent.parse(request.user_agent)
27
57
 
28
58
  agent_details = [agent.browser]
29
59
  agent_details << 'Mobile' if agent.mobile?
@@ -33,9 +63,12 @@ class BrowserDetails
33
63
  message << agent_details.join(' ')
34
64
  end
35
65
 
36
- request = Rack::Request.new(env)
37
-
38
- if request['utf8']
66
+ # Add whether Javascript is enabled or not if it is possible to tell.
67
+ if request.xhr?
68
+ # AJAX request - JS probably enabled.
69
+ message << 'JS enabled'
70
+ elsif request['utf8']
71
+ # Have a utf8 element - check if changed by JS.
39
72
  message << if request['utf8'] == '✓'
40
73
  'JS disabled'
41
74
  else
@@ -43,26 +76,16 @@ class BrowserDetails
43
76
  end
44
77
  end
45
78
 
79
+ # Log a message if any details were gathered.
46
80
  unless message.empty?
47
81
  log_message(env, message.join(', '))
48
82
  end
49
83
 
84
+ # Delegate to the application we are wrapping.
50
85
  @app.call(env)
51
86
  end
52
87
 
53
- def log_message(env, message)
54
- case LOGGER
55
- when :hatchet
56
- log.info(message)
57
- when :rails
58
- Rails.logger.info(message)
59
- when :rack
60
- if env['rack.logger']
61
- env['rack.logger'].info(message)
62
- end
63
- end
64
- end
65
-
66
88
  end
67
89
 
90
+ # Require the Railtie if Rails is present.
68
91
  require 'browser_details/railtie' if defined?(Rails)
@@ -1,5 +1,8 @@
1
1
  class BrowserDetails
2
2
 
3
+ # Public: Railtie for initializing the BrowserDetails middleware and making
4
+ # the browser_details.js file available.
5
+ #
3
6
  class Railtie < Rails::Railtie
4
7
  initializer "browser_details.insert_middleware" do |app|
5
8
  app.config.middleware.use BrowserDetails
@@ -1,3 +1,5 @@
1
1
  class BrowserDetails
2
- VERSION = "0.0.2"
2
+
3
+ VERSION = "0.0.3"
4
+
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browser_details
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: