barnie 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,39 +5,54 @@ module Barnie
5
5
  class Response
6
6
  include Helpers
7
7
 
8
- attr_accessor :page
8
+ attr_reader :page
9
9
 
10
10
  def initialize(page)
11
- self.page = page
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
- def snapshots
15
- raise Error.new('Blank page') if page.body == '' && page.code == 200
16
-
17
- container = page.search('#prod-container')
18
- container.map do |html|
19
-
20
- price = extract_price(html.search('.price strong').text)
21
- hours = extract_ships_in(html.search('.availability').text) || 999
22
- title = title(html)
23
- link = extract_link(title)
24
- isbn = extract_isbn(link)
25
-
26
- Kosher::Snapshot.new(
27
- 'bn.com',
28
- isbn,
29
- nil,
30
- nil,
31
- price > 0 ? 1 : 0,
32
- price > 0 ?
33
- [Kosher::Offer.new(
34
- nil,
35
- Kosher::Item.new(price, 'USD', 1, Kosher::Condition.new(1), Kosher::Description.new('')),
36
- Kosher::Seller.new(nil, 'Barnes & Noble.com', nil, Kosher::Location.new('US')),
37
- Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours))
38
- )] :
39
- [])
40
- end
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
@@ -1,3 +1,3 @@
1
1
  module Barnie #:nodoc:
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -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
- @client = Response.new(nil)
15
+ @response = Response.new(mock_page)
8
16
  end
9
17
 
10
18
  it "sanitizes string" do
11
- @client.sanitize_string('"I am" a book \r\n "').should eql 'I am" a book'
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
- @client.extract_price('$14.95 abc').should eql 1495
16
- @client.extract_price(nil).should eql 0
17
- @client.extract_price('').should eql 0
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
- @client.extract_isbn('http://search.barnesandnoble.com/Best-of-Waffles-Pancakes/Jane-Stacey/e/9780002554756/?itm=1').should eql "9780002554756"
22
- @client.extract_isbn('http://music.barnesandnoble.com/Live/Brian-Regan/e/706442377723/?itm=1').should eql "0706442377723"
23
- @client.extract_isbn('"http://music.barnesandnoble.com/Getting-a-Good-Nights-Sleep/John-Selby/e/52296602529/?itm=1"').should eql "0052296602529"
24
- @client.extract_isbn(nil).should be_nil
25
- @client.extract_isbn('').should be_nil
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
- @client.extract_binding(' Paperback ').should eql "Paperback"
30
- @client.extract_binding(nil).should be_nil
31
- @client.extract_binding('').should be_nil
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
- @client.extract_ships_in('Usually ships within 24 hours').should eql 24
36
- @client.extract_ships_in('Usually ships within 2-3 days').should eql 3 * 24
37
- @client.extract_ships_in('Usually ships within 7 days').should eql 7 * 24
38
- @client.extract_ships_in('Usually ships within 7 days').should eql 7 * 24
39
- @client.extract_ships_in('A new copy is not available from Barnes & Noble.com at this time.').should be_nil
40
- @client.extract_ships_in(nil).should be_nil
41
- @client.extract_ships_in('').should be_nil
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
- @client = request.get
61
- @html = @client.page.search("#prod-container").first
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
- @client.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
73
+ @response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
66
74
  end
67
75
 
68
76
  it "extracts title" do
69
- @client.extract_title(@client.title(@html)).should eql "Birth of the Symbol : Ancient Readers at the Limits of Their Texts"
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
- @client.extract_link(@client.title(@html)).should eql "http://search.barnesandnoble.com/Birth-of-the-Symbol/Peter-T-Struck/e/9781400826094/?itm=1"
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
- @client.extract_authors(@html).should =~ ["Peter T. Struck"]
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
- @client = request.get
85
- @html = @client.page.search("#prod-container").first
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
- @client.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
97
+ @response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet
90
98
  end
91
99
 
92
100
  it "extracts title" do
93
- @client.extract_title(@client.title(@html)).should eql "A Thousand Plateaus : Capitalism and Schizophrenia"
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
- @client.extract_link(@client.title(@html)).should eql "http://search.barnesandnoble.com/A-Thousand-Plateaus/Gilles-Deleuze/e/9780816614028/?itm=1"
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
- @client.extract_authors(@html).should =~ ["Brian Massumi", "Gilles Deleuze"]
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 "#snapshots" do
26
+ describe "#to_a" do
27
27
  it "returns snapshots" do
28
- snapshots = response.snapshots
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.snapshots.size.should eql 1
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("response")
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).snapshots
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.snapshots.first
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.snapshots.each do |snapshot|
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.snapshots.first
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.snapshots.should be_empty
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.2.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-10 00:00:00 +00:00
13
+ date: 2011-03-15 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency