lightweight_user_agent_parser 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ require_relative '../spec_helper'
2
+ require 'yaml'
3
+
4
+ start_time= Time.now
5
+ TEST_DATA.each do |user_agent_string|
6
+
7
+ agent = LightweightUserAgentParser.new(user_agent_string)
8
+
9
+ if agent.mobile? == false && LightweightUserAgentParser::MOBILE_DEVICES.include?(agent.platform)
10
+ p agent.to_s
11
+ $stdin.gets
12
+ end
13
+
14
+ end
15
+ puts Time.now - start_time
16
+
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe LightweightUserAgentParser do
4
+
5
+ TEST_CASES = [
6
+ {
7
+ ua_str: 'UCWEB/2.0 (Linux; U; Adr 4.2.1; zh-CN; Lenovo A3000) U2/1.0.0 UCBrowser/9.8.5.442 U2/1.0.0 Mobile',
8
+ mobile: true,
9
+ platform: 'Android'
10
+ },
11
+ {
12
+ ua_str: 'Opera/9.80 (Android; Opera Mini/6.1.25375/35.3730; U; ru) Presto/2.8.119 Version/11.10',
13
+ mobile: true,
14
+ platform: 'Android'
15
+ },
16
+ {
17
+ ua_str: 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_2_1 like Mac OS X; pt-br) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5',
18
+ mobile: true,
19
+ platform: 'iPod'
20
+ },
21
+ {
22
+ ua_str: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A405 Safari/600.1.4 BMID/E67A2870D2',
23
+ mobile: true,
24
+ platform: 'iPhone'
25
+ },
26
+ {
27
+ ua_str: 'Mozilla/5.0 (iPad; CPU OS 7_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) GSA/3.2.0.25255 Mobile/11D167 Safari/8536.25',
28
+ mobile: true,
29
+ platform: 'iPad'
30
+ },
31
+ {
32
+ ua_str: 'BlackBerry8110/4.5.0.180 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102',
33
+ mobile: true,
34
+ platform: 'BlackBerry'
35
+ },
36
+ {
37
+ ua_str: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; CHERRY; MOBILE Alpha Style)',
38
+ mobile: true,
39
+ platform: 'WindowsMobile'
40
+ },
41
+ {
42
+ ua_str: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; AlexaToolbar/amzni-3.0; BRI/2; Microsoft Outlook 15.0.4605; ms-office; MSOffice 15)',
43
+ mobile: false,
44
+ platform: 'Windows'
45
+ },
46
+ {
47
+ ua_str: 'Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14069/35.5003; U; uk) Presto/2.8.119 Version/11.10',
48
+ mobile: true,
49
+ platform: 'Symbian'
50
+ },
51
+ {
52
+ ua_str: 'Mozilla/5.0 (X11; FreeBSD i386) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19',
53
+ mobile: false,
54
+ platform: 'FreeBSD'
55
+ },
56
+ {
57
+ ua_str: 'Mozilla/5.0 (X11; U; Linux i686; pt-BR; rv:1.9.0.19) Gecko/2010040116 Ubuntu/9.04 (jaunty) Firefox/3.0.19',
58
+ mobile: false,
59
+ platform: 'Linux'
60
+ },
61
+ {
62
+ ua_str: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:29.0) Gecko/20100101 Firefox/29.0 AlexaToolbar/p1VF2_Uf-2.1',
63
+ mobile: false,
64
+ platform: 'Macintosh'
65
+ },
66
+ {
67
+ ua_str: 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S5330/1.0; U; Bada/1.0; pt-br) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WQVGA SMM-MMS/1.2.0 OPN-B',
68
+ mobile: true,
69
+ platform: 'Bada'
70
+ },
71
+ {
72
+ ua_str: 'Firefox/33.1 (x86 de); anonymized by Abelssoft 2048434593',
73
+ mobile: false,
74
+ platform: 'anonymized'
75
+ }
76
+ ]
77
+
78
+ let(:user_agent_string) { TEST_DATA[rand(0..(TEST_DATA.length-1))].dup }
79
+
80
+ describe '#initialize' do
81
+ subject { described_class.new(*args) }
82
+
83
+ context 'no argument string passed' do
84
+ let(:args) { [] }
85
+
86
+ it { expect { subject }.to raise_error(ArgumentError) }
87
+ end
88
+
89
+ context 'with user agent string passed as argument' do
90
+ let(:args) { [user_agent_string] }
91
+
92
+ it { is_expected.to be_instance_of described_class }
93
+ end
94
+
95
+ end
96
+
97
+ describe '#platform' do
98
+ subject { described_class.new(ua_str).platform.to_s }
99
+ TEST_CASES.each do |meta|
100
+
101
+ context "when platform is #{meta[:platform]}" do
102
+ let(:ua_str) { meta[:ua_str] }
103
+
104
+ it { is_expected.to eql meta[:platform] }
105
+ end
106
+
107
+ end
108
+ end
109
+
110
+ describe '#is_mobile?' do
111
+ subject { described_class.new(ua_str).mobile? }
112
+ TEST_CASES.each do |meta|
113
+
114
+ context "when platform is #{meta[:platform]}" do
115
+ let(:ua_str) { meta[:ua_str] }
116
+
117
+ it "then the mobile state should be #{meta[:mobile]}" do
118
+ is_expected.to eq meta[:mobile]
119
+ end
120
+ end
121
+
122
+ end
123
+ end
124
+
125
+ end
@@ -0,0 +1,95 @@
1
+ require 'rspec'
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
5
+ # file to always be loaded, without a need to explicitly require it in any files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need it.
13
+ #
14
+ # The `.rspec` file also contains a few flags that are not defaults but that
15
+ # users commonly want.
16
+ #
17
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # The settings below are suggested to provide a good initial experience
43
+ # with RSpec, but feel free to customize to your heart's content.
44
+ =begin
45
+ # These two settings work together to allow you to limit a spec run
46
+ # to individual examples or groups you care about by tagging them with
47
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
48
+ # get run.
49
+ config.filter_run :focus
50
+ config.run_all_when_everything_filtered = true
51
+
52
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
53
+ # For more details, see:
54
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
55
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
56
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
57
+ config.disable_monkey_patching!
58
+
59
+ # This setting enables warnings. It's recommended, but in some cases may
60
+ # be too noisy due to issues in dependencies.
61
+ config.warnings = true
62
+
63
+ # Many RSpec users commonly either run the entire suite or an individual
64
+ # file, and it's useful to allow more verbose output when running an
65
+ # individual spec file.
66
+ if config.files_to_run.one?
67
+ # Use the documentation formatter for detailed output,
68
+ # unless a formatter has already been configured
69
+ # (e.g. via a command-line flag).
70
+ config.default_formatter = 'doc'
71
+ end
72
+
73
+ # Print the 10 slowest examples and example groups at the
74
+ # end of the spec run, to help surface which specs are running
75
+ # particularly slow.
76
+ config.profile_examples = 10
77
+
78
+ # Run specs in random order to surface order dependencies. If you find an
79
+ # order dependency and want to debug it, you can fix the order by providing
80
+ # the seed, which is printed after each run.
81
+ # --seed 1234
82
+ config.order = :random
83
+
84
+ # Seed global randomization in this process using the `--seed` CLI option.
85
+ # Setting this allows you to use `--seed` to deterministically reproduce
86
+ # test failures related to randomization by passing the same `--seed` value
87
+ # as the one that triggered the failure.
88
+ Kernel.srand config.seed
89
+ =end
90
+ end
91
+
92
+ lib = File.expand_path('../lib', __FILE__)
93
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
94
+ require 'lightweight_user_agent_parser'
95
+ TEST_DATA = File.read(File.join(File.dirname(__FILE__),'fixtures','10K.txt')).split("\n")
data/spike/play.rb ADDED
@@ -0,0 +1,12 @@
1
+ UAS = File.read('../spec/data/10K.txt').split("\n")
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.join('..','lib'))
4
+ require 'lightweight_user_agent_parser'
5
+
6
+ UAS.each do |user_agent_string|
7
+
8
+ ua = LightweightUserAgentParser.new user_agent_string
9
+ p ua.to_hash
10
+
11
+ exit
12
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightweight_user_agent_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Emarsys Technologies
8
+ - Peter Kozma
9
+ - Adam Luzsi
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2016-02-03 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '3'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '3'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ description: Very lightweight user agent parser with the aim of detecting mobile devices
58
+ and identifying main device platforms.
59
+ email:
60
+ - peter.kozma@emarsys.com
61
+ - aluzsi@emarsys.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - ".gitignore"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - lib/lightweight_user_agent_parser.rb
74
+ - lib/lightweight_user_agent_parser/content_analyzer.rb
75
+ - lib/lightweight_user_agent_parser/mobile.txt
76
+ - lib/lightweight_user_agent_parser/regexp.rb
77
+ - lib/lightweight_user_agent_parser/version.rb
78
+ - lightweight-user-agent-parser.gemspec
79
+ - spec/fixtures/10K.txt
80
+ - spec/fixtures/test.rb
81
+ - spec/lightweight_user_agent_parser_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spike/play.rb
84
+ homepage: https://github.com/emartech/lightweight-useragent-parser
85
+ licenses:
86
+ - MIT License
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.9.3
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.8
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Very lightweight user agent parser with the aim of detecting mobile devices
108
+ and identifying main device platforms.
109
+ test_files:
110
+ - spec/fixtures/10K.txt
111
+ - spec/fixtures/test.rb
112
+ - spec/lightweight_user_agent_parser_spec.rb
113
+ - spec/spec_helper.rb