browscapper 0.1.0 → 0.2.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: ede4118b2f3bcec3b30148e6bac3265215ca7c31
4
- data.tar.gz: aa5ba1d31e8fae077eb4dd933f21e96d71d6ebb3
3
+ metadata.gz: dd9c677e74cca24388abf53368c52473a8ff239a
4
+ data.tar.gz: 1c10c846a8ce23085b7479e8ac6e7ea5139e0a7a
5
5
  SHA512:
6
- metadata.gz: d943570726015c59daf03b4fb53e3addfe266350bf4802827eee56bf01f1df472c33bc9b831fea74bbb6b65eba03e70df30292989f700f56ceafc683e07b6252
7
- data.tar.gz: ceece896924cd926fd69aa2943060d421374728688e85a91a250786ce7b4939171dd8008c4d67621c0e657af3d778574e753a7b071c99fdb853ff7a543127211
6
+ metadata.gz: 5ea30a48bf5cdbb87aa52182b46dd73278c82c03dec30931ea19d6bdf5b61b8210c5814414882ed211da3e11d7d709a795d496e111a80927fbb39ae2b3d0f3a1
7
+ data.tar.gz: 847ab8bf9659a4499bba78fd4f8db8a8f5d1c912ef9ed2172aae6ca6d7283d17467ac622db338bbec5cd698754b2a800c132a48bceb48e2f37b74a61accc2031
@@ -91,6 +91,8 @@ module Browscapper
91
91
  end
92
92
 
93
93
  def ==(other)
94
+ return false unless other.is_a?(self.class)
95
+
94
96
  ACCESSORS.each do |accessor|
95
97
  return false if self.send(accessor) != other.send(accessor)
96
98
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: BINARY
2
2
 
3
3
  module Browscapper
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
 
data/lib/browscapper.rb CHANGED
@@ -36,21 +36,17 @@ module Browscapper
36
36
  self
37
37
  end
38
38
 
39
- def dump(format = :marshal)
40
- @entries or Browscapper.load
41
-
42
- Marshal.dump(@entries)
43
- end
44
-
45
39
  def clear_cache
46
40
  MATCH_CACHE.clear
47
41
  end
48
42
 
49
43
  def matches(ua)
44
+ return nil if ua_empty?(ua)
45
+
50
46
  @entries or self.load
51
47
 
52
- ua_str = ua.downcase
53
- ua_len = ua.length
48
+ ua_str = ua.to_s.downcase
49
+ ua_len = ua_str.length
54
50
 
55
51
  MATCH_CACHE[ua] ||= @entries.select { |k, v|
56
52
  v[:pattern] =~ ua_str if v
@@ -62,6 +58,8 @@ module Browscapper
62
58
  end
63
59
 
64
60
  def match(ua)
61
+ return nil if ua_empty?(ua)
62
+
65
63
  if MATCH_CACHE[ua] && !MATCH_CACHE[ua].empty?
66
64
  MATCH_CACHE[ua].first
67
65
  else
@@ -70,5 +68,13 @@ module Browscapper
70
68
  end
71
69
  end
72
70
  alias :query :match
71
+
72
+ private
73
+ def ua_empty?(ua)
74
+ return true if ua.nil?
75
+ return true if ua.respond_to?(:empty) && ua.empty?
76
+
77
+ false
78
+ end
73
79
  end
74
80
  end
@@ -0,0 +1,27 @@
1
+ # encoding: BINARY
2
+
3
+ $: << File.dirname(__FILE__)
4
+ require 'test_helper'
5
+
6
+ class Browscapper::UserAgentTest < MiniTest::Unit::TestCase
7
+ def test_eqeq_equal
8
+ a = Browscapper::UserAgent.new(tablet: :foo)
9
+ b = Browscapper::UserAgent.new(tablet: :foo)
10
+
11
+ assert(a == b)
12
+ end
13
+
14
+ def test_eqeq_not_equal
15
+ a = Browscapper::UserAgent.new(tablet: true)
16
+ b = Browscapper::UserAgent.new(tablet: :foo)
17
+
18
+ refute(a == b)
19
+ end
20
+
21
+ def test_eqeq_not_a_user_agent
22
+ a = Browscapper::UserAgent.new(tablet: true)
23
+
24
+ refute(a == nil)
25
+ refute(a == :foo)
26
+ end
27
+ end
@@ -44,12 +44,35 @@ class BrowscapperTest < MiniTest::Unit::TestCase
44
44
 
45
45
  FIREFOX_ON_OSX.pattern = /^mozilla\/5\.0 \(macintosh; .*; .*mac os x.*; .*; rv:1\.9\.2.*\) gecko\/.* firefox\/3\.6.*$/
46
46
 
47
+ FIREFOX_ON_OSX_USER_AGENT_STRING = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8'.freeze
48
+
49
+ def setup
50
+ Browscapper.clear_cache
51
+ end
52
+
47
53
  def test_browscap_ini
48
- Browscapper.load(File.join('vendor', 'browscap.ini'))
49
- match = Browscapper.match('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8')
54
+ match = Browscapper.match(FIREFOX_ON_OSX_USER_AGENT_STRING)
50
55
 
51
56
  FIREFOX_ON_OSX.each do |k, v|
52
57
  assert(v == match[k], " Expected #{v.inspect} for #{k}, got #{match[k].inspect}")
53
58
  end
54
59
  end
60
+
61
+ def test_match_nil
62
+ assert_nil(Browscapper.match(nil))
63
+ end
64
+
65
+ def test_match_empty
66
+ assert_nil(Browscapper.match(""))
67
+ end
68
+
69
+ def test_match_to_s
70
+ value = OpenStruct.new
71
+
72
+ def value.to_s
73
+ FIREFOX_ON_OSX_USER_AGENT_STRING
74
+ end
75
+
76
+ assert_equal(FIREFOX_ON_OSX, Browscapper.match(value))
77
+ end
55
78
  end
data/test/test_helper.rb CHANGED
@@ -23,4 +23,4 @@ if RUBY_VERSION >= '1.9'
23
23
  MiniTest::Reporters.use!(MiniTest::Reporters::SpecReporter.new)
24
24
  end
25
25
 
26
-
26
+ Browscapper.load(File.join('vendor', 'browscap.ini'))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browscapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - J Smith
@@ -48,6 +48,7 @@ files:
48
48
  - lib/browscapper/version.rb
49
49
  - lib/tasks/dump.rake
50
50
  - test/browscapper/reader_tests.rb
51
+ - test/browscapper/user_agent_tests.rb
51
52
  - test/browscapper_tests.rb
52
53
  - test/test_helper.rb
53
54
  - vendor/browscap.ini
@@ -77,5 +78,6 @@ specification_version: 4
77
78
  summary: A browscap file parser and matcher.
78
79
  test_files:
79
80
  - test/browscapper/reader_tests.rb
81
+ - test/browscapper/user_agent_tests.rb
80
82
  - test/browscapper_tests.rb
81
83
  - test/test_helper.rb