barnie 0.2.0 → 0.3.0
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.
- data/lib/barnie/response.rb +44 -29
- data/lib/barnie/version.rb +1 -1
- data/spec/barnie/helpers_spec.rb +40 -32
- data/spec/barnie/response_spec.rb +9 -9
- metadata +2 -2
data/lib/barnie/response.rb
CHANGED
@@ -5,39 +5,54 @@ module Barnie
|
|
5
5
|
class Response
|
6
6
|
include Helpers
|
7
7
|
|
8
|
-
|
8
|
+
attr_reader :page
|
9
9
|
|
10
10
|
def initialize(page)
|
11
|
-
|
11
|
+
raise Error.new('Blank page') if page.body.empty? && page.code == 200
|
12
|
+
|
13
|
+
@page = page
|
14
|
+
end
|
15
|
+
|
16
|
+
# Yields each snapshot to given block.
|
17
|
+
#
|
18
|
+
def each
|
19
|
+
container.each { |html| yield parse(html) }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns an array of snapshots.
|
23
|
+
#
|
24
|
+
def to_a
|
25
|
+
container.map { |html| parse(html) }
|
12
26
|
end
|
13
27
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
)
|
39
|
-
|
40
|
-
|
28
|
+
private
|
29
|
+
|
30
|
+
def container
|
31
|
+
page.search('#prod-container')
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse(html)
|
35
|
+
price = extract_price(html.search('.price strong').text)
|
36
|
+
hours = extract_ships_in(html.search('.availability').text) || 999
|
37
|
+
title = title(html)
|
38
|
+
link = extract_link(title)
|
39
|
+
isbn = extract_isbn(link)
|
40
|
+
|
41
|
+
Kosher::Snapshot.new(
|
42
|
+
'bn.com',
|
43
|
+
isbn,
|
44
|
+
nil,
|
45
|
+
nil,
|
46
|
+
price > 0 ? 1 : 0,
|
47
|
+
price > 0 ?
|
48
|
+
[Kosher::Offer.new(
|
49
|
+
nil,
|
50
|
+
Kosher::Item.new(price, 'USD', 1, Kosher::Condition.new(1), Kosher::Description.new('')),
|
51
|
+
Kosher::Seller.new(nil, 'Barnes & Noble.com', nil, Kosher::Location.new('US')),
|
52
|
+
Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours))
|
53
|
+
)] :
|
54
|
+
[])
|
55
|
+
|
41
56
|
end
|
42
57
|
end
|
43
58
|
end
|
data/lib/barnie/version.rb
CHANGED
data/spec/barnie/helpers_spec.rb
CHANGED
@@ -3,42 +3,50 @@ require "spec_helper"
|
|
3
3
|
|
4
4
|
module Barnie
|
5
5
|
describe Helpers do
|
6
|
+
let(:mock_page) do
|
7
|
+
page = mock('response')
|
8
|
+
page.stub!(:body).and_return('foo')
|
9
|
+
page.stub!(:code)
|
10
|
+
|
11
|
+
page
|
12
|
+
end
|
13
|
+
|
6
14
|
before do
|
7
|
-
@
|
15
|
+
@response = Response.new(mock_page)
|
8
16
|
end
|
9
17
|
|
10
18
|
it "sanitizes string" do
|
11
|
-
@
|
19
|
+
@response.sanitize_string('"I am" a book \r\n "').should eql 'I am" a book'
|
12
20
|
end
|
13
21
|
|
14
22
|
it "extract price" do
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
23
|
+
@response.extract_price('$14.95 abc').should eql 1495
|
24
|
+
@response.extract_price(nil).should eql 0
|
25
|
+
@response.extract_price('').should eql 0
|
18
26
|
end
|
19
27
|
|
20
28
|
it "extracts ISBN" do
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
29
|
+
@response.extract_isbn('http://search.barnesandnoble.com/Best-of-Waffles-Pancakes/Jane-Stacey/e/9780002554756/?itm=1').should eql "9780002554756"
|
30
|
+
@response.extract_isbn('http://music.barnesandnoble.com/Live/Brian-Regan/e/706442377723/?itm=1').should eql "0706442377723"
|
31
|
+
@response.extract_isbn('"http://music.barnesandnoble.com/Getting-a-Good-Nights-Sleep/John-Selby/e/52296602529/?itm=1"').should eql "0052296602529"
|
32
|
+
@response.extract_isbn(nil).should be_nil
|
33
|
+
@response.extract_isbn('').should be_nil
|
26
34
|
end
|
27
35
|
|
28
36
|
it "extracts binding" do
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
37
|
+
@response.extract_binding(' Paperback ').should eql "Paperback"
|
38
|
+
@response.extract_binding(nil).should be_nil
|
39
|
+
@response.extract_binding('').should be_nil
|
32
40
|
end
|
33
41
|
|
34
42
|
it "extracts maximum shipping hours" do
|
35
|
-
@
|
36
|
-
@
|
37
|
-
@
|
38
|
-
@
|
39
|
-
@
|
40
|
-
@
|
41
|
-
@
|
43
|
+
@response.extract_ships_in('Usually ships within 24 hours').should eql 24
|
44
|
+
@response.extract_ships_in('Usually ships within 2-3 days').should eql 3 * 24
|
45
|
+
@response.extract_ships_in('Usually ships within 7 days').should eql 7 * 24
|
46
|
+
@response.extract_ships_in('Usually ships within 7 days').should eql 7 * 24
|
47
|
+
@response.extract_ships_in('A new copy is not available from Barnes & Noble.com at this time.').should be_nil
|
48
|
+
@response.extract_ships_in(nil).should be_nil
|
49
|
+
@response.extract_ships_in('').should be_nil
|
42
50
|
end
|
43
51
|
|
44
52
|
context "requiring real HTML objects" do
|
@@ -57,48 +65,48 @@ module Barnie
|
|
57
65
|
context "ebook" do
|
58
66
|
before do
|
59
67
|
request << ["9781400826094"]
|
60
|
-
@
|
61
|
-
@html = @
|
68
|
+
@response = request.get
|
69
|
+
@html = @response.page.search("#prod-container").first
|
62
70
|
end
|
63
71
|
|
64
72
|
it "returns title as XML NodeSet" do
|
65
|
-
@
|
73
|
+
@response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
|
66
74
|
end
|
67
75
|
|
68
76
|
it "extracts title" do
|
69
|
-
@
|
77
|
+
@response.extract_title(@response.title(@html)).should eql "Birth of the Symbol : Ancient Readers at the Limits of Their Texts"
|
70
78
|
end
|
71
79
|
|
72
80
|
it "extracts link" do
|
73
|
-
@
|
81
|
+
@response.extract_link(@response.title(@html)).should eql "http://search.barnesandnoble.com/Birth-of-the-Symbol/Peter-T-Struck/e/9781400826094/?itm=1"
|
74
82
|
end
|
75
83
|
|
76
84
|
it "extracts authors" do
|
77
|
-
@
|
85
|
+
@response.extract_authors(@html).should =~ ["Peter T. Struck"]
|
78
86
|
end
|
79
87
|
end
|
80
88
|
|
81
89
|
context "paperback" do
|
82
90
|
before do
|
83
91
|
request << ["9780816614028"]
|
84
|
-
@
|
85
|
-
@html = @
|
92
|
+
@response = request.get
|
93
|
+
@html = @response.page.search("#prod-container").first
|
86
94
|
end
|
87
95
|
|
88
96
|
it "returns title as XML NodeSet" do
|
89
|
-
@
|
97
|
+
@response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
|
90
98
|
end
|
91
99
|
|
92
100
|
it "extracts title" do
|
93
|
-
@
|
101
|
+
@response.extract_title(@response.title(@html)).should eql "A Thousand Plateaus : Capitalism and Schizophrenia"
|
94
102
|
end
|
95
103
|
|
96
104
|
it "extracts link" do
|
97
|
-
@
|
105
|
+
@response.extract_link(@response.title(@html)).should eql "http://search.barnesandnoble.com/A-Thousand-Plateaus/Gilles-Deleuze/e/9780816614028/?itm=1"
|
98
106
|
end
|
99
107
|
|
100
108
|
it "extracts authors" do
|
101
|
-
@
|
109
|
+
@response.extract_authors(@html).should =~ ["Brian Massumi", "Gilles Deleuze"]
|
102
110
|
end
|
103
111
|
end
|
104
112
|
end
|
@@ -23,9 +23,9 @@ module Barnie
|
|
23
23
|
Response.ancestors.should include Helpers
|
24
24
|
end
|
25
25
|
|
26
|
-
describe "#
|
26
|
+
describe "#to_a" do
|
27
27
|
it "returns snapshots" do
|
28
|
-
snapshots = response.
|
28
|
+
snapshots = response.to_a
|
29
29
|
|
30
30
|
snapshots.should be_an_instance_of Array
|
31
31
|
snapshots.first.should be_a Kosher::Snapshot
|
@@ -35,13 +35,13 @@ module Barnie
|
|
35
35
|
request << [isbns.first]
|
36
36
|
response = request.get
|
37
37
|
|
38
|
-
response.
|
38
|
+
response.to_a.size.should eql 1
|
39
39
|
end
|
40
40
|
|
41
41
|
context "when page does not include #prod-container element" do
|
42
42
|
|
43
43
|
let(:mock_page) do
|
44
|
-
page = mock(
|
44
|
+
page = mock('response')
|
45
45
|
page.stub!(:search).and_return(nil)
|
46
46
|
page
|
47
47
|
end
|
@@ -54,7 +54,7 @@ module Barnie
|
|
54
54
|
|
55
55
|
it "throws a Barnie error" do
|
56
56
|
expect do
|
57
|
-
Response.new(mock_page)
|
57
|
+
Response.new(mock_page)
|
58
58
|
end.to raise_error Barnie::Error, 'Blank page'
|
59
59
|
end
|
60
60
|
end
|
@@ -63,13 +63,13 @@ module Barnie
|
|
63
63
|
it "returns a book that is available for sale" do
|
64
64
|
request << ['9780816614028']
|
65
65
|
response = request.get
|
66
|
-
snapshot = response.
|
66
|
+
snapshot = response.to_a.first
|
67
67
|
snapshot.offers_count.should eql 1
|
68
68
|
end
|
69
69
|
|
70
70
|
it "returns an ebook" do
|
71
71
|
request << ["9781400826094", "9780203029923"]
|
72
|
-
request.get.
|
72
|
+
request.get.each do |snapshot|
|
73
73
|
snapshot.isbn.should be_an_instance_of String
|
74
74
|
snapshot.offers.first.price.cents.should > 0
|
75
75
|
snapshot.offers.first.shipping.should_not be_kosher
|
@@ -78,13 +78,13 @@ module Barnie
|
|
78
78
|
|
79
79
|
it "returns a book that is not available for sale" do
|
80
80
|
request << ["9780002189347"]
|
81
|
-
snapshot = request.get.
|
81
|
+
snapshot = request.get.to_a.first
|
82
82
|
snapshot.offers_count.should eql 0
|
83
83
|
end
|
84
84
|
|
85
85
|
it "does not return a book that is not in the Barnes & Noble catalogue" do
|
86
86
|
request << ["9780002196642"]
|
87
|
-
request.get.
|
87
|
+
request.get.to_a.should be_empty
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: barnie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paper Cavalier
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-15 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|