agent_user 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ # encoding: UTF-8
2
+
3
+ source 'https://rubygems.org'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Marc Lipovsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # AgentUser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'agent_user'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install agent_user
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "agent_user"
5
+ gem.version = "1.0.1"
6
+ gem.authors = ["Marc Lipovsky"]
7
+ gem.email = ["marclipovsky@gmail.com"]
8
+ gem.summary = "A better user agent parser."
9
+ gem.description = "."
10
+
11
+ gem.files = `git ls-files`.split($/)
12
+ gem.require_paths = ["lib"]
13
+ end
data/lib/agent_user.rb ADDED
@@ -0,0 +1,115 @@
1
+ # encoding: UTF-8
2
+
3
+ module AgentUser
4
+ class Parser
5
+
6
+ def self.parse(str)
7
+ self.new(str)
8
+ end
9
+
10
+ def initialize (str)
11
+ # Every device/browser/os name with version
12
+ all_names_and_versions_arr = str.scan %r`((?:[a-zA-Z]+\s?)+)[/ ]([0-9._]+)`
13
+
14
+ self.get_operating_system_data(str)
15
+ self.get_browser_data(all_names_and_versions_arr)
16
+ self.get_mobile_data(all_names_and_versions_arr)
17
+
18
+ return true
19
+ end
20
+
21
+
22
+
23
+ # Parse out operating system name and version
24
+ def get_browser_data(all_names_and_versions_arr)
25
+ # Filter our browser name and version based on user agent browser names
26
+ names_and_versions_without_os_info = all_names_and_versions_arr.reject {|e| e == @os_name_and_version.last}
27
+ browser_name_and_version = names_and_versions_without_os_info.select do |e|
28
+ e.to_s.include?('Safari') ||
29
+ e.to_s.include?('Mobile Safari') ||
30
+ e.to_s.include?('Firefox') ||
31
+ e.to_s.include?('MSIE') ||
32
+ e.to_s.include?('IEMobile') ||
33
+ e.to_s.include?('Opera') ||
34
+ e.to_s.include?('Internet Explorer') ||
35
+ e.to_s.include?('Chrome')
36
+ end.first
37
+
38
+ @browser_name, @browser_version = browser_name_and_version
39
+
40
+
41
+ # The version number for Safari is located in another place in the user agent
42
+ # Below will solve that by finding that version number
43
+ version_arr = names_and_versions_without_os_info.select {|e| e[0] == "Version"}.first
44
+ if @browser_name == "Safari" && version_arr
45
+ @browser_version = version_arr[1]
46
+ end
47
+ end
48
+
49
+ # Parse out operating system name and version
50
+ def get_operating_system_data (str)
51
+ @os_name_and_version = str.scan %r`((?:\w+\s?)+)\s([0-9._]+)`
52
+ @operating_system_name, @operating_system_version = @os_name_and_version.last
53
+ end
54
+
55
+ # Parse out mobile device name and version
56
+ def get_mobile_data(all_names_and_versions_arr)
57
+ # Mobile device name and version
58
+ mobile_device_name_and_version = all_names_and_versions_arr.select do |e|
59
+ e.to_s.include?('Android') || # Android
60
+ e.to_s.include?('Blackberry') || # Blackberry
61
+ e.to_s.include?('iPhone OS') || # iPhone
62
+ e.to_s.include?('CPU OS') || # iPad
63
+ e.to_s.include?('Windows Phone OS') # Windows Phone
64
+ end.first
65
+
66
+ @mobile_device_name, @mobile_device_version = mobile_device_name_and_version
67
+ end
68
+
69
+
70
+
71
+ def mobile?
72
+ # Force true or false if the based on the device name
73
+ !!@mobile_device_name
74
+ end
75
+
76
+
77
+
78
+ def mobile_device_name
79
+ # Change name from user agent to more readable iOS device
80
+ return 'iPad' if @mobile_device_name == 'CPU OS'
81
+ return 'iPhone' if @mobile_device_name == 'CPU iPhone OS'
82
+
83
+ @mobile_device_name
84
+ end
85
+
86
+ def mobile_device_version
87
+ # Replace _ in some version numbers to .
88
+ @mobile_device_version.gsub(?_,?.) if @mobile_device_version
89
+ end
90
+
91
+ def operating_system_name
92
+ # Change name from user agent to iOS if from an iPad or iPhone
93
+ return 'iOS' if ['CPU OS','CPU iPhone OS'].include? @operating_system_name
94
+
95
+ @operating_system_name || 'Other'
96
+ end
97
+
98
+ def operating_system_version
99
+ # Replace _ in some version numbers to .
100
+ @operating_system_version.gsub(?_,?.) if @operating_system_version
101
+ end
102
+
103
+ def browser_name
104
+ return 'Internet Explorer' if @browser_name == 'MSIE'
105
+
106
+ @browser_name || 'Other'
107
+ end
108
+
109
+ def browser_version
110
+ # Replace _ in some version numbers to .
111
+ @browser_version.gsub(?_,?.) if @browser_version
112
+ end
113
+
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agent_user
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marc Lipovsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: .
15
+ email:
16
+ - marclipovsky@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - agent_user.gemspec
26
+ - lib/agent_user.rb
27
+ homepage:
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.23
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: A better user agent parser.
51
+ test_files: []
52
+ has_rdoc: