From 49b0fe02689365104ba52b427922168824e9274d Mon Sep 17 00:00:00 2001 From: Bob Lail Date: Fri, 22 Apr 2011 12:01:53 -0500 Subject: [PATCH] use Josh Peek's useragent gem to parse HTTP_USER_AGENT --- Gemfile | 1 + Gemfile.lock | 2 ++ app/models/notice.rb | 5 +++++ spec/models/notice_spec.rb | 13 +++++++++++++ 4 files changed, 21 insertions(+), 0 deletions(-) diff --git a/Gemfile b/Gemfile index f0f9cc6..2341de9 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'will_paginate' gem 'devise', '~> 1.1.8' gem 'lighthouse-api' gem 'redmine_client', :git => "git://github.com/oruen/redmine_client.git" +gem 'useragent', '~> 0.3.1' platform :ruby do gem 'bson_ext', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index 69b33db..f0a9b55 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -109,6 +109,7 @@ GEM treetop (1.4.9) polyglot (>= 0.3.1) tzinfo (0.3.25) + useragent (0.3.1) warden (1.0.3) rack (>= 1.0.0) webmock (1.6.2) @@ -132,5 +133,6 @@ DEPENDENCIES redmine_client! rspec (~> 2.5) rspec-rails (~> 2.5) + useragent (~> 0.3.1) webmock will_paginate diff --git a/app/models/notice.rb b/app/models/notice.rb index 83b5ce1..4006722 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -46,6 +46,11 @@ class Notice }) end + def user_agent + agent_string = env_vars['HTTP_USER_AGENT'] + agent_string.blank? ? nil : UserAgent.parse(agent_string) + end + def request read_attribute(:request) || {} end diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index 2d89542..345fc52 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -105,6 +105,19 @@ describe Notice do end end + describe "user agent" do + it "should be parsed and human-readable" do + 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'}}) + notice.user_agent.browser.should == 'Chrome' + notice.user_agent.version.to_s.should =~ /^10\.0/ + end + + it "should be nil if HTTP_USER_AGENT is blank" do + notice = Factory.build(:notice) + notice.user_agent.should == nil + end + end + describe "email notifications" do before do @app = Factory(:app_with_watcher) -- libgit2 0.21.2