agent_orange 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/agent_orange/base.rb +13 -1
- data/lib/agent_orange/browser.rb +6 -11
- data/lib/agent_orange/device.rb +20 -25
- data/lib/agent_orange/engine.rb +9 -14
- data/lib/agent_orange/operating_system.rb +7 -12
- data/lib/agent_orange/platform.rb +9 -14
- data/lib/agent_orange/user_agent.rb +31 -30
- data/lib/agent_orange/version.rb +9 -9
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
= agent_orange
|
2
|
-
{<img src="https://secure.travis-ci.org/kevinelliott/agent_orange.png">}[http://travis-ci.org/kevinelliott/agent_orange]
|
2
|
+
{<img src="https://secure.travis-ci.org/kevinelliott/agent_orange.png">}[http://travis-ci.org/kevinelliott/agent_orange] {<img src="https://gemnasium.com/kevinelliott/agent_orange.png" alt="Dependency Status" />}[https://gemnasium.com/kevinelliott/agent_orange] {<img src="https://codeclimate.com/badge.png" alt="Code Climate" />}[https://codeclimate.com/github/kevinelliott/agent_orange]
|
3
3
|
|
4
4
|
User Agent detection for Ruby. ActionController integration coming soon.
|
5
5
|
|
data/lib/agent_orange/base.rb
CHANGED
@@ -2,9 +2,14 @@ module AgentOrange
|
|
2
2
|
class Base
|
3
3
|
|
4
4
|
def initialize(user_agent)
|
5
|
-
|
5
|
+
parse(user_agent)
|
6
6
|
end
|
7
7
|
|
8
|
+
def parse(user_agent)
|
9
|
+
raise 'Any class that extends AgentOrange::Base must implement parse(user_agent)'
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [Array]
|
8
13
|
def parse_user_agent_string_into_groups(user_agent)
|
9
14
|
results = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
|
10
15
|
groups = []
|
@@ -16,6 +21,7 @@ module AgentOrange
|
|
16
21
|
groups
|
17
22
|
end
|
18
23
|
|
24
|
+
# @return [Array]
|
19
25
|
def parse_comment(comment)
|
20
26
|
groups = []
|
21
27
|
comment.split('; ').each do |piece|
|
@@ -32,6 +38,7 @@ module AgentOrange
|
|
32
38
|
groups
|
33
39
|
end
|
34
40
|
|
41
|
+
# @return [String]
|
35
42
|
def determine_type(types={}, content="")
|
36
43
|
# Determine type
|
37
44
|
type = nil
|
@@ -54,5 +61,10 @@ module AgentOrange
|
|
54
61
|
AgentOrange.debug " Version: #{self.version}", 2
|
55
62
|
end
|
56
63
|
|
64
|
+
def analysis
|
65
|
+
AgentOrange.debug self.class.name.upcase + ' ANALYSIS', 2
|
66
|
+
debug_content(:type => type, :name => name, :version => version)
|
67
|
+
AgentOrange.debug '', 2
|
68
|
+
end
|
57
69
|
end
|
58
70
|
end
|
data/lib/agent_orange/browser.rb
CHANGED
@@ -48,32 +48,27 @@ module AgentOrange
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
|
51
|
+
populate(chosen_content)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
|
55
|
+
analysis
|
56
56
|
end
|
57
57
|
|
58
|
+
# @return [Browser]
|
58
59
|
def populate(content={})
|
59
|
-
|
60
|
+
debug_raw_content(content)
|
60
61
|
AgentOrange.debug "", 2
|
61
62
|
|
62
|
-
self.type =
|
63
|
+
self.type = determine_type(BROWSERS, content[:name])
|
63
64
|
self.name = content[:name]
|
64
65
|
self.version = AgentOrange::Version.new(content[:version])
|
65
66
|
self
|
66
67
|
end
|
67
68
|
|
68
|
-
def analysis
|
69
|
-
AgentOrange.debug "BROWSER ANALYSIS", 2
|
70
|
-
self.debug_content(:type => self.type, :name => self.name, :version => self.version)
|
71
|
-
AgentOrange.debug "", 2
|
72
|
-
end
|
73
|
-
|
74
69
|
# @return {String}
|
75
70
|
def to_s
|
76
|
-
[
|
71
|
+
[name, version].compact.join(' ')
|
77
72
|
end
|
78
73
|
end
|
79
74
|
end
|
data/lib/agent_orange/device.rb
CHANGED
@@ -48,58 +48,53 @@ module AgentOrange
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
|
51
|
+
analysis
|
52
52
|
|
53
|
-
self.platform
|
53
|
+
self.platform = AgentOrange::Platform.new(user_agent)
|
54
54
|
self.operating_system = AgentOrange::OperatingSystem.new(user_agent)
|
55
|
-
self.engine
|
55
|
+
self.engine = AgentOrange::Engine.new(user_agent)
|
56
56
|
end
|
57
57
|
|
58
|
+
# @return [Device]
|
58
59
|
def populate(content={})
|
59
|
-
|
60
|
+
debug_raw_content(content)
|
60
61
|
AgentOrange.debug "", 2
|
61
62
|
|
62
|
-
self.type =
|
63
|
-
self.bot
|
64
|
-
if (
|
63
|
+
self.type = determine_type(DEVICES, content[:comment])
|
64
|
+
self.bot = determine_type(BOTS, content[:comment]) == :bot
|
65
|
+
if (bot && type == "other")
|
65
66
|
self.type = :bot # backwards compatability
|
66
67
|
end
|
67
|
-
self.name =
|
68
|
+
self.name = type.to_s.capitalize
|
68
69
|
self.version = nil
|
69
70
|
self
|
70
71
|
end
|
71
72
|
|
72
|
-
def analysis
|
73
|
-
AgentOrange.debug "DEVICE ANALYSIS", 2
|
74
|
-
self.debug_content(:type => self.type, :name => self.name, :version => self.version)
|
75
|
-
AgentOrange.debug "", 2
|
76
|
-
end
|
77
|
-
|
78
73
|
# @return [Boolean]
|
79
74
|
def is_computer?(name=nil)
|
80
75
|
if name
|
81
76
|
case name
|
82
77
|
when String
|
83
|
-
return
|
78
|
+
return platform.name.downcase.include?(name.downcase)
|
84
79
|
when Symbol
|
85
|
-
return
|
80
|
+
return platform.name.downcase.include?(name.to_s.downcase)
|
86
81
|
end
|
87
82
|
else
|
88
|
-
(
|
83
|
+
(type == :computer)
|
89
84
|
end
|
90
85
|
end
|
91
86
|
|
92
87
|
# @return [Boolean]
|
93
88
|
def is_mobile?(name=nil)
|
94
|
-
if !name.nil? && !
|
89
|
+
if !name.nil? && !platform.name.nil?
|
95
90
|
case name
|
96
91
|
when String
|
97
|
-
return
|
92
|
+
return platform.name.downcase.include?(name.downcase) || platform.version.downcase.include?(name.downcase)
|
98
93
|
when Symbol
|
99
|
-
return
|
94
|
+
return platform.name.downcase.include?(name.to_s.downcase) || platform.version.to_s.downcase.include?(name.to_s.downcase)
|
100
95
|
end
|
101
96
|
else
|
102
|
-
(
|
97
|
+
(type == :mobile)
|
103
98
|
end
|
104
99
|
end
|
105
100
|
|
@@ -108,18 +103,18 @@ module AgentOrange
|
|
108
103
|
if name
|
109
104
|
case name
|
110
105
|
when String
|
111
|
-
return
|
106
|
+
return name.downcase.include?(name.downcase)
|
112
107
|
when Symbol
|
113
|
-
return
|
108
|
+
return name.downcase.include?(name.to_s.downcase)
|
114
109
|
end
|
115
110
|
else
|
116
|
-
|
111
|
+
bot
|
117
112
|
end
|
118
113
|
end
|
119
114
|
|
120
115
|
# @return [String]
|
121
116
|
def to_s
|
122
|
-
[
|
117
|
+
[name, version].compact.join(' ')
|
123
118
|
end
|
124
119
|
end
|
125
120
|
end
|
data/lib/agent_orange/engine.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'agent_orange/base'
|
2
2
|
require 'agent_orange/browser'
|
3
3
|
require 'agent_orange/version'
|
4
|
-
require 'pp'
|
5
4
|
|
6
5
|
module AgentOrange
|
7
6
|
class Engine < Base
|
@@ -32,7 +31,7 @@ module AgentOrange
|
|
32
31
|
groups.each_with_index do |content,i|
|
33
32
|
if content[:name] =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
|
34
33
|
# Matched group against name
|
35
|
-
|
34
|
+
populate(content)
|
36
35
|
elsif content[:comment] =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
|
37
36
|
# Matched group against comment
|
38
37
|
chosen_content = { :name => nil, :version => nil }
|
@@ -43,33 +42,29 @@ module AgentOrange
|
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
|
-
|
45
|
+
populate(chosen_content)
|
47
46
|
end
|
48
47
|
end
|
49
48
|
|
50
|
-
|
49
|
+
analysis
|
50
|
+
|
51
51
|
self.browser = AgentOrange::Browser.new(user_agent)
|
52
52
|
end
|
53
53
|
|
54
|
+
# @return [Engine]
|
54
55
|
def populate(content={})
|
55
|
-
|
56
|
+
debug_raw_content(content)
|
56
57
|
AgentOrange.debug "", 2
|
57
58
|
|
58
|
-
self.type =
|
59
|
-
self.name = ENGINES[
|
59
|
+
self.type = determine_type(ENGINES, content[:name])
|
60
|
+
self.name = ENGINES[type.to_sym]
|
60
61
|
self.version = AgentOrange::Version.new(content[:version])
|
61
62
|
self
|
62
63
|
end
|
63
64
|
|
64
|
-
def analysis
|
65
|
-
AgentOrange.debug "ENGINE ANALYSIS", 2
|
66
|
-
self.debug_content(:type => self.type, :name => self.name, :version => self.version)
|
67
|
-
AgentOrange.debug "", 2
|
68
|
-
end
|
69
|
-
|
70
65
|
# @return [String]
|
71
66
|
def to_s
|
72
|
-
[
|
67
|
+
[name, version].compact.join(' ') || "Unknown"
|
73
68
|
end
|
74
69
|
end
|
75
70
|
end
|
@@ -46,32 +46,27 @@ module AgentOrange
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
populate(chosen_content)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
|
53
|
+
analysis
|
54
54
|
end
|
55
55
|
|
56
|
+
# @return [OperatingSystem]
|
56
57
|
def populate(content={})
|
57
|
-
|
58
|
+
debug_raw_content(content)
|
58
59
|
AgentOrange.debug "", 2
|
59
60
|
|
60
|
-
self.type =
|
61
|
-
self.name = OPERATING_SYSTEM_NAMES[
|
61
|
+
self.type = determine_type(OPERATING_SYSTEMS, content[:name])
|
62
|
+
self.name = OPERATING_SYSTEM_NAMES[type.to_sym]
|
62
63
|
self.version = AgentOrange::Version.new(content[:version])
|
63
64
|
self
|
64
65
|
end
|
65
66
|
|
66
|
-
def analysis
|
67
|
-
AgentOrange.debug "OPERATING SYSTEM ANALYSIS", 2
|
68
|
-
self.debug_content(:type => self.type, :name => self.name, :version => self.version)
|
69
|
-
AgentOrange.debug "", 2
|
70
|
-
end
|
71
|
-
|
72
67
|
# @return [String]
|
73
68
|
def to_s
|
74
|
-
[
|
69
|
+
[name, version].compact.join(' ')
|
75
70
|
end
|
76
71
|
end
|
77
72
|
end
|
@@ -38,22 +38,23 @@ module AgentOrange
|
|
38
38
|
groups.each_with_index do |content,i|
|
39
39
|
if content[:comment] =~ /(#{PLATFORMS.collect{|cat,regex| regex}.join(')|(')})/i
|
40
40
|
# Matched group against name
|
41
|
-
|
41
|
+
populate(content)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
|
45
|
+
analysis
|
46
46
|
end
|
47
47
|
|
48
|
+
# @return [Platform]
|
48
49
|
def populate(content={})
|
49
|
-
|
50
|
+
debug_raw_content(content)
|
50
51
|
AgentOrange.debug "", 2
|
51
52
|
|
52
|
-
self.type =
|
53
|
-
self.name = PLATFORM_NAMES[
|
53
|
+
self.type = determine_type(PLATFORMS, content[:comment])
|
54
|
+
self.name = PLATFORM_NAMES[type.to_sym]
|
54
55
|
|
55
|
-
if
|
56
|
-
self.version = case
|
56
|
+
if type == :apple
|
57
|
+
self.version = case determine_type(PLATFORM_VERSIONS, content[:comment])
|
57
58
|
when :ipad
|
58
59
|
AgentOrange::Version.new("iPad")
|
59
60
|
when :iphone
|
@@ -70,14 +71,8 @@ module AgentOrange
|
|
70
71
|
self
|
71
72
|
end
|
72
73
|
|
73
|
-
def analysis
|
74
|
-
AgentOrange.debug "PLATFORM ANALYSIS", 2
|
75
|
-
self.debug_content(:type => self.type, :name => self.name, :version => self.version)
|
76
|
-
AgentOrange.debug "", 2
|
77
|
-
end
|
78
|
-
|
79
74
|
def to_s
|
80
|
-
[
|
75
|
+
[name, version].compact.join(' ')
|
81
76
|
end
|
82
77
|
end
|
83
78
|
end
|
@@ -15,51 +15,52 @@ module AgentOrange
|
|
15
15
|
|
16
16
|
# @param [String] user_agent_string
|
17
17
|
def initialize(user_agent_string)
|
18
|
-
|
18
|
+
parse(user_agent_string)
|
19
19
|
end
|
20
20
|
|
21
21
|
def parse(user_agent)
|
22
22
|
self.user_agent_string = user_agent
|
23
|
-
self.device = AgentOrange::Device.new(
|
23
|
+
self.device = AgentOrange::Device.new(user_agent_string)
|
24
24
|
|
25
|
-
AgentOrange.debug "Device = #{
|
26
|
-
AgentOrange.debug "Platform = #{
|
27
|
-
AgentOrange.debug "OS = #{
|
28
|
-
AgentOrange.debug "Engine = #{
|
29
|
-
AgentOrange.debug "Browser = #{
|
25
|
+
AgentOrange.debug "Device = #{device}"
|
26
|
+
AgentOrange.debug "Platform = #{device.platform}"
|
27
|
+
AgentOrange.debug "OS = #{device.operating_system}"
|
28
|
+
AgentOrange.debug "Engine = #{device.engine}"
|
29
|
+
AgentOrange.debug "Browser = #{device.engine.browser}"
|
30
30
|
|
31
|
-
|
31
|
+
summary
|
32
32
|
end
|
33
33
|
|
34
34
|
# @return [Boolean]
|
35
35
|
def is_computer?(type=nil)
|
36
|
-
|
36
|
+
device.is_computer?(type)
|
37
37
|
end
|
38
38
|
|
39
39
|
# @return [Boolean]
|
40
40
|
def is_mobile?(type=nil)
|
41
|
-
|
41
|
+
device.is_mobile?(type)
|
42
42
|
end
|
43
43
|
|
44
44
|
# @return [Boolean]
|
45
45
|
def is_bot?(type=nil)
|
46
|
-
|
46
|
+
device.is_bot?(type)
|
47
47
|
end
|
48
48
|
|
49
49
|
# @return [String]
|
50
50
|
def to_s
|
51
|
-
[
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
[
|
52
|
+
device,
|
53
|
+
device.platform,
|
54
|
+
device.operating_system,
|
55
|
+
device.engine,
|
56
|
+
device.engine.browser
|
57
|
+
].compact.join(", ")
|
57
58
|
end
|
58
59
|
|
59
60
|
# @return [String]
|
60
61
|
def to_human_string
|
61
|
-
if
|
62
|
-
"User has a #{
|
62
|
+
if device && device.engine && device.engine.browser
|
63
|
+
"User has a #{device} running #{device.engine.browser} (which is based on #{device.engine})."
|
63
64
|
else
|
64
65
|
"User has some kind of device that I've never seen."
|
65
66
|
end
|
@@ -68,19 +69,19 @@ module AgentOrange
|
|
68
69
|
def summary
|
69
70
|
AgentOrange.debug
|
70
71
|
AgentOrange.debug "SUMMARY"
|
71
|
-
AgentOrange.debug " Is computer? #{
|
72
|
-
if
|
73
|
-
AgentOrange.debug " Is a Mac? #{
|
74
|
-
AgentOrange.debug " Is a PC? #{
|
72
|
+
AgentOrange.debug " Is computer? #{is_computer?}"
|
73
|
+
if is_computer?
|
74
|
+
AgentOrange.debug " Is a Mac? #{is_computer? :mac}"
|
75
|
+
AgentOrange.debug " Is a PC? #{is_computer? :pc}"
|
75
76
|
end
|
76
|
-
AgentOrange.debug " Is mobile? #{
|
77
|
-
if
|
78
|
-
AgentOrange.debug " Is an iPhone? #{
|
79
|
-
AgentOrange.debug " Is an iPad? #{
|
80
|
-
AgentOrange.debug " Is an iPod? #{
|
81
|
-
AgentOrange.debug " Is an Android? #{
|
77
|
+
AgentOrange.debug " Is mobile? #{is_mobile?}"
|
78
|
+
if is_mobile?
|
79
|
+
AgentOrange.debug " Is an iPhone? #{is_mobile? :iphone}"
|
80
|
+
AgentOrange.debug " Is an iPad? #{is_mobile? :ipad}"
|
81
|
+
AgentOrange.debug " Is an iPod? #{is_mobile? :ipod}"
|
82
|
+
AgentOrange.debug " Is an Android? #{is_mobile? :android}"
|
82
83
|
end
|
83
|
-
AgentOrange.debug " Is bot? #{
|
84
|
+
AgentOrange.debug " Is bot? #{is_bot?}"
|
84
85
|
AgentOrange.debug
|
85
86
|
end
|
86
87
|
end
|
data/lib/agent_orange/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module AgentOrange
|
2
|
-
VERSION = "0.1.
|
2
|
+
VERSION = "0.1.6" # This is for the gem and does not conflict with the rest of the functionality
|
3
3
|
|
4
4
|
class Version
|
5
5
|
# @return [String, nil]
|
@@ -16,23 +16,23 @@ module AgentOrange
|
|
16
16
|
|
17
17
|
def initialize(version_string)
|
18
18
|
version_string = sanitize_version_string(version_string)
|
19
|
-
self.major
|
20
|
-
self.minor
|
21
|
-
self.patch_level
|
19
|
+
self.major = nil
|
20
|
+
self.minor = nil
|
21
|
+
self.patch_level = nil
|
22
22
|
self.build_number = nil
|
23
23
|
|
24
|
-
pieces
|
24
|
+
pieces = version_string.split('.')
|
25
25
|
pieces_count = pieces.count
|
26
26
|
|
27
|
-
self.major
|
28
|
-
self.minor
|
29
|
-
self.patch_level
|
27
|
+
self.major = pieces[0] if pieces_count >= 1
|
28
|
+
self.minor = pieces[1] if pieces_count >= 2
|
29
|
+
self.patch_level = pieces[2] if pieces_count >= 3
|
30
30
|
self.build_number = pieces[3] if pieces_count >= 4
|
31
31
|
end
|
32
32
|
|
33
33
|
# @return [String]
|
34
34
|
def to_s
|
35
|
-
[
|
35
|
+
[major, minor, patch_level, build_number].compact.join('.')
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: agent_orange
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
104
|
version: '0'
|
105
105
|
segments:
|
106
106
|
- 0
|
107
|
-
hash:
|
107
|
+
hash: 3541338043838672934
|
108
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
109
|
none: false
|
110
110
|
requirements:
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
version: '0'
|
114
114
|
segments:
|
115
115
|
- 0
|
116
|
-
hash:
|
116
|
+
hash: 3541338043838672934
|
117
117
|
requirements: []
|
118
118
|
rubyforge_project: agent_orange
|
119
119
|
rubygems_version: 1.8.24
|