product_spy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in product_spy.gemspec
4
+ gemspec
5
+ gem 'rake'
6
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Paul Spieker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ [![Build Status](https://travis-ci.org/spieker/product_spy.png)](https://travis-ci.org/spieker/product_spy)
2
+
3
+ # ProductSpy
4
+
5
+ ProductSpy is made for parsing product URLs, i.e. Amazon-URLs, to get
6
+ the product keys and creating product URLs based on these keys. This can
7
+ be used to identify products based on the url or just to clean up
8
+ product URLs.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'product_spy'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install product_spy
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ # Parsing URLs
28
+ ProductSpy.parse('https://www.amazon.de/dp/B004O9DF6O') => ['www.amazon.de', ['B004O9DF6O']]
29
+
30
+ # Building URLs
31
+ ProductSpy.build('amazon.de', ['B004O9DF6O']) # => https://www.amazon.de/dp/B004O9DF6O
32
+
33
+ # defining patterns
34
+ ProductSpy.setup do |config|
35
+ config.host 'www.amazon.de', :aliases => ['amazon.de'] do |host|
36
+ # ** Mobile
37
+ # http://www.amazon.de/gp/aw/d/B004O9DF7I/ref=aw_d_var_2nd_sports_img?vs=1
38
+ # http://www.amazon.de/gp/aw/B000K7BELW/ref=aw_imgblk_0?h=356&ie=UTF8&selIdx=0&tag=viddleit-21&w=320
39
+ host.parse /^http[s]{0,1}:\/\/(www\.|)amazon.de\/gp\/aw(\/d|)\/([A-Z0-9]{10})(\/.*|)$/, [3]
40
+
41
+ # ** Desktop
42
+ # http://www.amazon.de/Hauck-662984-Hochstuhl-Alpha-natur/dp/B000K7BELW/ref=sr_1_2?s=baby&ie=UTF8&qid=1360979509&sr=1-2
43
+ # http://www.amazon.de/dp/B000K7BELW/
44
+ host.parse /^http[s]{0,1}:\/\/(www\.|)amazon.de(\/.*?|)\/dp\/([A-Z0-9]{10})(\/.*|)$/, [3]
45
+
46
+ host.build :default, 'https://www.amazon.de/dp/:1'
47
+ host.build :mobile, 'https://www.amazon.de/gp/aw/d/:1'
48
+ end
49
+ end
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => [:spec]
5
+ desc 'run Rspec specs'
6
+ task :spec do
7
+ sh 'rspec spec'
8
+ end
@@ -0,0 +1,28 @@
1
+ module ProductSpy
2
+ class Configuration
3
+ include Singleton
4
+
5
+ attr_reader :hosts
6
+
7
+ def initialize
8
+ @hosts = {}
9
+ end
10
+
11
+ def host(host, options = {}, &block)
12
+ host_inst = Host.new(host)
13
+ yield host_inst
14
+ @hosts[host.to_sym] = host_inst
15
+
16
+ options = {
17
+ :aliases => []
18
+ }.merge(options)
19
+
20
+ options[:aliases].each do |host|
21
+ @hosts[host.to_sym] = host_inst
22
+ end
23
+ end
24
+ end
25
+ def self.setup(&block)
26
+ yield Configuration.instance
27
+ end
28
+ end
@@ -0,0 +1,62 @@
1
+ module ProductSpy
2
+ class Host
3
+ class Parser
4
+ def initialize(regexp, pk_keys)
5
+ @regexp = regexp
6
+ @pk_keys = pk_keys
7
+ end
8
+
9
+ def get_pk(url)
10
+ if m = @regexp.match(url)
11
+ return @pk_keys.map do |i|
12
+ m[i]
13
+ end
14
+ else
15
+ return nil
16
+ end
17
+ end
18
+ end
19
+
20
+ class Builder
21
+ def initialize(pattern)
22
+ @pattern = pattern
23
+ end
24
+
25
+ def get_url(pk)
26
+ @pattern.gsub /:(\d+)/ do |i|
27
+ pk[i.to_i]
28
+ end
29
+ end
30
+ end
31
+
32
+ attr_reader :host_name
33
+
34
+ def initialize(host_name)
35
+ @host_name = host_name
36
+ @parser = []
37
+ @builder = {}
38
+ end
39
+
40
+ def parse(regex, pk_keys)
41
+ @parser << Parser.new(regex, pk_keys)
42
+ end
43
+
44
+ def build(type, pattern)
45
+ @builder[type.to_sym] = Builder.new(pattern)
46
+ end
47
+
48
+ def make_url(pk, options)
49
+ builder = @builder[options[:type].to_sym]
50
+ builder.get_url(pk)
51
+ end
52
+
53
+ def make_pk(url)
54
+ @parser.each do |parser|
55
+ result = parser.get_pk(url)
56
+ return result unless result.nil?
57
+ end
58
+ return nil
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,3 @@
1
+ module ProductSpy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'singleton'
2
+ require 'addressable/uri'
3
+ require 'product_spy/version'
4
+ require 'product_spy/configuration'
5
+ require 'product_spy/host'
6
+
7
+ module ProductSpy
8
+ setup do |config|
9
+ config.host 'www.amazon.de', :aliases => ['amazon.de'] do |host|
10
+ # ** Mobile
11
+ # http://www.amazon.de/gp/aw/d/B004O9DF7I/ref=aw_d_var_2nd_sports_img?vs=1
12
+ # http://www.amazon.de/gp/aw/B000K7BELW/ref=aw_imgblk_0?h=356&ie=UTF8&selIdx=0&tag=viddleit-21&w=320
13
+ host.parse /^http[s]{0,1}:\/\/(www\.|)amazon.de\/gp\/aw(\/d|)\/([A-Z0-9]{10})(\/.*|)$/, [3]
14
+
15
+ # ** Desktop
16
+ # http://www.amazon.de/Hauck-662984-Hochstuhl-Alpha-natur/dp/B000K7BELW/ref=sr_1_2?s=baby&ie=UTF8&qid=1360979509&sr=1-2
17
+ # http://www.amazon.de/dp/B000K7BELW/
18
+ host.parse /^http[s]{0,1}:\/\/(www\.|)amazon.de(\/.*?|)\/dp\/([A-Z0-9]{10})(\/.*|)$/, [3]
19
+
20
+ host.build :default, 'https://www.amazon.de/dp/:1'
21
+ host.build :mobile, 'https://www.amazon.de/gp/aw/d/:1'
22
+ end
23
+ end
24
+
25
+ # Build the url for a given host and primary key
26
+ #
27
+ # Example
28
+ # =======
29
+ #
30
+ # ProductSpy.build('amazon.de', ['B004O9DF6O']) # => https://www.amazon.de/dp/B004O9DF6O
31
+ #
32
+ def self.build(host, pk, options = {})
33
+ options = {
34
+ :type => :default
35
+ }.merge(options)
36
+
37
+ host = Configuration.instance.hosts[host.to_sym]
38
+ host.make_url(pk, options)
39
+ end
40
+
41
+ # Get the primary key of a given url
42
+ #
43
+ # Example
44
+ # =======
45
+ #
46
+ # ProductSpy.parse('https://www.amazon.de/dp/B004O9DF6O') => ['www.amazon.de', ['B004O9DF6O']]
47
+ #
48
+ def self.parse(url)
49
+ uri = Addressable::URI.parse(url)
50
+ host = Configuration.instance.hosts[uri.host.downcase.to_sym]
51
+ return nil if host.nil?
52
+ [host.host_name.downcase, host.make_pk(url)]
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'product_spy/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "product_spy"
8
+ gem.version = ProductSpy::VERSION
9
+ gem.authors = ["Paul Spieker"]
10
+ gem.email = ["p.spieker@duenos.de"]
11
+ gem.description = %q{ProductSpy is made for parsing product URLs, i.e. Amazon-URLs, to get the product keys and creating product URLs based on these keys. This can be used to identify products based on the url or just to clean up product URLs.}
12
+ gem.summary = %q{ProductSpy is made for parsing product URLs, i.e. Amazon-URLs, to get the product keys and creating product URLs based on these keys}
13
+ gem.homepage = "https://github.com/spieker/product_spy"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'addressable'
21
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ describe ProductSpy, 'amazon' do
3
+ context 'parse url' do
4
+ it 'parses: https://www.amazon.de/dp/B000K7BELW' do
5
+ host, pk = ProductSpy.parse('https://www.amazon.de/dp/B000K7BELW')
6
+ host.should == 'www.amazon.de'
7
+ pk.should == ['B000K7BELW']
8
+ end
9
+
10
+ it 'parses: https://www.amazon.de/dp/B000K7BELW/' do
11
+ host, pk = ProductSpy.parse('https://www.amazon.de/dp/B000K7BELW/')
12
+ host.should == 'www.amazon.de'
13
+ pk.should == ['B000K7BELW']
14
+ end
15
+
16
+ it 'parses: http://www.amazon.de/gp/aw/d/B004O9DF6O/ref=s9_simh_awgw_d1_g200_i0/279-4049004-9468652?aid=aw_gw&apid=317822427&arc=1201&arid=13W5CTR69XS38DGRS4HQ&asn=center-6&pf_rd_i=aw_gw&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_p=317822427&pf_rd_r=13W5CTR69XS38DGRS4HQ&pf_rd_s=center-6&pf_rd_t=1201' do
17
+ host, pk = ProductSpy.parse('http://www.amazon.de/gp/aw/d/B004O9DF6O/ref=s9_simh_awgw_d1_g200_i0/279-4049004-9468652?aid=aw_gw&apid=317822427&arc=1201&arid=13W5CTR69XS38DGRS4HQ&asn=center-6&pf_rd_i=aw_gw&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_p=317822427&pf_rd_r=13W5CTR69XS38DGRS4HQ&pf_rd_s=center-6&pf_rd_t=1201')
18
+ host.should == 'www.amazon.de'
19
+ pk.should == ['B004O9DF6O']
20
+ end
21
+
22
+ it 'parses: http://www.amazon.de/Hauck-662984-Hochstuhl-Alpha-natur/dp/B000K7BELW/ref=sr_1_2?s=baby&ie=UTF8&qid=1360983324&sr=1-2' do
23
+ host, pk = ProductSpy.parse('http://www.amazon.de/Hauck-662984-Hochstuhl-Alpha-natur/dp/B000K7BELW/ref=sr_1_2?s=baby&ie=UTF8&qid=1360983324&sr=1-2')
24
+ host.should == 'www.amazon.de'
25
+ pk.should == ['B000K7BELW']
26
+ end
27
+ end
28
+
29
+ context 'build url' do
30
+ it 'returns: https://www.amazon.de/dp/B000K7BELW' do
31
+ ProductSpy.build('www.amazon.de', ['B000K7BELW']).should == 'https://www.amazon.de/dp/B000K7BELW'
32
+ end
33
+
34
+ it 'returns: https://www.amazon.de/gp/aw/d/B000K7BELW' do
35
+ ProductSpy.build('www.amazon.de', ['B000K7BELW'], :type => 'mobile').should == 'https://www.amazon.de/gp/aw/d/B000K7BELW'
36
+ end
37
+ end
38
+
39
+ context 'aliases' do
40
+ context 'parse url' do
41
+ it 'parses: https://amazon.de/dp/B000K7BELW' do
42
+ host, pk = ProductSpy.parse('https://www.amazon.de/dp/B000K7BELW')
43
+ host.should == 'www.amazon.de'
44
+ pk.should == ['B000K7BELW']
45
+ end
46
+ end
47
+
48
+ context 'build url' do
49
+ it 'returns: https://www.amazon.de/dp/B000K7BELW' do
50
+ ProductSpy.build('amazon.de', ['B000K7BELW']).should == 'https://www.amazon.de/dp/B000K7BELW'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'product_spy'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: product_spy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Spieker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ProductSpy is made for parsing product URLs, i.e. Amazon-URLs, to get
31
+ the product keys and creating product URLs based on these keys. This can be used
32
+ to identify products based on the url or just to clean up product URLs.
33
+ email:
34
+ - p.spieker@duenos.de
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - .rspec
41
+ - .travis.yml
42
+ - Gemfile
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - lib/product_spy.rb
47
+ - lib/product_spy/configuration.rb
48
+ - lib/product_spy/host.rb
49
+ - lib/product_spy/version.rb
50
+ - product_spy.gemspec
51
+ - spec/amazon_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/spieker/product_spy
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: ProductSpy is made for parsing product URLs, i.e. Amazon-URLs, to get the
77
+ product keys and creating product URLs based on these keys
78
+ test_files:
79
+ - spec/amazon_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: