referer-parser 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,13 +53,13 @@ module RefererParser
53
53
  uri = if raw_url.is_a? String
54
54
  begin
55
55
  URI.parse(raw_url)
56
- rescue error
57
- raise InvalidUriError.new("Cannot parse String #{raw_url} into URI", error)
56
+ rescue => error
57
+ raise InvalidUriError, error.message
58
58
  end
59
59
  elsif raw_url.is_a? URI
60
60
  raw_url
61
61
  else
62
- raise InvalidUriError "'#{raw_url}' must be a String or URI"
62
+ raise InvalidUriError, "'#{raw_url}' must be a String or URI"
63
63
  end
64
64
 
65
65
  unless %w( http https ).include?(uri.scheme)
@@ -74,6 +74,7 @@ module RefererParser
74
74
  # Returns a 'tuple' of the parameter found plus
75
75
  # the keywords.
76
76
  def self.extract_search(uri, possible_parameters)
77
+ param = nil
77
78
 
78
79
  # Only get keywords if there's a query string to extract them from...
79
80
  if uri.query
@@ -82,12 +83,17 @@ module RefererParser
82
83
  # Try each possible keyword parameter with the querystring until one returns a result
83
84
  possible_parameters.each do | pp |
84
85
  if parameters.has_key?(pp)
85
- return [pp, parameters[pp].first] # Silently swallow a second or third value in the array
86
+ param = pp
87
+ parameters[pp].each do |result|
88
+ unless result == ""
89
+ return [pp, result] # return first value not eql ""
90
+ end
91
+ end
86
92
  end
87
93
  end
88
94
  end
89
95
 
90
- return [nil, []] # No parameter or keywords to return
96
+ return [param, []] # No parameter or keywords to return
91
97
  end
92
98
 
93
99
  # Constructor. Takes the `referer_url`
@@ -15,5 +15,5 @@
15
15
 
16
16
  module RefererParser
17
17
  NAME = "referer-parser"
18
- VERSION = "0.1.1"
18
+ VERSION = "0.2.0"
19
19
  end
data/spec/referer-spec.rb CHANGED
@@ -21,6 +21,7 @@ describe RefererParser::Referer do
21
21
  GOOGLE_COM_REFERER = 'http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari&tbo=d&biw=768&bih=900&source=lnms&tbm=isch&ei=t9fTT_TFEYb28gTtg9HZAw&sa=X&oi=mode_link&ct=mode&cd=2&sqi=2&ved=0CEUQ_AUoAQ'
22
22
  GOOGLE_CO_UK_REFERER = 'http://www.google.co.uk/search?hl=en&client=safari&q=psychic+bazaar&oq=psychic+bazaa&aq=0&aqi=g1&aql=&gs_l=mobile-gws-serp.1.0.0.61498.64599.0.66559.12.9.1.1.2.2.2407.10525.6-2j0j1j3.6.0...0.0.DiYO_7K_ndg&mvs=0'
23
23
  FACEBOOK_COM_REFERER = 'http://www.facebook.com/l.php?u=http%3A%2F%2Fpsy.bz%2FLtPadV&h=MAQHYFyRRAQFzmokHhn3w4LGWVzjs7YwZGejw7Up5TqNHIw'
24
+ TRUNCATED_REFERER = 'http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9108147844898389&output=html&h=60&slotname=1720218904&w=468&lmt=1368485108&flash=11.7.700.169&url=http%3A%2F%2Fwww.bsaving.com%2Fprintable-online-target-coupons%3Futm_source%3Dbsaving_new_Email%2'
24
25
 
25
26
  it "Should be initializable with an external referers.yml" do
26
27
  external_referer = File.join(File.dirname(__FILE__), '..', 'data', 'referers.yml') # Using the bundled referers.yml in fact
@@ -68,4 +69,24 @@ describe RefererParser::Referer do
68
69
  r.uri.host.should eql "www.google.com"
69
70
  end
70
71
 
72
+ it "Should return the better result when the referer contains two or more parameters" do
73
+ referer_contains_two_params = "http://search.tiscali.it/?tiscalitype=web&collection=web&q=&key=hello"
74
+ r = RefererParser::Referer.new(referer_contains_two_params)
75
+ r.search_term.should eql "hello"
76
+ r.search_parameter.should eql "key"
77
+ end
78
+
79
+ it "Should return the better result when the referer contains same parameters" do
80
+ referer_contains_two_params = "http://search.tiscali.it/?tiscalitype=web&collection=web&key=&key=hello"
81
+ r = RefererParser::Referer.new(referer_contains_two_params)
82
+ r.search_term.should eql "hello"
83
+ r.search_parameter.should eql "key"
84
+ end
85
+
86
+ it "should raise InvalidUriError on a truncated Uri" do
87
+ expect{
88
+ r = RefererParser::Referer.new(TRUNCATED_REFERER)
89
+ }.to raise_error(RefererParser::InvalidUriError)
90
+ end
91
+
71
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: referer-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-06-01 00:00:00.000000000 Z
14
+ date: 2013-06-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec