Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#language and rspec 2 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
153 changes: 87 additions & 66 deletions lib/user-agent/agent.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Agent

##
Expand Down Expand Up @@ -52,6 +51,19 @@ def os
Agent.os_for_user_agent string
end

##
# User agent language.

def language
Agent.language string
end

##
# Rails locale
def locale
$1.to_sym if Agent.language(string).to_s =~ /(\w+)/
end

##
# User agent string.

Expand Down Expand Up @@ -80,82 +92,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
end

@agents = []
##
# 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

##
# Map agent _name_ to _options_.
##
# Return language extracted from _string_.
def language string
$1 if string =~ /\; (\D{2,5})\)/
end

@agents = []

def self.map name, options = {}
@agents << [name, options]
##
# Map agent _name_ to _options_.

def map name, options = {}
@agents << [name, options]
end
end

end
30 changes: 21 additions & 9 deletions spec/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,72 @@
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 == '#<Agent:Safari version:"4.0dp1" engine:"webkit:528.4" os:"OS X 10.5">'
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

describe '#locale' do
it 'should return :en locale' do
@agent.locale.should eq :en
end
end
end
31 changes: 15 additions & 16 deletions tasks/spec.rake
Original file line number Diff line number Diff line change
@@ -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

end