referer-parser 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  module RefererParser
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  require 'uri'
@@ -28,6 +28,20 @@ module RefererParser
28
28
  # So can be interrogated with .known? too.
29
29
  alias_method :known?, :known
30
30
 
31
+ def parse(referer_url)
32
+ @uri = Referer::parse_uri(referer_url)
33
+
34
+ referer = Referers::get_referer(@uri)
35
+ unless referer.nil?
36
+ @known = true
37
+ @referer = referer['name']
38
+ @search_parameter, @search_term = Referer::extract_search(@uri, referer['parameters'])
39
+ else
40
+ @known = false
41
+ @referer, @search_parameter, @search_term = nil # Being explicit
42
+ end
43
+ end
44
+
31
45
  private # -------------------------------------------------------------
32
46
 
33
47
  # Static method to turn a `raw_url`
@@ -75,23 +89,24 @@ module RefererParser
75
89
 
76
90
  return [nil, []] # No parameter or keywords to return
77
91
  end
78
-
92
+
79
93
  # Constructor. Takes the `referer_url`
80
94
  # to extract the referer from (can be
81
95
  # a String or URI)
82
- def initialize(referer_url)
83
-
84
- @uri = Referer::parse_uri(referer_url)
85
-
86
- referer = Referers::get_referer(@uri)
87
- unless referer.nil?
88
- @known = true
89
- @referer = referer['name']
90
- @search_parameter, @search_term = Referer::extract_search(@uri, referer['parameters'])
96
+ #
97
+ # Optionaly it takes the `referer_file` param
98
+ # to use instead of the bundle referers.yml
99
+ # (must be a yaml file)
100
+ def initialize(referer_url, referer_file = nil)
101
+
102
+ if referer_file.nil?
103
+ Referers::load_referers_from_yaml(Referers::get_yaml_file())
91
104
  else
92
- @known = false
93
- @referer, @search_parameter, @search_term = nil # Being explicit
105
+ Referers::load_referers_from_yaml(Referers::get_yaml_file(referer_file))
94
106
  end
107
+
108
+ parse(referer_url)
109
+
95
110
  end
96
111
  end
97
- end
112
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  require 'yaml'
@@ -38,8 +38,12 @@ module RefererParser
38
38
 
39
39
  # Returns the path to the YAML
40
40
  # file of referers
41
- def self.get_yaml_file
42
- File.join(File.dirname(__FILE__), '..', '..', 'data', 'search.yml')
41
+ def self.get_yaml_file(referer_file = nil)
42
+ if referer_file.nil?
43
+ File.join(File.dirname(__FILE__), '..', '..', 'data', 'referers.yml')
44
+ else
45
+ referer_file
46
+ end
43
47
  end
44
48
 
45
49
  # Initializes a hash of referers
@@ -52,11 +56,11 @@ module RefererParser
52
56
 
53
57
  # Load referer data stored in YAML file
54
58
  begin
55
- yaml = YAML.load_file(yaml_file)
59
+ yaml = YAML.load_file(yaml_file)['search'] # TODO: fix this when we support the other types
56
60
  rescue error
57
61
  raise CorruptReferersYamlError.new("Could not parse referers YAML file '#{yaml_file}'", error)
58
62
  end
59
- load_referers(yaml)
63
+ @referers = load_referers(yaml)
60
64
  end
61
65
 
62
66
  # Validate and expand the `raw_referers`
@@ -83,7 +87,5 @@ module RefererParser
83
87
  }
84
88
  return referers
85
89
  end
86
-
87
- @referers = load_referers_from_yaml(get_yaml_file())
88
90
  end
89
- end
91
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -9,11 +9,11 @@
9
9
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
- # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
12
+ # Original Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  module RefererParser
17
17
  NAME = "referer-parser"
18
- VERSION = "0.0.3"
19
- end
18
+ VERSION = "0.1.1"
19
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  require "referer-parser/version"
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  # -*- encoding: utf-8 -*-
@@ -19,10 +19,10 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
19
19
  require 'referer-parser/version'
20
20
 
21
21
  Gem::Specification.new do |gem|
22
- gem.authors = ["Yali Sassoon"]
22
+ gem.authors = ["Yali Sassoon", "Martin Loy", "Alex Dean"]
23
23
  gem.email = ["support@snowplowanalytics.com"]
24
24
  gem.description = %q{Library for extracting marketing attribution data from referer URLs}
25
- gem.summary = %q{Library for extracting marketing attribution data (e.g. search terms) from referer URLs. This is used by SnowPlow (http://github.com/snowplow/snowplow). Our hope is that this library (and referers.yml) will be extended by anyone interested in parsing referer URLs.}
25
+ gem.summary = %q{Library for extracting marketing attribution data (e.g. search terms) from referer (sic) URLs. This is used by Snowplow (http://github.com/snowplow/snowplow). Our hope is that this library (and referers.yml) will be extended by anyone interested in parsing referer URLs.}
26
26
  gem.homepage = "http://github.com/snowplow/referer-parser"
27
27
 
28
28
  gem.files = `git ls-files`.split($/)
data/spec/referer-spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
1
+ # Copyright (c) 2012-2013 Snowplow Analytics Ltd. All rights reserved.
2
2
  #
3
3
  # This program is licensed to you under the Apache License Version 2.0,
4
4
  # and you may not use this file except in compliance with the Apache License Version 2.0.
@@ -10,7 +10,7 @@
10
10
  # See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11
11
 
12
12
  # Author:: Yali Sassoon (mailto:support@snowplowanalytics.com)
13
- # Copyright:: Copyright (c) 2012 SnowPlow Analytics Ltd
13
+ # Copyright:: Copyright (c) 2012-2013 Snowplow Analytics Ltd
14
14
  # License:: Apache License Version 2.0
15
15
 
16
16
  require 'referer-parser'
@@ -22,6 +22,19 @@ describe RefererParser::Referer do
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
24
 
25
+ it "Should be initializable with an external referers.yml" do
26
+ external_referer = File.join(File.dirname(__FILE__), '..', 'data', 'referers.yml') # Using the bundled referers.yml in fact
27
+ uri = URI.parse(GOOGLE_COM_REFERER)
28
+ r = RefererParser::Referer.new(uri, external_referer)
29
+ r.referer.should eql "Google"
30
+ end
31
+
32
+ it "Should be initializable without an external referers.yml" do
33
+ uri = URI.parse(GOOGLE_COM_REFERER)
34
+ r = RefererParser::Referer.new(uri)
35
+ r.referer.should eql "Google"
36
+ end
37
+
25
38
  it "Should correctly parse a google.com referer URL" do
26
39
  r = RefererParser::Referer.new(GOOGLE_COM_REFERER)
27
40
  r.known?.should eql true
@@ -46,4 +59,13 @@ describe RefererParser::Referer do
46
59
  r = RefererParser::Referer.new(uri)
47
60
  r.referer.should eql "Google"
48
61
  end
49
- end
62
+
63
+ it "Should be possible to re-use a Referer object" do
64
+ r = RefererParser::Referer.new(GOOGLE_CO_UK_REFERER)
65
+ r.search_term.should eql "psychic bazaar"
66
+ r.parse(GOOGLE_COM_REFERER)
67
+ r.search_term.should eql "gateway oracle cards denise linn"
68
+ r.uri.host.should eql "www.google.com"
69
+ end
70
+
71
+ end
metadata CHANGED
@@ -1,53 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: referer-parser
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Yali Sassoon
9
+ - Martin Loy
10
+ - Alex Dean
14
11
  autorequire:
15
12
  bindir: bin
16
13
  cert_chain: []
17
-
18
- date: 2012-12-10 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
14
+ date: 2013-06-01 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
21
17
  name: rspec
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
24
19
  none: false
25
- requirements:
20
+ requirements:
26
21
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 15
29
- segments:
30
- - 2
31
- - 6
32
- version: "2.6"
22
+ - !ruby/object:Gem::Version
23
+ version: '2.6'
33
24
  type: :development
34
- version_requirements: *id001
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '2.6'
35
32
  description: Library for extracting marketing attribution data from referer URLs
36
- email:
33
+ email:
37
34
  - support@snowplowanalytics.com
38
35
  executables: []
39
-
40
36
  extensions: []
41
-
42
37
  extra_rdoc_files: []
43
-
44
- files:
38
+ files:
45
39
  - .gitignore
46
40
  - Gemfile
47
41
  - LICENSE-2.0.txt
48
42
  - README.md
49
43
  - Rakefile
50
- - data/search.yml
44
+ - data/referers.yml
51
45
  - lib/referer-parser.rb
52
46
  - lib/referer-parser/errors.rb
53
47
  - lib/referer-parser/referer.rb
@@ -57,36 +51,30 @@ files:
57
51
  - spec/referer-spec.rb
58
52
  homepage: http://github.com/snowplow/referer-parser
59
53
  licenses: []
60
-
61
54
  post_install_message:
62
55
  rdoc_options: []
63
-
64
- require_paths:
56
+ require_paths:
65
57
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
58
+ required_ruby_version: !ruby/object:Gem::Requirement
67
59
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
- required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
65
  none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
82
- - 0
83
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
84
70
  requirements: []
85
-
86
71
  rubyforge_project:
87
- rubygems_version: 1.8.24
72
+ rubygems_version: 1.8.25
88
73
  signing_key:
89
74
  specification_version: 3
90
- summary: Library for extracting marketing attribution data (e.g. search terms) from referer URLs. This is used by SnowPlow (http://github.com/snowplow/snowplow). Our hope is that this library (and referers.yml) will be extended by anyone interested in parsing referer URLs.
91
- test_files:
75
+ summary: Library for extracting marketing attribution data (e.g. search terms) from
76
+ referer (sic) URLs. This is used by Snowplow (http://github.com/snowplow/snowplow).
77
+ Our hope is that this library (and referers.yml) will be extended by anyone interested
78
+ in parsing referer URLs.
79
+ test_files:
92
80
  - spec/referer-spec.rb