barnie 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
- require "rspec"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
4
 
5
- require File.expand_path("../../lib/barnie", __FILE__)
5
+ require File.expand_path('../../lib/barnie', __FILE__)
6
6
 
7
7
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -1,12 +1,9 @@
1
- require 'bookland'
2
-
3
- def isbns_fixture
4
- isbns = File.new(File.expand_path("../../fixtures/asins.txt", __FILE__), "r").
5
- map { |line| Bookland::ISBN.to_13 line.chomp }
1
+ def isbns
2
+ file = File.new(File.expand_path("../../fixtures/isbns.txt", __FILE__), 'r')
6
3
 
7
4
  if block_given?
8
- isbns.each { |isbn| yield isbn }
5
+ file.each { |line| yield line.chomp }
6
+ else
7
+ file.map { |line| line.chomp }
9
8
  end
10
-
11
- isbns
12
9
  end
@@ -2,9 +2,9 @@ require "launchy"
2
2
 
3
3
  class Mechanize
4
4
  def show_me_the_page
5
- file_path = ::File.expand_path("../m.#{Time.now.to_i}.html", __FILE__)
5
+ file_path = ::File.expand_path("../mechanize-#{Time.now.to_i}.html", __FILE__)
6
6
 
7
- file = ::File.new(file_path, "w")
7
+ file = ::File.new(file_path, 'w')
8
8
  file.write(current_page.body)
9
9
  file.close
10
10
 
data/spec/support/vcr.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'vcr'
2
2
 
3
3
  VCR.config do |c|
4
- c.cassette_library_dir = File.expand_path("../../fixtures", __FILE__)
4
+ c.cassette_library_dir = File.expand_path('../../fixtures', __FILE__)
5
5
  c.stub_with :webmock
6
6
  c.ignore_localhost = true
7
7
  c.default_cassette_options = { :record => :new_episodes }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: barnie
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paper Cavalier
@@ -10,30 +10,30 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-08 00:00:00 +00:00
13
+ date: 2011-03-10 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: mechanize
17
+ name: kosher
18
18
  prerelease: false
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 1.0.0
24
+ version: 0.2.10
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
- name: bookland
28
+ name: mechanize
29
29
  prerelease: false
30
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
- - - "="
33
+ - - ~>
34
34
  - !ruby/object:Gem::Version
35
- version: 0.3.1
36
- type: :development
35
+ version: 1.0.0
36
+ type: :runtime
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: launchy
@@ -54,18 +54,18 @@ dependencies:
54
54
  requirements:
55
55
  - - ~>
56
56
  - !ruby/object:Gem::Version
57
- version: 2.1.0
57
+ version: 2.5.0
58
58
  type: :development
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: throttler
61
+ name: ruby-debug19
62
62
  prerelease: false
63
63
  requirement: &id005 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: 0.2.1
68
+ version: 0.11.0
69
69
  type: :development
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
@@ -76,7 +76,7 @@ dependencies:
76
76
  requirements:
77
77
  - - ~>
78
78
  - !ruby/object:Gem::Version
79
- version: 1.3.1
79
+ version: 1.7.0
80
80
  type: :development
81
81
  version_requirements: *id006
82
82
  - !ruby/object:Gem::Dependency
@@ -87,23 +87,11 @@ dependencies:
87
87
  requirements:
88
88
  - - ~>
89
89
  - !ruby/object:Gem::Version
90
- version: 1.6.1
90
+ version: 1.6.2
91
91
  type: :development
92
92
  version_requirements: *id007
93
- - !ruby/object:Gem::Dependency
94
- name: ruby-debug19
95
- prerelease: false
96
- requirement: &id008 !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- version: 0.11.0
102
- type: :development
103
- version_requirements: *id008
104
93
  description: A fake API to a well-known bookstore
105
- email:
106
- - code@papercavalier.com
94
+ email: code@papercavalier.com
107
95
  executables: []
108
96
 
109
97
  extensions: []
@@ -112,35 +100,26 @@ extra_rdoc_files: []
112
100
 
113
101
  files:
114
102
  - .gitignore
115
- - .rspec
116
103
  - Gemfile
117
104
  - LICENSE
118
105
  - README.md
119
106
  - Rakefile
120
107
  - barnie.gemspec
121
- - benchmarks/bm_helper.rb
122
- - benchmarks/stress_test.rb
123
108
  - lib/barnie.rb
124
- - lib/barnie/client.rb
125
- - lib/barnie/error.rb
126
109
  - lib/barnie/helpers.rb
110
+ - lib/barnie/request.rb
111
+ - lib/barnie/response.rb
127
112
  - lib/barnie/version.rb
128
113
  - rvmrc.example
129
- - spec/acceptance/availability_spec.rb
130
- - spec/acceptance/smokescreen_spec.rb
131
- - spec/fixtures/asins.txt
132
- - spec/fixtures/barnie_availability.yml
133
- - spec/fixtures/barnie_client.yml
134
- - spec/fixtures/barnie_helpers.yml
135
- - spec/fixtures/barnie_smokescreen.yml
136
- - spec/fixtures/bug.yml
114
+ - spec/barnie/helpers_spec.rb
115
+ - spec/barnie/request_spec.rb
116
+ - spec/barnie/response_spec.rb
117
+ - spec/fixtures/barnie.yml
118
+ - spec/fixtures/isbns.txt
137
119
  - spec/spec_helper.rb
138
120
  - spec/support/isbns.rb
139
121
  - spec/support/show_me_the_page.rb
140
122
  - spec/support/vcr.rb
141
- - spec/unit/barnie/client_spec.rb
142
- - spec/unit/barnie/helpers_spec.rb
143
- - spec/unit/barnie_spec.rb
144
123
  has_rdoc: true
145
124
  homepage: http://gloss.papercavalier.com/barnie
146
125
  licenses: []
@@ -165,23 +144,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
144
  requirements: []
166
145
 
167
146
  rubyforge_project: barnie
168
- rubygems_version: 1.5.0
147
+ rubygems_version: 1.5.2
169
148
  signing_key:
170
149
  specification_version: 3
171
150
  summary: A fake API to a well-known bookstore
172
151
  test_files:
173
- - spec/acceptance/availability_spec.rb
174
- - spec/acceptance/smokescreen_spec.rb
175
- - spec/fixtures/asins.txt
176
- - spec/fixtures/barnie_availability.yml
177
- - spec/fixtures/barnie_client.yml
178
- - spec/fixtures/barnie_helpers.yml
179
- - spec/fixtures/barnie_smokescreen.yml
180
- - spec/fixtures/bug.yml
152
+ - spec/barnie/helpers_spec.rb
153
+ - spec/barnie/request_spec.rb
154
+ - spec/barnie/response_spec.rb
155
+ - spec/fixtures/barnie.yml
156
+ - spec/fixtures/isbns.txt
181
157
  - spec/spec_helper.rb
182
158
  - spec/support/isbns.rb
183
159
  - spec/support/show_me_the_page.rb
184
160
  - spec/support/vcr.rb
185
- - spec/unit/barnie/client_spec.rb
186
- - spec/unit/barnie/helpers_spec.rb
187
- - spec/unit/barnie_spec.rb
data/.rspec DELETED
@@ -1 +0,0 @@
1
- -cfd
@@ -1,5 +0,0 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- require File.expand_path("../../lib/barnie", __FILE__)
5
- require File.expand_path("../../spec/support/isbns", __FILE__)
@@ -1,28 +0,0 @@
1
- require File.expand_path("../bm_helper", __FILE__)
2
- require "throttler"
3
-
4
- include Throttler
5
-
6
- print "Enter batch size: "
7
- batch_size = STDIN.gets.to_i
8
- printed_at = Time.now
9
- timestamps = []
10
- found = 0
11
-
12
- client = Barnie.new('Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-HK) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5')
13
-
14
- isbns_fixture.each_slice(batch_size) do |isbns|
15
- client.isbns = isbns
16
- throttle("bm") { found += client.books.size }
17
- timestamps << Time.now
18
-
19
- if Time.now - printed_at > 5.0
20
- puts Time.now.strftime("%H:%M:%S").center(38, '-')
21
- printed_at = Time.now
22
- average = (timestamps.
23
- select { |ts| printed_at - ts < 60.0 }.size / 60.0).
24
- round(3)
25
- puts [timestamps.size, average, found].
26
- map { |w| w.to_s.center(8) }.join(' | ')
27
- end
28
- end
data/lib/barnie/client.rb DELETED
@@ -1,57 +0,0 @@
1
- module Barnie
2
-
3
- # A wrapper around the API request
4
- class Client
5
- include Helpers
6
-
7
- HOST = "productsearch.barnesandnoble.com"
8
- PATH = "/search/results.aspx"
9
-
10
- attr_accessor :isbns,
11
- :agent
12
-
13
- def initialize(user_agent)
14
- self.agent = Mechanize.new { |a| a.user_agent = "iPhone #{user_agent}" }
15
- end
16
-
17
- def request
18
- agent.get(uri)
19
- end
20
-
21
- def books
22
- begin
23
- page = request
24
- container = page.search("#prod-container")
25
- container.map do |html|
26
- title = title(html)
27
-
28
- book = {
29
- :title => extract_title(title),
30
- :authors => extract_authors(html),
31
- :binding => extract_binding(html.search(".format").text),
32
- :ships_in => extract_ships_in(html.search(".availability").text),
33
- :price => extract_price(html.search(".price strong").text),
34
- :link => extract_link(title)
35
- }
36
- book[:isbn] = extract_isbn(book[:link])
37
- book
38
- end
39
- rescue NoMethodError => e
40
- raise Barnie::Error.new(e.message + page.inspect)
41
- end
42
- end
43
-
44
- private
45
-
46
- def build_query
47
- "ISBNLIST=#{self.isbns[0, 50].join(',')}"
48
- end
49
-
50
- def uri
51
- URI::HTTP.build(
52
- :host => HOST,
53
- :path => PATH,
54
- :query => build_query)
55
- end
56
- end
57
- end
data/lib/barnie/error.rb DELETED
@@ -1,7 +0,0 @@
1
- module Barnie
2
- class Error < StandardError
3
- def initialize(msg)
4
- super
5
- end
6
- end
7
- end
@@ -1,43 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Availability" do
4
- before do
5
- VCR.insert_cassette('barnie_availability')
6
- @client = Barnie.new("company details")
7
- end
8
-
9
- after do
10
- VCR.eject_cassette
11
- end
12
-
13
- it "returns a book that is available for sale" do
14
- @client.isbns = ["9780816614028"]
15
- book = @client.books.first
16
- book[:price].should be_an_instance_of Fixnum
17
- book[:ships_in].should be_an_instance_of Fixnum
18
- end
19
-
20
- it "returns an ebook" do
21
- # these are available as ebooks only
22
- # VCR should ensure it stays that way even if this changes in the future
23
- @client.isbns = ["9781400826094", "9780203029923"]
24
- @client.books.each do |book|
25
- book[:title].should be_an_instance_of String
26
- book[:isbn].should be_an_instance_of String
27
- book[:price].should be_an_instance_of Fixnum
28
- book[:ships_in].should eql nil
29
- end
30
- end
31
-
32
- it "returns a book that is not available for sale" do
33
- @client.isbns = ["9780002189347"]
34
- book = @client.books.first
35
- book[:price].should eql 0
36
- book[:ships_in].should be_nil
37
- end
38
-
39
- it "does not return a book that is not in the Barnes & Noble catalogue" do
40
- @client.isbns = ["9780002196642"]
41
- @client.books.should be_empty
42
- end
43
- end
@@ -1,18 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Barnie" do
4
- before do
5
- VCR.insert_cassette('barnie_smokescreen')
6
- @client = Barnie.new("company details")
7
- end
8
-
9
- after do
10
- VCR.eject_cassette
11
- end
12
-
13
- it "finds a book" do
14
- @client.isbns = ["9780486272351"]
15
- book = @client.books.first
16
- pp book
17
- end
18
- end