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.
@@ -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
 
@@ -2,9 +2,14 @@ module AgentOrange
2
2
  class Base
3
3
 
4
4
  def initialize(user_agent)
5
- self.parse(user_agent)
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
@@ -48,32 +48,27 @@ module AgentOrange
48
48
  end
49
49
  end
50
50
 
51
- self.populate(chosen_content)
51
+ populate(chosen_content)
52
52
  end
53
53
  end
54
54
 
55
- self.analysis
55
+ analysis
56
56
  end
57
57
 
58
+ # @return [Browser]
58
59
  def populate(content={})
59
- self.debug_raw_content(content)
60
+ debug_raw_content(content)
60
61
  AgentOrange.debug "", 2
61
62
 
62
- self.type = self.determine_type(BROWSERS, content[:name])
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
- [self.name, self.version].compact.join(' ')
71
+ [name, version].compact.join(' ')
77
72
  end
78
73
  end
79
74
  end
@@ -48,58 +48,53 @@ module AgentOrange
48
48
  end
49
49
  end
50
50
 
51
- self.analysis
51
+ analysis
52
52
 
53
- self.platform = AgentOrange::Platform.new(user_agent)
53
+ self.platform = AgentOrange::Platform.new(user_agent)
54
54
  self.operating_system = AgentOrange::OperatingSystem.new(user_agent)
55
- self.engine = AgentOrange::Engine.new(user_agent)
55
+ self.engine = AgentOrange::Engine.new(user_agent)
56
56
  end
57
57
 
58
+ # @return [Device]
58
59
  def populate(content={})
59
- self.debug_raw_content(content)
60
+ debug_raw_content(content)
60
61
  AgentOrange.debug "", 2
61
62
 
62
- self.type = self.determine_type(DEVICES, content[:comment])
63
- self.bot = self.determine_type(BOTS, content[:comment]) == :bot
64
- if (self.bot && self.type == "other")
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 = self.type.to_s.capitalize
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 self.platform.name.downcase.include?(name.downcase)
78
+ return platform.name.downcase.include?(name.downcase)
84
79
  when Symbol
85
- return self.platform.name.downcase.include?(name.to_s.downcase)
80
+ return platform.name.downcase.include?(name.to_s.downcase)
86
81
  end
87
82
  else
88
- (self.type == :computer)
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? && !self.platform.name.nil?
89
+ if !name.nil? && !platform.name.nil?
95
90
  case name
96
91
  when String
97
- return self.platform.name.downcase.include?(name.downcase) || self.platform.version.downcase.include?(name.downcase)
92
+ return platform.name.downcase.include?(name.downcase) || platform.version.downcase.include?(name.downcase)
98
93
  when Symbol
99
- return self.platform.name.downcase.include?(name.to_s.downcase) || self.platform.version.to_s.downcase.include?(name.to_s.downcase)
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
- (self.type == :mobile)
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 self.name.downcase.include?(name.downcase)
106
+ return name.downcase.include?(name.downcase)
112
107
  when Symbol
113
- return self.name.downcase.include?(name.to_s.downcase)
108
+ return name.downcase.include?(name.to_s.downcase)
114
109
  end
115
110
  else
116
- self.bot
111
+ bot
117
112
  end
118
113
  end
119
114
 
120
115
  # @return [String]
121
116
  def to_s
122
- [self.name, self.version].compact.join(' ')
117
+ [name, version].compact.join(' ')
123
118
  end
124
119
  end
125
120
  end
@@ -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
- self.populate(content)
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
- self.populate(chosen_content)
45
+ populate(chosen_content)
47
46
  end
48
47
  end
49
48
 
50
- self.analysis
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
- self.debug_raw_content(content)
56
+ debug_raw_content(content)
56
57
  AgentOrange.debug "", 2
57
58
 
58
- self.type = self.determine_type(ENGINES, content[:name])
59
- self.name = ENGINES[self.type.to_sym]
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
- [self.name, self.version].compact.join(' ') || "Unknown"
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
- self.populate(chosen_content)
49
+ populate(chosen_content)
50
50
  end
51
51
  end
52
52
 
53
- self.analysis
53
+ analysis
54
54
  end
55
55
 
56
+ # @return [OperatingSystem]
56
57
  def populate(content={})
57
- self.debug_raw_content(content)
58
+ debug_raw_content(content)
58
59
  AgentOrange.debug "", 2
59
60
 
60
- self.type = self.determine_type(OPERATING_SYSTEMS, content[:name])
61
- self.name = OPERATING_SYSTEM_NAMES[self.type.to_sym]
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
- [self.name, self.version].compact.join(' ')
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
- self.populate(content)
41
+ populate(content)
42
42
  end
43
43
  end
44
44
 
45
- self.analysis
45
+ analysis
46
46
  end
47
47
 
48
+ # @return [Platform]
48
49
  def populate(content={})
49
- self.debug_raw_content(content)
50
+ debug_raw_content(content)
50
51
  AgentOrange.debug "", 2
51
52
 
52
- self.type = self.determine_type(PLATFORMS, content[:comment])
53
- self.name = PLATFORM_NAMES[self.type.to_sym]
53
+ self.type = determine_type(PLATFORMS, content[:comment])
54
+ self.name = PLATFORM_NAMES[type.to_sym]
54
55
 
55
- if self.type == :apple
56
- self.version = case self.determine_type(PLATFORM_VERSIONS, content[:comment])
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
- [self.name, self.version].compact.join(' ')
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
- self.parse(user_agent_string)
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(self.user_agent_string)
23
+ self.device = AgentOrange::Device.new(user_agent_string)
24
24
 
25
- AgentOrange.debug "Device = #{self.device}"
26
- AgentOrange.debug "Platform = #{self.device.platform}"
27
- AgentOrange.debug "OS = #{self.device.operating_system}"
28
- AgentOrange.debug "Engine = #{self.device.engine}"
29
- AgentOrange.debug "Browser = #{self.device.engine.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
- self.summary
31
+ summary
32
32
  end
33
33
 
34
34
  # @return [Boolean]
35
35
  def is_computer?(type=nil)
36
- self.device.is_computer?(type)
36
+ device.is_computer?(type)
37
37
  end
38
38
 
39
39
  # @return [Boolean]
40
40
  def is_mobile?(type=nil)
41
- self.device.is_mobile?(type)
41
+ device.is_mobile?(type)
42
42
  end
43
43
 
44
44
  # @return [Boolean]
45
45
  def is_bot?(type=nil)
46
- self.device.is_bot?(type)
46
+ device.is_bot?(type)
47
47
  end
48
48
 
49
49
  # @return [String]
50
50
  def to_s
51
- [self.device,
52
- self.device.platform,
53
- self.device.operating_system,
54
- self.device.engine,
55
- self.device.engine.browser
56
- ].compact.join(", ")
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 self.device && self.device.engine && self.device.engine.browser
62
- "User has a #{self.device} running #{self.device.engine.browser} (which is based on #{self.device.engine})."
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? #{self.is_computer?}"
72
- if self.is_computer?
73
- AgentOrange.debug " Is a Mac? #{self.is_computer? :mac}"
74
- AgentOrange.debug " Is a PC? #{self.is_computer? :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? #{self.is_mobile?}"
77
- if self.is_mobile?
78
- AgentOrange.debug " Is an iPhone? #{self.is_mobile? :iphone}"
79
- AgentOrange.debug " Is an iPad? #{self.is_mobile? :ipad}"
80
- AgentOrange.debug " Is an iPod? #{self.is_mobile? :ipod}"
81
- AgentOrange.debug " Is an Android? #{self.is_mobile? :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? #{self.is_bot?}"
84
+ AgentOrange.debug " Is bot? #{is_bot?}"
84
85
  AgentOrange.debug
85
86
  end
86
87
  end
@@ -1,5 +1,5 @@
1
1
  module AgentOrange
2
- VERSION = "0.1.5" # This is for the gem and does not conflict with the rest of the functionality
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 = nil
20
- self.minor = nil
21
- self.patch_level = nil
19
+ self.major = nil
20
+ self.minor = nil
21
+ self.patch_level = nil
22
22
  self.build_number = nil
23
23
 
24
- pieces = version_string.split('.')
24
+ pieces = version_string.split('.')
25
25
  pieces_count = pieces.count
26
26
 
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
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
- [self.major, self.minor, self.patch_level, self.build_number].compact.join('.')
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.5
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-05 00:00:00.000000000 Z
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: 3187188624662308564
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: 3187188624662308564
116
+ hash: 3541338043838672934
117
117
  requirements: []
118
118
  rubyforge_project: agent_orange
119
119
  rubygems_version: 1.8.24