meta_hari 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetaHari::Spyglass::Base do
4
+ let(:uri) { URI.parse 'http://example.com:8080/product.html' }
5
+ let(:path) { '/product.html' }
6
+ subject { described_class.new uri }
7
+
8
+ describe '#fetch_request' do
9
+ it 'creates a new GET request' do
10
+ expect(Net::HTTP::Get).to receive(:new).with(
11
+ path, 'User-Agent' => subject.send(:user_agent)
12
+ )
13
+ subject.send :fetch_request
14
+ end
15
+
16
+ it 'returns a GET request instance' do
17
+ expect(subject.send :fetch_request).to be_instance_of Net::HTTP::Get
18
+ end
19
+ end
20
+
21
+ describe '#fetch_data' do
22
+ it 'starts a new HTTP request' do
23
+ expect(Net::HTTP).to receive(:start).with(
24
+ uri.host, uri.port
25
+ ).and_return OpenStruct.new(:'error!' => nil)
26
+ subject.send :fetch_data
27
+ end
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
+ it 'fires an http request' do
36
+ http = OpenStruct.new request: nil
37
+ fetch_request = subject.send :fetch_request
38
+ allow(subject).to receive(:fetch_request).and_return fetch_request
39
+ expect(http).to receive(:request).with(fetch_request)
40
+ allow(Net::HTTP).to receive(:start).with(
41
+ uri.host, uri.port
42
+ ).and_yield(http).and_return OpenStruct.new(:'error!' => nil)
43
+ subject.send :fetch_data
44
+ end
45
+ end
46
+
47
+ describe '#spy' do
48
+ context 'with JSON-DL document' do
49
+ let(:instance) { described_class.new(uri) }
50
+ let(:html) { resource_content 'json_ld_example.html' }
51
+ subject { instance.spy }
52
+
53
+ before :each do
54
+ allow(instance).to receive(:fetch_data).and_return(html)
55
+ end
56
+
57
+ it { should be_an OpenStruct }
58
+ it { should respond_to :name }
59
+ it { should respond_to :image }
60
+ it { should respond_to :description }
61
+
62
+ it 'has the correct name' do
63
+ expect(subject.name).to eql 'Executive Anvil'
64
+ end
65
+
66
+ it 'has the correct image url' do
67
+ expected_value = 'http://www.example.com/anvil_executive.jpg'
68
+ expect(subject.image).to eql expected_value
69
+ end
70
+
71
+ it 'has the correct description' do
72
+ expected_value = [
73
+ 'Sleeker than ACME\'s Classic Anvil, the Executive Anvil is perfect',
74
+ 'for the business traveler looking for something to drop from a',
75
+ 'height.'
76
+ ].join(' ')
77
+ expect(subject.description).to eql expected_value
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetaHari do
4
+ describe '#find_suitable_spyglass' do
5
+ it 'returns a matching spy glass if it exists' do
6
+ uri = URI.parse 'http://amazon.de'
7
+ spyglass = MetaHari.send :find_suitable_spyglass, uri
8
+ expect(spyglass).to be MetaHari::Spyglass::AmazonDe
9
+ end
10
+
11
+ it 'returns the base spyglass if no matching exists' do
12
+ uri = URI.parse 'http://example.com'
13
+ spyglass = MetaHari.send :find_suitable_spyglass, uri
14
+ expect(spyglass).to be MetaHari::Spyglass::Base
15
+ end
16
+ end
17
+
18
+ describe '#suitable_spyglass_instance' do
19
+ it 'uses find_suitable_spyglass' do
20
+ uri = URI.parse 'http://example.com'
21
+ expect(MetaHari).to receive(:find_suitable_spyglass).with(uri).and_return(
22
+ MetaHari::Spyglass::Base
23
+ )
24
+ MetaHari.send :suitable_spyglass_instance, uri
25
+ end
26
+
27
+ it 'creates an instance of the suitable class' do
28
+ uri = URI.parse 'http://example.com'
29
+ expect(MetaHari::Spyglass::Base).to receive(:new).with(uri)
30
+ MetaHari.send :suitable_spyglass_instance, uri
31
+ end
32
+
33
+ it 'returns an instance of the suitable class' do
34
+ uri = URI.parse 'http://example.com'
35
+ spyglass = MetaHari.send :suitable_spyglass_instance, uri
36
+ expect(spyglass).to be_instance_of MetaHari::Spyglass::Base
37
+ end
38
+ end
39
+ end