lightweight_user_agent_parser 1.3.1 → 1.4.0

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: 04a55b38d3ecf70c795febaa61b68981bc1888b5
4
- data.tar.gz: 9b13ef73e36f46ad8de140375f87b18a1c36dc9e
3
+ metadata.gz: bcb44b3bbeb4da6e7c0de51a266c92de697775b1
4
+ data.tar.gz: 794051f4544389286b69c7c3a3f6c3c61e9f4d3c
5
5
  SHA512:
6
- metadata.gz: ad069fbad7fb6c497930c71b397c6248bb692f3883d67095396d358b4149139be6de207507a22ad816d642e188f49798ccc080ef6fefdb9d5ac015d60f6fd8aa
7
- data.tar.gz: 2dab9275d52f62360b5af65e95ef2e15cb3ca38c8f7e0f4736495ffd7fe341186567d794b52860c54fa57ee4987fb670798ce87efb17fb575391997d4101ca1d
6
+ metadata.gz: 93b2df1cbb426b23c16f9e673c8cb15a1b1218bc036d691027ff4daf0273b4abcfeedb40f3a50eacfb32f5b4ad372a00726fb9d72a956d4e672ac01e85c9e847
7
+ data.tar.gz: f70ab3a2f275fa8f30bfb8449c9c5554c4f58e58bd1f66429f304c72bdf16e7fd8f1e766b2fcafb25ca7c3c283d6baac8991134201eb509629646a0d4d11abfd
@@ -1,3 +1,5 @@
1
+ require 'digest/md5'
2
+
1
3
  require 'lightweight_user_agent_parser/version'
2
4
  require 'lightweight_user_agent_parser/regexp'
3
5
  require 'lightweight_user_agent_parser/content_analyzer'
@@ -16,7 +18,7 @@ class LightweightUserAgentParser
16
18
  end
17
19
 
18
20
  def platform
19
- @platform ||= -> {
21
+ @platform ||= lambda {
20
22
 
21
23
  DEVICES.each do |name, regexp|
22
24
  if user_agent_string =~ regexp[:strict]
@@ -41,17 +43,32 @@ class LightweightUserAgentParser
41
43
 
42
44
  (mobile or not desktop) and not empty?
43
45
  end
44
-
45
46
  alias mobile? is_mobile?
46
47
 
47
48
  def anonymized?
48
49
  !!(user_agent_string =~ ANONYMIZED_REGEXP)
49
50
  end
50
51
 
52
+ def md5
53
+ Digest::MD5.hexdigest(user_agent_string)
54
+ end
55
+
56
+ def to_hash
57
+ (TO_HASH_METHODS).reduce({}) do |memory, method_name|
58
+ hash_key = method_name.to_s.sub(/\?$/, '').to_sym
59
+
60
+ memory.merge!(hash_key => self.public_send(method_name))
61
+
62
+ memory
63
+ end
64
+ end
65
+
51
66
  protected
52
67
 
53
68
  def empty?
54
69
  user_agent_string.empty?
55
70
  end
56
71
 
72
+ TO_HASH_METHODS = self.public_instance_methods(false)-[:to_hash, :user_agent_string, :to_s, :is_mobile?]
73
+
57
74
  end
@@ -17,12 +17,6 @@ class LightweightUserAgentParser
17
17
  inner_content_elements && inner_content_elements[2]
18
18
  end
19
19
 
20
- def to_hash
21
- (ContentAnalyzer.public_instance_methods(false)-[:to_hash]).reduce({}) do |memory,method_name|
22
- memory.merge!(method_name => self.public_send(method_name));memory
23
- end
24
- end
25
-
26
20
  protected
27
21
 
28
22
  def inner_content_elements
@@ -1,3 +1,3 @@
1
1
  class LightweightUserAgentParser
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
@@ -114,7 +114,7 @@ RSpec.describe LightweightUserAgentParser do
114
114
  end
115
115
 
116
116
  describe '#is_mobile?' do
117
- subject { described_class.new(ua_str).mobile? }
117
+ subject { described_class.new(ua_str).is_mobile? }
118
118
  TEST_CASES.each do |meta|
119
119
 
120
120
  context "when platform is #{meta[:platform]}" do
@@ -132,4 +132,37 @@ RSpec.describe LightweightUserAgentParser do
132
132
  end
133
133
  end
134
134
 
135
+ describe '#to_hash' do
136
+ subject { described_class.new(user_agent_string).to_hash }
137
+
138
+ TEST_CASES.each do |meta|
139
+
140
+ context "when user agent string is: #{meta[:ua_str].inspect}" do
141
+ let(:user_agent_string) { meta[:ua_str] }
142
+
143
+ it 'should parse platform and mobile state' do
144
+
145
+ expected_hash = {
146
+ mobile: meta[:mobile],
147
+ platform: meta[:platform].to_sym,
148
+ md5: Digest::MD5.hexdigest(meta[:ua_str]),
149
+ anonymized: meta[:ua_str].include?('anonymized') ? true : false
150
+ }
151
+
152
+ is_expected.to eq expected_hash
153
+
154
+ end
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ describe '#md5' do
162
+ subject{ described_class.new(user_agent_string).md5}
163
+ let(:user_agent_string){'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'}
164
+
165
+ it { is_expected.to eq '50e330f013751df448e496bc19def744' }
166
+ end
167
+
135
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightweight_user_agent_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emarsys Technologies