Skip to content

Commit 578b507

Browse files
committed
Adding new user-agent gem.
1 parent caf7619 commit 578b507

File tree

16 files changed

+480
-0
lines changed

16 files changed

+480
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source :gemcutter
2+
3+
gem "rake"
4+
gem "echoe"
5+
gem "rspec"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
=== 2010-05-07
3+
4+
* Support for Firefox
5+
6+
=== 1.0.0 / 2009-10-08
7+
8+
* Specs for Agent#==
9+
10+
=== 0.0.1 / 2009-08-28
11+
12+
* Initial release
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Gemfile
2+
History.rdoc
3+
Manifest
4+
README.rdoc
5+
Rakefile
6+
lib/user-agent.rb
7+
lib/user-agent/agent.rb
8+
lib/user-agent/version.rb
9+
spec/agent_spec.rb
10+
spec/agents_spec.rb
11+
spec/spec.opts
12+
spec/spec_helper.rb
13+
tasks/docs.rake
14+
tasks/gemspec.rake
15+
tasks/spec.rake
16+
user-agent.gemspec
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
= User Agent
3+
4+
User agent parser.
5+
6+
== Example
7+
8+
agent = Agent.new 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
9+
agent.name # => :Safari
10+
agent.version # => '4.0.3'
11+
agent.engine # => :webkit
12+
agent.os # => :'Windows Vista'
13+
agent.engine_version # => '531.9'
14+
15+
== Supported Agents
16+
17+
* Safari
18+
* Chrome
19+
* Opera
20+
* IE
21+
* Konqueror
22+
* PS3
23+
* PSP
24+
* Wii
25+
26+
== License:
27+
28+
(The MIT License)
29+
30+
Copyright (c) 2009 TJ Holowaychuk <[email protected]>
31+
32+
Permission is hereby granted, free of charge, to any person obtaining
33+
a copy of this software and associated documentation files (the
34+
'Software'), to deal in the Software without restriction, including
35+
without limitation the rights to use, copy, modify, merge, publish,
36+
distribute, sublicense, an d/or sell copies of the Software, and to
37+
permit persons to whom the Software is furnished to do so, subject to
38+
the following conditions:
39+
40+
The above copyright notice and this permission notice shall be
41+
included in all copies or substantial portions of the Software.
42+
43+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
$:.unshift 'lib'
3+
require 'user-agent'
4+
require 'rubygems'
5+
require 'rake'
6+
require 'echoe'
7+
8+
Echoe.new "user-agent", Agent::VERSION do |p|
9+
p.author = "TJ Holowaychuk"
10+
p.email = "[email protected]"
11+
p.summary = "User agent parser"
12+
p.runtime_dependencies = []
13+
end
14+
15+
Dir['tasks/**/*.rake'].sort.each { |f| load f }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#--
2+
# Copyright (c) 2009 TJ Holowaychuk <[email protected]>
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining
5+
# a copy of this software and associated documentation files (the
6+
# "Software"), to deal in the Software without restriction, including
7+
# without limitation the rights to use, copy, modify, merge, publish,
8+
# distribute, sublicense, and/or sell copies of the Software, and to
9+
# permit persons to whom the Software is furnished to do so, subject to
10+
# the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be
13+
# included in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
#++
23+
24+
require 'user-agent/agent'
25+
require 'user-agent/version'
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
2+
class Agent
3+
4+
##
5+
# User agent string.
6+
7+
attr_reader :string
8+
9+
##
10+
# Initialize with user agent _string_.
11+
12+
def initialize string
13+
@string = string.strip
14+
end
15+
16+
#--
17+
# Instance methods
18+
#++
19+
20+
##
21+
# User agent name symbol.
22+
23+
def name
24+
Agent.name_for_user_agent string
25+
end
26+
27+
##
28+
# User agent version.
29+
30+
def version
31+
Agent.version_for_user_agent string
32+
end
33+
34+
##
35+
# User agent engine symbol.
36+
37+
def engine
38+
Agent.engine_for_user_agent string
39+
end
40+
41+
##
42+
# User agent engine version string.
43+
44+
def engine_version
45+
Agent.engine_version_for_user_agent string
46+
end
47+
48+
##
49+
# User agent os symbol.
50+
51+
def os
52+
Agent.os_for_user_agent string
53+
end
54+
55+
##
56+
# User agent string.
57+
58+
def to_s
59+
string
60+
end
61+
62+
##
63+
# Inspect.
64+
65+
def inspect
66+
"#<Agent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
67+
end
68+
69+
##
70+
# Check if the agent is the same as _other_ agent.
71+
72+
def == other
73+
string == other.string
74+
end
75+
76+
#--
77+
# Class methods
78+
#++
79+
80+
##
81+
# Return engine version for user agent _string_.
82+
83+
def self.engine_version_for_user_agent string
84+
$1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i
85+
end
86+
87+
##
88+
# Return version for user agent _string_.
89+
90+
def self.version_for_user_agent string
91+
case name = name_for_user_agent(string)
92+
when :Chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i
93+
when :Safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i
94+
when :PS3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i
95+
when :PSP ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i
96+
else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i
97+
end
98+
end
99+
100+
##
101+
# Return engine symbol for user agent _string_.
102+
103+
def self.engine_for_user_agent string
104+
case string
105+
when /webkit/i ; :webkit
106+
when /khtml/i ; :khtml
107+
when /konqueror/i ; :konqueror
108+
when /chrome/i ; :chrome
109+
when /presto/i ; :presto
110+
when /gecko/i ; :gecko
111+
when /msie/i ; :msie
112+
else :unknown
113+
end
114+
end
115+
116+
##
117+
# Return the os for user agent _string_.
118+
119+
def self.os_for_user_agent string
120+
case string
121+
when /windows nt 6\.0/i ; :'Windows Vista'
122+
when /windows nt 6\.\d+/i ; :'Windows 7'
123+
when /windows nt 5\.2/i ; :'Windows 2003'
124+
when /windows nt 5\.1/i ; :'Windows XP'
125+
when /windows nt 5\.0/i ; :'Windows 2000'
126+
when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}"
127+
when /linux/i ; :Linux
128+
when /wii/i ; :Wii
129+
when /playstation 3/i ; :Playstation
130+
when /playstation portable/i ; :Playstation
131+
else ; :Unknown
132+
end
133+
end
134+
135+
##
136+
# Return name for user agent _string_.
137+
138+
def self.name_for_user_agent string
139+
case string
140+
when /konqueror/i ; :Konqueror
141+
when /chrome/i ; :Chrome
142+
when /safari/i ; :Safari
143+
when /msie/i ; :IE
144+
when /opera/i ; :Opera
145+
when /playstation 3/i ; :PS3
146+
when /playstation portable/i ; :PSP
147+
when /firefox/i ; :Firefox
148+
else ; :Unknown
149+
end
150+
end
151+
152+
@agents = []
153+
154+
##
155+
# Map agent _name_ to _options_.
156+
157+
def self.map name, options = {}
158+
@agents << [name, options]
159+
end
160+
161+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class Agent
3+
VERSION = '1.0.0'
4+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
require File.dirname(__FILE__) + '/spec_helper'
3+
4+
describe Agent do
5+
before :each do
6+
@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'
7+
end
8+
9+
describe "#initialize" do
10+
it "should allow a user agent string to be passed" do
11+
Agent.new('foo').string.should == 'foo'
12+
end
13+
end
14+
15+
describe "#os" do
16+
it "should return operating system symbol" do
17+
@agent.os.should == :'OS X 10.5'
18+
end
19+
end
20+
21+
describe "#engine" do
22+
it "should return engine symbol" do
23+
@agent.engine.should == :webkit
24+
end
25+
end
26+
27+
describe "#engine_version" do
28+
it "should return engine version" do
29+
@agent.engine_version.should == '528.4'
30+
end
31+
end
32+
33+
describe "#to_s" do
34+
it "should return the user agent string" do
35+
@agent.to_s.should == @agent.string
36+
end
37+
end
38+
39+
describe "#inspect" do
40+
it "should return string presenting the engine, os, version, etc" do
41+
@agent.inspect.should == '#<Agent:Safari version:"4.0dp1" engine:"webkit:528.4" os:"OS X 10.5">'
42+
end
43+
end
44+
45+
describe "#name" do
46+
it "should return the agent name symbol" do
47+
@agent.name.should == :'Safari'
48+
end
49+
end
50+
51+
describe "#==" do
52+
it "should be equal when the user agent strings are the same" do
53+
a = Agent.new 'foo'
54+
b = Agent.new 'foo'
55+
a.should == b
56+
end
57+
58+
it "should not be equal when user agent strings are different" do
59+
a = Agent.new 'foo'
60+
b = Agent.new 'bar'
61+
a.should_not == b
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)