barnie 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = 'barnie'
16
16
 
17
- s.add_dependency('kosher', '~> 0.2.10')
17
+ s.add_dependency('kosher', '~> 0.2.18')
18
18
  s.add_dependency('mechanize', '~> 1.0.0')
19
19
  s.add_development_dependency('launchy', '~> 0.3.7')
20
20
  s.add_development_dependency('rspec', '~> 2.5.0')
@@ -4,6 +4,7 @@ require 'barnie/helpers'
4
4
  module Barnie
5
5
  class Response
6
6
  include Helpers
7
+ include Enumerable
7
8
 
8
9
  attr_reader :page
9
10
 
@@ -13,24 +14,12 @@ module Barnie
13
14
  @page = page
14
15
  end
15
16
 
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) }
17
+ def each(&block)
18
+ page.search('#prod-container').each { |html| block.call(parse(html)) }
26
19
  end
27
20
 
28
21
  private
29
22
 
30
- def container
31
- page.search('#prod-container')
32
- end
33
-
34
23
  def parse(html)
35
24
  price = extract_price(html.search('.price strong').text)
36
25
  hours = extract_ships_in(html.search('.availability').text) || 999
@@ -38,7 +27,7 @@ module Barnie
38
27
  link = extract_link(title)
39
28
  isbn = extract_isbn(link)
40
29
 
41
- Kosher::Snapshot.new(
30
+ Kosher::Book.new(
42
31
  'bn.com',
43
32
  isbn,
44
33
  nil,
@@ -52,7 +41,6 @@ module Barnie
52
41
  Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours))
53
42
  )] :
54
43
  [])
55
-
56
44
  end
57
45
  end
58
46
  end
@@ -1,3 +1,3 @@
1
- module Barnie #:nodoc:
2
- VERSION = '0.3.0'
1
+ module Barnie
2
+ VERSION = '0.3.1'
3
3
  end
@@ -19,73 +19,73 @@ module Barnie
19
19
  VCR.eject_cassette
20
20
  end
21
21
 
22
- it "includes Helpers" do
22
+ it "includes the Helpers module" do
23
23
  Response.ancestors.should include Helpers
24
24
  end
25
25
 
26
- describe "#to_a" do
27
- it "returns snapshots" do
28
- snapshots = response.to_a
29
-
30
- snapshots.should be_an_instance_of Array
31
- snapshots.first.should be_a Kosher::Snapshot
32
- end
33
-
34
- it "returns a result for a single ISBN" do
35
- request << [isbns.first]
36
- response = request.get
26
+ it "includes the Enumerable module" do
27
+ Response.ancestors.should include Enumerable
28
+ end
37
29
 
38
- response.to_a.size.should eql 1
30
+ context "when page does not include #prod-container element" do
31
+ let(:mock_page) do
32
+ page = mock('response')
33
+ page.stub!(:search).and_return(nil)
34
+ page
39
35
  end
40
36
 
41
- context "when page does not include #prod-container element" do
42
-
43
- let(:mock_page) do
44
- page = mock('response')
45
- page.stub!(:search).and_return(nil)
46
- page
37
+ context "when page body is empty and HTTP is OK" do
38
+ before do
39
+ mock_page.stub!(:body).and_return('')
40
+ mock_page.stub!(:code).and_return(200)
47
41
  end
48
42
 
49
- context "when page body is empty and HTTP is OK" do
50
- before do
51
- mock_page.stub!(:body).and_return("")
52
- mock_page.stub!(:code).and_return(200)
53
- end
54
-
55
- it "throws a Barnie error" do
56
- expect do
57
- Response.new(mock_page)
58
- end.to raise_error Barnie::Error, 'Blank page'
59
- end
43
+ it "throws a Barnie error" do
44
+ expect do
45
+ Response.new(mock_page)
46
+ end.to raise_error Barnie::Error, 'Blank page'
60
47
  end
61
48
  end
49
+ end
62
50
 
63
- it "returns a book that is available for sale" do
64
- request << ['9780816614028']
65
- response = request.get
66
- snapshot = response.to_a.first
67
- snapshot.offers_count.should eql 1
51
+ it "yields books" do
52
+ response.each do |book|
53
+ book.should be_a Kosher::Book
68
54
  end
55
+ end
69
56
 
70
- it "returns an ebook" do
71
- request << ["9781400826094", "9780203029923"]
72
- request.get.each do |snapshot|
73
- snapshot.isbn.should be_an_instance_of String
74
- snapshot.offers.first.price.cents.should > 0
75
- snapshot.offers.first.shipping.should_not be_kosher
76
- end
77
- end
57
+ it "returns a result for a single ISBN" do
58
+ request << [isbns.first]
59
+ response = request.get
78
60
 
79
- it "returns a book that is not available for sale" do
80
- request << ["9780002189347"]
81
- snapshot = request.get.to_a.first
82
- snapshot.offers_count.should eql 0
83
- end
61
+ response.to_a.size.should eql 1
62
+ end
63
+
64
+ it "returns a book that is available for sale" do
65
+ request << ['9780816614028']
66
+ response = request.get
67
+ book = response.to_a.first
68
+ book.offers_total.should eql 1
69
+ end
84
70
 
85
- it "does not return a book that is not in the Barnes & Noble catalogue" do
86
- request << ["9780002196642"]
87
- request.get.to_a.should be_empty
71
+ it "returns an ebook" do
72
+ request << ["9781400826094", "9780203029923"]
73
+ request.get.each do |book|
74
+ book.isbn.should be_an_instance_of String
75
+ book.offers.first.price.cents.should > 0
76
+ book.offers.first.shipping.should_not be_kosher
88
77
  end
89
78
  end
79
+
80
+ it "returns a book that is not available for sale" do
81
+ request << ["9780002189347"]
82
+ book = request.get.to_a.first
83
+ book.offers_total.should eql 0
84
+ end
85
+
86
+ it "does not return a book that is not in the Barnes & Noble catalogue" do
87
+ request << ["9780002196642"]
88
+ request.get.to_a.should be_empty
89
+ end
90
90
  end
91
91
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: barnie
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.3.1
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-15 00:00:00 +00:00
13
+ date: 2011-03-17 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -21,7 +21,7 @@ dependencies:
21
21
  requirements:
22
22
  - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 0.2.10
24
+ version: 0.2.18
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency