Commit 49b0fe02689365104ba52b427922168824e9274d

Authored by Bob Lail
1 parent bd17fa78
Exists in master and in 1 other branch production

use Josh Peek's useragent gem to parse HTTP_USER_AGENT

@@ -8,6 +8,7 @@ gem 'will_paginate' @@ -8,6 +8,7 @@ gem 'will_paginate'
8 gem 'devise', '~> 1.1.8' 8 gem 'devise', '~> 1.1.8'
9 gem 'lighthouse-api' 9 gem 'lighthouse-api'
10 gem 'redmine_client', :git => "git://github.com/oruen/redmine_client.git" 10 gem 'redmine_client', :git => "git://github.com/oruen/redmine_client.git"
  11 +gem 'useragent', '~> 0.3.1'
11 12
12 platform :ruby do 13 platform :ruby do
13 gem 'bson_ext', '~> 1.2' 14 gem 'bson_ext', '~> 1.2'
@@ -109,6 +109,7 @@ GEM @@ -109,6 +109,7 @@ GEM
109 treetop (1.4.9) 109 treetop (1.4.9)
110 polyglot (>= 0.3.1) 110 polyglot (>= 0.3.1)
111 tzinfo (0.3.25) 111 tzinfo (0.3.25)
  112 + useragent (0.3.1)
112 warden (1.0.3) 113 warden (1.0.3)
113 rack (>= 1.0.0) 114 rack (>= 1.0.0)
114 webmock (1.6.2) 115 webmock (1.6.2)
@@ -132,5 +133,6 @@ DEPENDENCIES @@ -132,5 +133,6 @@ DEPENDENCIES
132 redmine_client! 133 redmine_client!
133 rspec (~> 2.5) 134 rspec (~> 2.5)
134 rspec-rails (~> 2.5) 135 rspec-rails (~> 2.5)
  136 + useragent (~> 0.3.1)
135 webmock 137 webmock
136 will_paginate 138 will_paginate
app/models/notice.rb
@@ -46,6 +46,11 @@ class Notice @@ -46,6 +46,11 @@ class Notice
46 }) 46 })
47 end 47 end
48 48
  49 + def user_agent
  50 + agent_string = env_vars['HTTP_USER_AGENT']
  51 + agent_string.blank? ? nil : UserAgent.parse(agent_string)
  52 + end
  53 +
49 def request 54 def request
50 read_attribute(:request) || {} 55 read_attribute(:request) || {}
51 end 56 end
spec/models/notice_spec.rb
@@ -105,6 +105,19 @@ describe Notice do @@ -105,6 +105,19 @@ describe Notice do
105 end 105 end
106 end 106 end
107 107
  108 + describe "user agent" do
  109 + it "should be parsed and human-readable" do
  110 + notice = Factory.build(:notice, :request => {'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16'}})
  111 + notice.user_agent.browser.should == 'Chrome'
  112 + notice.user_agent.version.to_s.should =~ /^10\.0/
  113 + end
  114 +
  115 + it "should be nil if HTTP_USER_AGENT is blank" do
  116 + notice = Factory.build(:notice)
  117 + notice.user_agent.should == nil
  118 + end
  119 + end
  120 +
108 describe "email notifications" do 121 describe "email notifications" do
109 before do 122 before do
110 @app = Factory(:app_with_watcher) 123 @app = Factory(:app_with_watcher)