meta_hari 0.0.4 → 0.0.5
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.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/meta_hari/redirect_loop.rb +2 -0
- data/lib/meta_hari/redirect_notification.rb +9 -0
- data/lib/meta_hari/spyglass/base.rb +11 -11
- data/lib/meta_hari/version.rb +1 -1
- data/lib/meta_hari.rb +9 -5
- data/spec/lib/meta_hari/spyglass/base_spec.rb +32 -6
- data/spec/lib/meta_hari_spec.rb +27 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d588a4900a63c06626caa3b9474757320d223f6
|
4
|
+
data.tar.gz: e5129bc542ec83386fc0c372fff7a0c093b1a475
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 345088ec4c31789da877a5987fa82fc71e2cbcc92e6867c7df4807d9d492f70754e82c8c2791bc8d4882e7f8d500cdcd3c907cc9650802c29bff3aac90e49d75
|
7
|
+
data.tar.gz: e5748efbcfa759d3181c0eef50f4da419347f618d3be997001aee3e19ab59fb1362c2886d26df098d9ba99170dcf1243251b23d1667a490e9c81363508eb22e2
|
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# MetaHari
|
2
2
|
|
3
|
+
[ ](https://codeship.com/projects/80646)
|
4
|
+
[](https://gemnasium.com/spieker/meta_hari)
|
5
|
+
[](https://codeclimate.com/github/spieker/meta_hari)
|
6
|
+
|
3
7
|
Meta Hari is receiving product informations from a given product link
|
4
8
|
(i.e. from Amazon).
|
5
9
|
|
@@ -4,13 +4,15 @@ module MetaHari
|
|
4
4
|
module Spyglass
|
5
5
|
class Base
|
6
6
|
attr_reader :uri
|
7
|
+
attr_reader :iteration
|
7
8
|
|
8
9
|
def self.suitable?(uri)
|
9
10
|
fail StandardError.new, "not implemented for '#{uri.host}'"
|
10
11
|
end
|
11
12
|
|
12
|
-
def initialize(uri)
|
13
|
-
@uri
|
13
|
+
def initialize(uri, iteration = 0)
|
14
|
+
@uri = uri
|
15
|
+
@iteration = iteration
|
14
16
|
end
|
15
17
|
|
16
18
|
def spy
|
@@ -21,11 +23,7 @@ module MetaHari
|
|
21
23
|
protected
|
22
24
|
|
23
25
|
def spy_list
|
24
|
-
|
25
|
-
:spy_json_ld,
|
26
|
-
:spy_microdata,
|
27
|
-
:spy_open_graph
|
28
|
-
]
|
26
|
+
%i(spy_json_ld spy_microdata spy_open_graph)
|
29
27
|
end
|
30
28
|
|
31
29
|
def user_agent
|
@@ -47,12 +45,14 @@ module MetaHari
|
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
|
-
def fetch_data
|
48
|
+
def fetch_data
|
51
49
|
return @_data if @_data
|
52
|
-
fail ArgumentError.new, 'HTTP redirect too deep' if limit == 0
|
53
50
|
case res = fetch_response
|
54
|
-
when Net::HTTPSuccess
|
55
|
-
|
51
|
+
when Net::HTTPSuccess
|
52
|
+
@_data = res.body
|
53
|
+
when Net::HTTPRedirection
|
54
|
+
notification = RedirectNotification.new res['location'], iteration + 1
|
55
|
+
fail notification, 'Redirection'
|
56
56
|
else res.error!
|
57
57
|
end
|
58
58
|
end
|
data/lib/meta_hari/version.rb
CHANGED
data/lib/meta_hari.rb
CHANGED
@@ -3,6 +3,7 @@ require 'json'
|
|
3
3
|
require 'uri'
|
4
4
|
require 'net/http'
|
5
5
|
require 'microdata'
|
6
|
+
require 'meta_hari/redirect_notification'
|
6
7
|
require 'meta_hari/version'
|
7
8
|
require 'meta_hari/helpers'
|
8
9
|
require 'meta_hari/product'
|
@@ -19,17 +20,20 @@ require 'meta_hari/spyglass'
|
|
19
20
|
# ```
|
20
21
|
module MetaHari
|
21
22
|
class <<self
|
22
|
-
def spy(
|
23
|
-
uri = URI.parse
|
24
|
-
spyglass = suitable_spyglass_instance uri
|
23
|
+
def spy(uri, iteration = 0)
|
24
|
+
uri = URI.parse uri unless uri.is_a? URI
|
25
|
+
spyglass = suitable_spyglass_instance uri, iteration
|
25
26
|
spyglass.spy
|
27
|
+
rescue RedirectNotification => redirect
|
28
|
+
raise RedirectLoop.new, 'to many redirects' if redirect.iteration > 5
|
29
|
+
spy redirect.uri, redirect.iteration
|
26
30
|
end
|
27
31
|
|
28
32
|
private
|
29
33
|
|
30
|
-
def suitable_spyglass_instance(uri)
|
34
|
+
def suitable_spyglass_instance(uri, iteration = 0)
|
31
35
|
klass = find_suitable_spyglass(uri)
|
32
|
-
klass.new(uri)
|
36
|
+
klass.new(uri, iteration)
|
33
37
|
end
|
34
38
|
|
35
39
|
# Finding a suitable spyglass for the given URL. If no suitable spyglass
|
@@ -26,12 +26,6 @@ describe MetaHari::Spyglass::Base do
|
|
26
26
|
subject.send :fetch_data
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'fails when redirection limit is zero' do
|
30
|
-
expect do
|
31
|
-
subject.send :fetch_data, 0
|
32
|
-
end.to raise_exception
|
33
|
-
end
|
34
|
-
|
35
29
|
it 'fires an http request' do
|
36
30
|
http = OpenStruct.new request: nil
|
37
31
|
fetch_request = subject.send :fetch_request
|
@@ -42,6 +36,38 @@ describe MetaHari::Spyglass::Base do
|
|
42
36
|
).and_yield(http).and_return OpenStruct.new(:'error!' => nil)
|
43
37
|
subject.send :fetch_data
|
44
38
|
end
|
39
|
+
|
40
|
+
context 'with redirect' do
|
41
|
+
before :each do
|
42
|
+
redirect = Net::HTTPRedirection.new({}, 301, 'foo')
|
43
|
+
redirect['location'] = 'http://example.com/new'
|
44
|
+
allow(subject).to receive(:fetch_response).and_return redirect
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'throws a redirection notification' do
|
48
|
+
expect do
|
49
|
+
subject.send :fetch_data
|
50
|
+
end.to raise_error(RedirectNotification)
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'RedirectNotification' do
|
54
|
+
it 'contains the target url' do
|
55
|
+
begin
|
56
|
+
subject.send :fetch_data
|
57
|
+
rescue RedirectNotification => redirect
|
58
|
+
expect(redirect.uri).to match 'http://example.com/new'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'contains the iteration' do
|
63
|
+
begin
|
64
|
+
subject.send :fetch_data
|
65
|
+
rescue RedirectNotification => redirect
|
66
|
+
expect(redirect.iteration).to match 1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
45
71
|
end
|
46
72
|
|
47
73
|
describe '#spy' do
|
data/spec/lib/meta_hari_spec.rb
CHANGED
@@ -26,7 +26,7 @@ describe MetaHari do
|
|
26
26
|
|
27
27
|
it 'creates an instance of the suitable class' do
|
28
28
|
uri = URI.parse 'http://example.com'
|
29
|
-
expect(MetaHari::Spyglass::Base).to receive(:new).with(uri)
|
29
|
+
expect(MetaHari::Spyglass::Base).to receive(:new).with(uri, 0)
|
30
30
|
MetaHari.send :suitable_spyglass_instance, uri
|
31
31
|
end
|
32
32
|
|
@@ -36,4 +36,30 @@ describe MetaHari do
|
|
36
36
|
expect(spyglass).to be_instance_of MetaHari::Spyglass::Base
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
describe '#spy' do
|
41
|
+
context 'with redirect' do
|
42
|
+
it 'calls the spy method again with the new location' do
|
43
|
+
redirect = Net::HTTPRedirection.new({}, 301, 'foo')
|
44
|
+
redirect['location'] = 'http://example.com/new'
|
45
|
+
original_spy = MetaHari.method(:spy)
|
46
|
+
allow(MetaHari).to receive(:spy) { |a| original_spy.call(a) }
|
47
|
+
expect(MetaHari).to receive(:spy).with('http://example.com/new', 1)
|
48
|
+
did_once = false
|
49
|
+
allow_any_instance_of(MetaHari::Spyglass::Base).to(
|
50
|
+
receive(:fetch_response)
|
51
|
+
) do
|
52
|
+
if did_once
|
53
|
+
success = Net::HTTPSuccess.new({}, 200, 'foo')
|
54
|
+
allow(success).to receive(:body).and_return('')
|
55
|
+
success
|
56
|
+
else
|
57
|
+
did_once = true
|
58
|
+
redirect
|
59
|
+
end
|
60
|
+
end
|
61
|
+
MetaHari.spy('http://example.com')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
39
65
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meta_hari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Spieker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: microdata
|
@@ -170,6 +170,8 @@ files:
|
|
170
170
|
- lib/meta_hari/helpers/microdata.rb
|
171
171
|
- lib/meta_hari/helpers/open_graph.rb
|
172
172
|
- lib/meta_hari/product.rb
|
173
|
+
- lib/meta_hari/redirect_loop.rb
|
174
|
+
- lib/meta_hari/redirect_notification.rb
|
173
175
|
- lib/meta_hari/spyglass.rb
|
174
176
|
- lib/meta_hari/spyglass/amazon_de.rb
|
175
177
|
- lib/meta_hari/spyglass/base.rb
|