agent_helpers 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62dd40d9ba486c340fc2dbfc15c70bfae2860af5
4
- data.tar.gz: 24e7182726b1c65f79ebd79503ce915de2c5f330
3
+ metadata.gz: 3554deaaaf3356dfb3929d7c7776930b0346304f
4
+ data.tar.gz: e2552702bc1a372a0f845d72653c5411375c737b
5
5
  SHA512:
6
- metadata.gz: d570d834d0b53c0e51e18789ff18d39246f0e65d2c3f5274476bde5d69337004437d1845bd5336f272c451b80c7a112cd20d170b50f755fc44f31461cc68c288
7
- data.tar.gz: 442e83190874bbfc5ff157d7c212464e9ad364eee22b9378407d04b4597f2ab80e1051a8588868a32b2e969ffd25f013f5e7022df9b775c6e046cdb2c32ed442
6
+ metadata.gz: 7fb3a8bfae9a91a989d783e1de0ec07e9b96ada9bca8487e96c95d266e782439a4e223f620604b0d60113b0a67e49cc4f84749c64d2352e1f196ce96f156b3fa
7
+ data.tar.gz: 055f12bfd5dcb8031f35fc40493ffccdb6242fca6f424867ff8042428dbd86d04b80c99697939a6ed1148936e3e0f492e55c5f3a749f4bc57b4e01fbc11132f0
@@ -3,55 +3,83 @@ module AgentHelpers::DetectorHelper
3
3
  if !controller.instance_variable_get(:@agent_helpers_detector)
4
4
  controller.instance_variable_set(:@agent_helpers_detector, ::AgentHelpers::Detector.new(:request => request))
5
5
  end
6
-
6
+
7
7
  return controller.instance_variable_get(:@agent_helpers_detector)
8
8
  end
9
-
9
+
10
10
  def agent_robot?
11
11
  agent_detector.browser == :bot
12
12
  end
13
-
13
+
14
14
  def agent_human?
15
15
  agent_detector.browser != :bot && agent_detector.browser != :unknown
16
16
  end
17
-
17
+
18
18
  def agent_browser
19
19
  agent_detector.browser
20
20
  end
21
-
21
+
22
22
  def agent_title
23
23
  agent_detector.title
24
24
  end
25
-
25
+
26
26
  def agent_version
27
27
  agent_detector.version
28
28
  end
29
-
29
+
30
+ def agent_version_major
31
+ agent_detector.version_major
32
+ end
33
+
30
34
  def agent_device
31
35
  agent_detector.device
32
36
  end
33
-
37
+
38
+ def agent_os
39
+ agent_detector.os
40
+ end
41
+
34
42
  def agent_chrome?
35
43
  agent_detector.browser == :chrome
36
44
  end
37
-
45
+
38
46
  def agent_firefox?
39
47
  agent_detector.browser == :firefox
40
48
  end
41
-
49
+
42
50
  def agent_ie?
43
51
  agent_detector.browser == :ie
44
52
  end
45
-
53
+
46
54
  def agent_safari?
47
55
  agent_detector.browser == :safari
48
56
  end
49
-
57
+
50
58
  def agent_opera?
51
59
  agent_detector.browser == :opera
52
60
  end
53
-
61
+
54
62
  def agent_mobile?
55
- agent_detector.device == :android || agent_detector.device == :ipad || agent_detector.device == :iphone
63
+ agent_detector.os == :android || agent_detector.os == :ios
64
+ end
65
+
66
+ def agent_windows?
67
+ agent_detector.os == :windows
68
+ end
69
+
70
+ def agent_linux?
71
+ agent_detector.os == :linux
72
+ end
73
+
74
+ def agent_osx?
75
+ agent_detector.os == :osx
76
+ end
77
+
78
+ def agent_os_version
79
+ agent_detector.os_version
80
+ end
81
+
82
+ def agent_os_title
83
+ agent_detector.os_title
56
84
  end
57
85
  end
@@ -1,130 +1,168 @@
1
1
  class AgentHelpers::Detector
2
- attr_reader :browser, :title, :version, :device
3
-
2
+ attr_reader :browser, :title, :version, :device, :os, :os_title, :os_version
3
+
4
+ WINDOWS_VERSIONS = {
5
+ "NT 5.0" => "Windows 2000",
6
+ "NT 5.01" => "Windows 2000 SP1",
7
+ "NT 5.1" => "Windows XP",
8
+ "NT 5.2" => "Windows XP 64bit",
9
+ "NT 6.0" => "Windows Vista",
10
+ "NT 6.1" => "Windows 7",
11
+ "NT 6.2" => "Windows 8",
12
+ "NT 6.3" => "Windows 8.1"
13
+ }
14
+
15
+ MAC_OSX_VERSIONS = {
16
+ "10.0" => "Cheetah",
17
+ "10.1" => "Puma",
18
+ "10.2" => "Juguar",
19
+ "10.3" => "Panther",
20
+ "10.4" => "Tiger",
21
+ "10.5" => "Leopard",
22
+ "10.6" => "Snow Leopard",
23
+ "10.7" => "Lion",
24
+ "10.8" => "Mountain Lion",
25
+ "10.9" => "Mavericks",
26
+ "10.10" => "Yosemite"
27
+ }
28
+
4
29
  def initialize(args)
5
30
  @request = args[:request]
6
31
  raise "No request was given." unless @request
7
32
  @env = @request.env
8
33
  @user_agent = @request.user_agent.to_s.downcase.strip
9
34
  raise "No user agent was given." if @user_agent.empty?
10
- parse
35
+
36
+ browsers = [:chrome, :ie, :firefox, :opera, :safari]
37
+ browsers.each do |browser|
38
+ __send__("parse_#{browser}") unless @browser
39
+ end
40
+
41
+ parse_bots unless @browser
42
+ parse_device
43
+ end
44
+
45
+ def version_major
46
+ if match = version.to_s.match(/^(\d+)/)
47
+ return match[1].to_i
48
+ else
49
+ return nil
50
+ end
11
51
  end
12
-
52
+
13
53
  private
14
-
15
- def parse
16
- if match = @user_agent.match(/chrome\/(\d+\.\d+)/)
54
+
55
+ def parse_chrome
56
+ if match = @user_agent.match(/chrome\/([\d+\.]+)/)
17
57
  @browser = :chrome
18
58
  @title = "Google Chrome"
19
59
  @version = match[1]
20
- elsif match = @user_agent.match(/firefox\/(\d+\.\d+)/)
21
- @browser = :firefox
22
- @title = "Mozilla Firefox"
23
- @version = match[1]
24
- elsif @env["HTTP_ACCEPT"] == "*/*" && @env["HTTP_ACCEPT_LANGUAGE"] == "zh-cn,zh-hk,zh-tw,en-us" && @env["HTTP_USER_AGENT"] == "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" && (@env["REMOTE_ADDR"][0,10] == "114.80.93." || @env["HTTP_X_FORWARDED_FOR"][0, 10] == "114.80.93.")
25
- @browser = :bot
26
- @title = "bot"
27
- @version = "(unknown annoying bot from China)"
28
- elsif match = @user_agent.match(/msie\s*(\d+\.\d+)/)
60
+ end
61
+ end
62
+
63
+ def parse_ie
64
+ if match = @user_agent.match(/msie\s*([\d+\.]+)/)
29
65
  @browser = :ie
30
66
  @title = "Microsoft Internet Explorer"
31
67
  @version = match[1]
32
- elsif match = @user_agent.match(/opera\/([\d+\.]+)/)
68
+ end
69
+ end
70
+
71
+ def parse_firefox
72
+ if match = @user_agent.match(/firefox\/([\d+\.]+)/)
73
+ @browser = :firefox
74
+ @title = "Mozilla Firefox"
75
+ @version = match[1]
76
+ end
77
+ end
78
+
79
+ def parse_opera
80
+ if match = @user_agent.match(/opera\/([\d+\.]+)/)
33
81
  @browser = :opera
34
82
  @title = "Opera"
35
83
  @version = match[1]
36
- elsif match = @user_agent.match(/wget\/([\d+\.]+)/)
37
- @browser = :bot
38
- @title = "Bot"
39
- @version = "Wget #{match[1]}"
40
- elsif @user_agent.include?("baiduspider")
41
- @browser = :bot
42
- @title = "Bot"
43
- @version = "Baiduspider"
44
- elsif match = @user_agent.match(/googlebot\/([\d\.]+)/)
45
- @browser = :bot
46
- @title = "Googlebot"
47
- @version = match[1]
48
- elsif @user_agent.include?("gidbot")
49
- @browser = :bot
50
- @title = "Bot"
51
- @version = "GIDBot"
52
- elsif match = @user_agent.match(/android\s+([\d\.]+)/)
53
- @browser = :android
54
- @title = "Android"
55
- @version = match[1]
56
- elsif match = @user_agent.match(/safari\/(\d+)/)
84
+ end
85
+ end
86
+
87
+ def parse_safari
88
+ if match = @user_agent.match(/safari\/([\d+\.]+)/)
57
89
  @browser = :safari
58
90
  @title = "Safari"
59
91
  @version = match[1]
60
- elsif @user_agent.include?("bingbot")
61
- @browser = :bot
62
- @title = "Bot"
63
- @version = "Bingbot"
64
- elsif @user_agent.include?("yahoo! slurp")
65
- @browser = :bot
66
- @title = "Bot"
67
- @version = "Yahoo! Slurp"
68
- elsif @user_agent.include?("hostharvest")
69
- @browser = :bot
70
- @title = "Bot"
71
- @version = "HostHarvest"
72
- elsif @user_agent.include?("exabot")
73
- @browser = :bot
74
- @title = "Bot"
75
- @version = "Exabot"
76
- elsif @user_agent.include?("dotbot")
77
- @browser = :bot
78
- @title = "Bot"
79
- @version = "DotBot"
80
- elsif @user_agent.include?("msnbot")
81
- @browser = :bot
82
- @title = "Bot"
83
- @version = "MSN bot"
84
- elsif @user_agent.include?("yandexbot")
85
- @browser = :bot
86
- @title = "Bot"
87
- @version = "Yandex Bot"
88
- elsif @user_agent.include?("mj12bot")
89
- @browser = :bot
90
- @title = "Bot"
91
- @version = "Majestic12 Bot"
92
- elsif @user_agent.include?("facebookexternalhit")
93
- @browser = :bot
94
- @title = "Bot"
95
- @version = "Facebook Externalhit"
96
- elsif @user_agent.include?("sitebot")
97
- @browser = :bot
98
- @title = "Bot"
99
- @version = "SiteBot"
92
+ end
93
+ end
94
+
95
+ def parse_apps
96
+ if match = @user_agent.match(/wget\/([\d+\.]+)/)
97
+ @browser = :app
98
+ @title = "Wget"
99
+ @version = match[1]
100
100
  elsif match = @user_agent.match(/java\/([\d\.]+)/)
101
- @browser = :bot
101
+ @browser = :app
102
102
  @title = "Java"
103
103
  @version = match[1]
104
- elsif match = @user_agent.match(/ezooms\/([\d\.]+)/)
105
- @browser = :bot
106
- @title = "Ezooms"
107
- @version = match[1]
108
- elsif match = @user_agent.match(/ahrefsbot\/([\d\.]+)/)
104
+ end
105
+ end
106
+
107
+ def parse_bots
108
+ regex_list = [/(googlebot)\/([\d\.]+)/, /(ezooms)\/([\d\.]+)/, /(ahrefsbot)\/([\d\.]+)/, /(baiduspider)/, /(gidbot)/, /(yahoo! slurp)/,
109
+ /(hostharvest)/, /(exabot)/, /(dotbot)/, /(msnbot)/, /(yandexbot)/, /(sitebot)/, /(sosospider)/, /(bingbot)/]
110
+ parse_regex_list(regex_list, :bot)
111
+ return if @browser
112
+
113
+ if @env["HTTP_ACCEPT"] == "*/*" && @env["HTTP_ACCEPT_LANGUAGE"] == "zh-cn,zh-hk,zh-tw,en-us" && @env["HTTP_USER_AGENT"] == "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" && (@env["REMOTE_ADDR"][0,10] == "114.80.93." || @env["HTTP_X_FORWARDED_FOR"][0, 10] == "114.80.93.")
114
+ @browser = :bot
115
+ @title = "(unknown annoying bot from China)"
116
+ elsif @user_agent.include?("mj12bot")
109
117
  @browser = :bot
110
- @title = "AhrefsBot"
111
- @version = match[1]
112
- elsif @user_agent.include?("sosospider")
118
+ @title = "Majestic12 Bot"
119
+ elsif @user_agent.include?("facebookexternalhit")
113
120
  @browser = :bot
114
- @title = "Bot"
115
- @version = "Sosospider"
116
- else
117
- @browser = :unknown
118
- @title = "(unknown browser)"
119
- @version = "(unknown version)"
121
+ @title = "Facebook Externalhit"
120
122
  end
121
-
123
+ end
124
+
125
+ def parse_device
122
126
  if @user_agent.include?("ipad")
123
127
  @device = :ipad
128
+ @os = :ios
124
129
  elsif @user_agent.include?("iphone")
125
130
  @device = :iphone
131
+ @os = :ios
126
132
  elsif @user_agent.include?("android")
127
- @device = :android
133
+ @os = :android
134
+ elsif match = @user_agent.match(/windows (nt |)([\d\.]+)/)
135
+ @os = :windows
136
+ @os_version = match[2]
137
+ @os_version = "NT #{@os_version}" if match[1] == "nt "
138
+ @os_title = WINDOWS_VERSIONS[@os_version]
139
+ elsif @user_agent.include?("linux")
140
+ @os = :linux
141
+ @os_title = "Linux"
142
+ elsif match = @user_agent.match(/mac os x ([\d_]+)/)
143
+ @os = :osx
144
+ @os_version = match[1].gsub("_", ".")
145
+ detect_osx_title
146
+ end
147
+ end
148
+
149
+ def detect_osx_title
150
+ major_version = @os_version.match(/^\d+\.\d+/)[0]
151
+ @os_title = "Mac OSX"
152
+
153
+ if title = MAC_OSX_VERSIONS[major_version]
154
+ @os_title << " #{title}"
155
+ end
156
+ end
157
+
158
+ def parse_regex_list(list, browser)
159
+ list.each do |regex|
160
+ if match = @user_agent.match(regex)
161
+ @browser = browser
162
+ @title = match[1].to_s.capitalize
163
+ @version = match[2] if match[2]
164
+ break
165
+ end
128
166
  end
129
167
  end
130
168
  end
@@ -1,3 +1,3 @@
1
1
  module AgentHelpers
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,97 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agent_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-23 00:00:00.000000000 Z
11
+ date: 2014-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: string-cases
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec-rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: forgery
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: factory_girl_rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  description: Inspects the user agent for you and allows you to take action based on
@@ -102,21 +116,20 @@ executables: []
102
116
  extensions: []
103
117
  extra_rdoc_files: []
104
118
  files:
105
- - app/helpers/agent_helpers/application_helper.rb
106
- - app/helpers/agent_helpers/detector_helper.rb
119
+ - MIT-LICENSE
120
+ - Rakefile
107
121
  - app/assets/javascripts/agent_helpers/application.js
108
122
  - app/assets/stylesheets/agent_helpers/application.css
109
123
  - app/controllers/agent_helpers/application_controller.rb
124
+ - app/helpers/agent_helpers/application_helper.rb
125
+ - app/helpers/agent_helpers/detector_helper.rb
110
126
  - app/views/layouts/agent_helpers/application.html.erb
111
127
  - config/routes.rb
112
128
  - lib/agent_helpers.rb
129
+ - lib/agent_helpers/detector.rb
113
130
  - lib/agent_helpers/engine.rb
114
131
  - lib/agent_helpers/version.rb
115
- - lib/agent_helpers/detector.rb
116
132
  - lib/tasks/agent_helpers_tasks.rake
117
- - MIT-LICENSE
118
- - Rakefile
119
- - README.rdoc
120
133
  homepage: https://www.github.com/kaspernj/agent_helpers
121
134
  licenses: []
122
135
  metadata: {}
@@ -126,17 +139,17 @@ require_paths:
126
139
  - lib
127
140
  required_ruby_version: !ruby/object:Gem::Requirement
128
141
  requirements:
129
- - - '>='
142
+ - - ">="
130
143
  - !ruby/object:Gem::Version
131
144
  version: '0'
132
145
  required_rubygems_version: !ruby/object:Gem::Requirement
133
146
  requirements:
134
- - - '>='
147
+ - - ">="
135
148
  - !ruby/object:Gem::Version
136
149
  version: '0'
137
150
  requirements: []
138
151
  rubyforge_project:
139
- rubygems_version: 2.0.7
152
+ rubygems_version: 2.4.0
140
153
  signing_key:
141
154
  specification_version: 4
142
155
  summary: Easily detect robots, browser, versions and more with convenient helpers.
data/README.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = AgentHelpers
2
-
3
- This project rocks and uses MIT-LICENSE.