From 3729a167283cd3b3de00e13e8b7b1d4a3788371c Mon Sep 17 00:00:00 2001 From: Eugene Russkikh Date: Thu, 22 Mar 2012 15:51:17 +0200 Subject: [PATCH 1/2] initial commit for languages and rspec 2 --- Gemfile.lock | 35 ++++++++++ lib/user-agent/agent.rb | 147 ++++++++++++++++++++++------------------ spec/agent_spec.rb | 24 ++++--- tasks/spec.rake | 31 ++++----- 4 files changed, 146 insertions(+), 91 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..92365ff --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,35 @@ +GEM + remote: http://rubygems.org/ + specs: + allison (2.0.3) + diff-lcs (1.1.3) + echoe (4.6.3) + allison (>= 2.0.3) + gemcutter (>= 0.7.0) + rake (>= 0.9.2) + rdoc (>= 3.6.1) + rubyforge (>= 2.0.4) + gemcutter (0.7.1) + json (1.6.5) + json_pure (1.6.5) + rake (0.9.2.2) + rdoc (3.12) + json (~> 1.4) + rspec (2.9.0) + rspec-core (~> 2.9.0) + rspec-expectations (~> 2.9.0) + rspec-mocks (~> 2.9.0) + rspec-core (2.9.0) + rspec-expectations (2.9.0) + diff-lcs (~> 1.1.3) + rspec-mocks (2.9.0) + rubyforge (2.0.4) + json_pure (>= 1.1.7) + +PLATFORMS + ruby + +DEPENDENCIES + echoe + rake + rspec diff --git a/lib/user-agent/agent.rb b/lib/user-agent/agent.rb index 9628164..6f0b5f3 100644 --- a/lib/user-agent/agent.rb +++ b/lib/user-agent/agent.rb @@ -1,4 +1,3 @@ - class Agent ## @@ -52,6 +51,13 @@ def os Agent.os_for_user_agent string end + ## + # User agent language. + + def language + Agent.language string + end + ## # User agent string. @@ -80,82 +86,91 @@ def == other ## # Return engine version for user agent _string_. - def self.engine_version_for_user_agent string - $1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i - end + class << self - ## - # Return version for user agent _string_. - - def self.version_for_user_agent string - case name = name_for_user_agent(string) - when :Chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i - when :Safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i - when :PS3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i - when :PSP ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i - else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i + def engine_version_for_user_agent string + $1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i end - end - ## - # Return engine symbol for user agent _string_. - - def self.engine_for_user_agent string - case string - when /webkit/i ; :webkit - when /khtml/i ; :khtml - when /konqueror/i ; :konqueror - when /chrome/i ; :chrome - when /presto/i ; :presto - when /gecko/i ; :gecko - when /msie/i ; :msie - else :unknown + ## + # Return version for user agent _string_. + + def version_for_user_agent string + case name = name_for_user_agent(string) + when :Chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i + when :Safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i + when :PS3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i + when :PSP ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i + else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i + end end - end - ## - # Return the os for user agent _string_. - - def self.os_for_user_agent string - case string - when /windows nt 6\.0/i ; :'Windows Vista' - when /windows nt 6\.\d+/i ; :'Windows 7' - when /windows nt 5\.2/i ; :'Windows 2003' - when /windows nt 5\.1/i ; :'Windows XP' - when /windows nt 5\.0/i ; :'Windows 2000' - when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}" - when /linux/i ; :Linux - when /wii/i ; :Wii - when /playstation 3/i ; :Playstation - when /playstation portable/i ; :Playstation - else ; :Unknown + ## + # Return engine symbol for user agent _string_. + + def engine_for_user_agent string + case string + when /webkit/i ; :webkit + when /khtml/i ; :khtml + when /konqueror/i ; :konqueror + when /chrome/i ; :chrome + when /presto/i ; :presto + when /gecko/i ; :gecko + when /msie/i ; :msie + else :unknown + end end - end - ## - # Return name for user agent _string_. - - def self.name_for_user_agent string - case string - when /konqueror/i ; :Konqueror - when /chrome/i ; :Chrome - when /safari/i ; :Safari - when /msie/i ; :IE - when /opera/i ; :Opera - when /playstation 3/i ; :PS3 - when /playstation portable/i ; :PSP - when /firefox/i ; :Firefox - else ; :Unknown + ## + # Return the os for user agent _string_. + + def os_for_user_agent string + case string + when /windows nt 6\.0/i ; :'Windows Vista' + when /windows nt 6\.\d+/i ; :'Windows 7' + when /windows nt 5\.2/i ; :'Windows 2003' + when /windows nt 5\.1/i ; :'Windows XP' + when /windows nt 5\.0/i ; :'Windows 2000' + when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}" + when /linux/i ; :Linux + when /wii/i ; :Wii + when /playstation 3/i ; :Playstation + when /playstation portable/i ; :Playstation + else ; :Unknown + end + end + + ## + # Return name for user agent _string_. + + def name_for_user_agent string + case string + when /konqueror/i ; :Konqueror + when /chrome/i ; :Chrome + when /safari/i ; :Safari + when /msie/i ; :IE + when /opera/i ; :Opera + when /playstation 3/i ; :PS3 + when /playstation portable/i ; :PSP + when /firefox/i ; :Firefox + else ; :Unknown + end end - end - @agents = [] + ## + # Return language extracted from _string_. + def language string + $1 if string =~ /\; (\D{2,5})\)/ + end - ## - # Map agent _name_ to _options_. + @agents = [] - def self.map name, options = {} - @agents << [name, options] + ## + # Map agent _name_ to _options_. + + def map name, options = {} + @agents << [name, options] + end end end diff --git a/spec/agent_spec.rb b/spec/agent_spec.rb index e14f02f..6d4fac7 100644 --- a/spec/agent_spec.rb +++ b/spec/agent_spec.rb @@ -5,60 +5,66 @@ before :each do @agent = Agent.new 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_4; en-us) AppleWebKit/528.4+ (KHTML, like Gecko) Version/4.0dp1 Safari/526.11.2' end - + describe "#initialize" do it "should allow a user agent string to be passed" do Agent.new('foo').string.should == 'foo' end end - + describe "#os" do it "should return operating system symbol" do @agent.os.should == :'OS X 10.5' end end - + describe "#engine" do it "should return engine symbol" do @agent.engine.should == :webkit end end - + describe "#engine_version" do it "should return engine version" do @agent.engine_version.should == '528.4' end end - + describe "#to_s" do it "should return the user agent string" do @agent.to_s.should == @agent.string end end - + describe "#inspect" do it "should return string presenting the engine, os, version, etc" do @agent.inspect.should == '#' end end - + describe "#name" do it "should return the agent name symbol" do @agent.name.should == :'Safari' end end - + describe "#==" do it "should be equal when the user agent strings are the same" do a = Agent.new 'foo' b = Agent.new 'foo' a.should == b end - + it "should not be equal when user agent strings are different" do a = Agent.new 'foo' b = Agent.new 'bar' a.should_not == b end end + + describe '#language' do + it 'should return en-us language' do + @agent.language.should eq 'en-us' + end + end end \ No newline at end of file diff --git a/tasks/spec.rake b/tasks/spec.rake index fbf9986..b7b0069 100644 --- a/tasks/spec.rake +++ b/tasks/spec.rake @@ -1,25 +1,24 @@ +require 'rspec/core/rake_task' -require 'spec/rake/spectask' - desc "Run all specifications" -Spec::Rake::SpecTask.new(:spec) do |t| - t.libs << "lib" - t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"] +RSpec::Core::RakeTask.new(:spec) do |t| + # t.libs << "lib" + t.rspec_opts = ["--color", "-fd", "--require", "spec/spec_helper.rb"] end - + namespace :spec do desc "Run all specifications verbosely" - Spec::Rake::SpecTask.new(:verbose) do |t| - t.libs << "lib" - t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"] + RSpec::Core::RakeTask.new(:verbose) do |t| + # t.libs << "lib" + t.rspec_opts = ["--color", "-fd", "--require", "spec/spec_helper.rb"] end - + desc "Run specific specification verbosely (specify SPEC)" - Spec::Rake::SpecTask.new(:select) do |t| - t.libs << "lib" - t.spec_files = [ENV["SPEC"]] - t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"] + RSpec::Core::RakeTask.new(:select) do |t| + # t.libs << "lib" + t.pattern = 'spec/*_spec.rb' + t.rspec_opts = ["--color", "-fd", "--require", "spec/spec_helper.rb"] end - -end \ No newline at end of file + +end From ca54c24edfb93e4307870a896559e16318e44d58 Mon Sep 17 00:00:00 2001 From: Eugene Russkikh Date: Thu, 22 Mar 2012 16:32:30 +0200 Subject: [PATCH 2/2] also, rails locale --- lib/user-agent/agent.rb | 6 ++++++ spec/agent_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/user-agent/agent.rb b/lib/user-agent/agent.rb index 6f0b5f3..49280c9 100644 --- a/lib/user-agent/agent.rb +++ b/lib/user-agent/agent.rb @@ -58,6 +58,12 @@ def language Agent.language string end + ## + # Rails locale + def locale + $1.to_sym if Agent.language(string).to_s =~ /(\w+)/ + end + ## # User agent string. diff --git a/spec/agent_spec.rb b/spec/agent_spec.rb index 6d4fac7..a4181cf 100644 --- a/spec/agent_spec.rb +++ b/spec/agent_spec.rb @@ -67,4 +67,10 @@ @agent.language.should eq 'en-us' end end + + describe '#locale' do + it 'should return :en locale' do + @agent.locale.should eq :en + end + end end \ No newline at end of file