kosher 0.1.12 → 0.2.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.
@@ -2,71 +2,33 @@ require 'spec_helper'
2
2
 
3
3
  module Kosher
4
4
  describe Offer do
5
- let(:offer) { Fabricate(:offer) }
6
-
7
- describe ".build" do
8
- use_vcr_cassette '0143105825'
9
-
10
- let(:asin) { '0143105825' }
11
-
12
- let(:doc) do
13
- request = Request.new(credentials)
14
- request.locale = :us
15
- request.batchify([asin])
16
- algorithm = request.get
17
-
18
- response = algorithm.instance_variable_get(:@response)
19
- response.find('Offer').first
20
- end
21
-
22
- it "should build an offer" do
23
- offer = Offer.build(doc)
24
-
25
- offer.should be_a Offer
26
- expect do
27
- offer.kosher?
28
- end.should_not raise_error
29
- end
30
-
31
- it "populates the offer listing ID" do
32
- offer = Offer.build(doc)
33
- offer.listing_id.should_not be_nil
34
- end
35
-
36
- it "populates the exchange ID" do
37
- offer = Offer.build(doc)
38
- offer.exchange_id.should_not be_nil
39
- end
40
-
41
- it "should handle blank descriptions" do
42
- doc['OfferAttributes']['ConditionNote'] = ''
43
- offer = Offer.build(doc)
44
-
45
- offer.description.should eql ''
46
- end
47
- end
5
+ let(:offer) { Offer.new }
48
6
 
49
7
  describe "#kosher?" do
50
- context "when condition is kosher" do
8
+ describe "when condition is kosher" do
51
9
  before do
52
- offer.condition = Fabricate(:kosher_condition, :grade => 1)
10
+ offer.condition = Condition.new(1)
53
11
  end
54
12
 
55
- context "when seller is kosher" do
13
+ describe "when seller is kosher" do
56
14
  before do
57
- offer.seller = Fabricate(:good_seller)
15
+ offer.seller = Seller.new
58
16
  end
59
17
 
60
- context "when description is kosher" do
61
- context "when offer ships now" do
18
+ describe "when description is kosher" do
19
+ before do
20
+ offer.description = Description.new
21
+ end
22
+
23
+ describe "when offer ships now" do
62
24
  it "returns true" do
63
25
  offer.should be_kosher
64
26
  end
65
27
  end
66
28
 
67
- context "when offer does not ship now" do
29
+ describe "when offer does not ship now" do
68
30
  before do
69
- offer.ships_in = 96
31
+ offer.hours_shipped = 72
70
32
  end
71
33
 
72
34
  it "returns false" do
@@ -75,9 +37,9 @@ module Kosher
75
37
  end
76
38
  end
77
39
 
78
- context "when description is not kosher" do
40
+ describe "when description is not kosher" do
79
41
  before do
80
- offer.description = Kosher::Description.new("Withdrawn library book")
42
+ offer.description = Description.new('Withdrawn library book')
81
43
  end
82
44
 
83
45
  it "returns false" do
@@ -86,9 +48,9 @@ module Kosher
86
48
  end
87
49
  end
88
50
 
89
- context "when seller is not kosher" do
51
+ describe "when seller is not kosher" do
90
52
  before do
91
- offer.seller = Fabricate(:bad_seller)
53
+ offer.seller = Seller.new('foo', 'bar', 4.0)
92
54
  end
93
55
 
94
56
  it "returns false" do
@@ -97,9 +59,9 @@ module Kosher
97
59
  end
98
60
  end
99
61
 
100
- context "when condition is not kosher" do
62
+ describe "when condition is not kosher" do
101
63
  before do
102
- offer.condition = Fabricate(:kosher_condition, :grade => 5)
64
+ offer.condition = Condition.new(5)
103
65
  end
104
66
 
105
67
  it "returns false" do
@@ -108,24 +70,106 @@ module Kosher
108
70
  end
109
71
  end
110
72
 
111
- describe "#ships_now?" do
112
- context "when offer ships within 48 hours" do
113
- before do
114
- offer.ships_in = 48
115
- end
73
+ describe "#available?" do
74
+ before do
75
+ Config.max_hours_shipped = 48
76
+ end
116
77
 
78
+ describe "when offer is expected to ship on time" do
117
79
  it "returns true" do
118
- offer.send(:ships_now?).should be_true
80
+ offer.hours_shipped = 48
81
+ offer.should be_available
119
82
  end
120
83
  end
121
84
 
122
- context "when offer ships in over 48 hours" do
85
+ describe "when offer is not expected to ship on time" do
86
+ it "returns false" do
87
+ offer.hours_shipped = 96
88
+ offer.should_not be_available
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "#price" do
94
+ it "returns the price" do
95
+ offer.price_in_cents = 100
96
+ offer.currency = 'USD'
97
+
98
+ offer.price.should be_kind_of Money
99
+ offer.price.format.should eql '$1.00'
100
+ end
101
+ end
102
+
103
+ describe "#<=>" do
104
+ before do
105
+ offer.condition = Condition.new(1)
106
+ offer.seller = Seller.new
107
+ offer.description = Description.new
108
+ offer.price_in_cents = 100
109
+ offer.currency = 'EUR'
110
+ @another_offer = offer.dup
111
+ end
112
+
113
+ context "when kosher" do
114
+ it "is greater than a non-kosher offer" do
115
+ @another_offer.condition = Condition.new(5)
116
+ offer.should be_kosher
117
+ @another_offer.should_not be_kosher
118
+
119
+ offer.should be > @another_offer
120
+ end
121
+
122
+ context "when the other offer is kosher too" do
123
+ it "is greater than another kosher offer with a higher price" do
124
+ @another_offer.price_in_cents = 200
125
+
126
+ offer.should be > @another_offer
127
+ end
128
+
129
+ it "is equal to another kosher offer with an equal price" do
130
+ offer.should be == @another_offer
131
+ end
132
+
133
+ it "is less than another kosher offer with a lower price" do
134
+ @another_offer.price_in_cents = 50
135
+
136
+ offer.should be < @another_offer
137
+ end
138
+ end
139
+ end
140
+
141
+ context "when not kosher" do
123
142
  before do
124
- offer.ships_in = 96
143
+ offer.condition = Condition.new(5)
125
144
  end
126
145
 
127
- it "returns false" do
128
- offer.send(:ships_now?).should be_false
146
+ it "is less than a kosher offer" do
147
+ offer.should_not be_kosher
148
+ @another_offer.should be_kosher
149
+
150
+ offer.should be < @another_offer
151
+ end
152
+
153
+ context "when the other offer is not kosher either" do
154
+ before do
155
+ @another_offer.condition = Condition.new(5)
156
+ end
157
+
158
+ it "is greater than another unkosher offer with a higher price" do
159
+ @another_offer.price_in_cents = 200
160
+
161
+ offer.should be > @another_offer
162
+ end
163
+
164
+ it "is equal to another unkosher offer with an equal price" do
165
+ offer.should be == @another_offer
166
+ end
167
+
168
+ it "is less than another unkosher offer with a lower price" do
169
+ @another_offer.price_in_cents = 50
170
+
171
+ offer.should be < @another_offer
172
+ end
129
173
  end
130
174
  end
131
175
  end
@@ -2,68 +2,54 @@ require 'spec_helper'
2
2
 
3
3
  module Kosher
4
4
  describe Seller do
5
- describe ".build" do
6
- use_vcr_cassette '0143105825'
7
-
8
- let(:asin) { '0143105825' }
9
-
10
- let(:response) do
11
- request = Request.new(credentials)
12
- request.locale = :us
13
- request.batchify([asin])
14
- algorithm = request.get
15
-
16
- algorithm.instance_variable_get(:@response)
17
- end
5
+ before do
6
+ Config.blacklist = nil
7
+ end
18
8
 
19
- let(:seller) do
20
- docs = response.find('Merchant')
21
- Seller.build(docs.first)
9
+ describe "#blacklisted?" do
10
+ before do
11
+ Config.blacklist = ['foo']
22
12
  end
23
13
 
24
- it "populates the merchant ID of a seller" do
25
- seller.merchant_id.should match /^[0-9A-Z]{13,14}$/
14
+ it "returns true if the seller is blacklisted" do
15
+ Seller.new('foo').should be_blacklisted
26
16
  end
27
17
 
28
- it "populates the name of a seller" do
29
- seller.name.should_not be_nil
18
+ it "returns false if the seller is not blacklisted" do
19
+ Seller.new('bar').should_not be_blacklisted
30
20
  end
21
+ end
31
22
 
32
- it "populates the average rating of a seller" do
33
- seller.average_rating.should be_an_instance_of Float
23
+ describe "#kosher?" do
24
+ before do
25
+ Config.min_rating = 4.8
34
26
  end
35
27
 
36
- it "builds a newly-launched seller" do
37
- doc = response.find('Merchant').detect { |doc| doc["AverageFeedbackRating"] == "0.0" }
38
- new_seller = Seller.build(doc)
39
-
40
- new_seller.average_rating.should eql 0.0
28
+ it "returns true if seller's average rating is 0.0" do
29
+ seller = Seller.new('foo', 'bar', 0.0)
30
+ seller.should be_kosher
41
31
  end
42
- end
43
32
 
44
- describe "#kosher?" do
45
- it "returns true if seller's average rating is 0.0" do
46
- seller = Fabricate(:new_seller)
33
+ it "returns true if seller's average rating is nil" do
34
+ seller = Seller.new
47
35
  seller.should be_kosher
48
36
  end
49
37
 
50
- it "returns true if seller's average rating is above 4.7" do
51
- seller = Fabricate(:good_seller)
38
+ it "returns true if seller's average rating is equal to or above the min average rating" do
39
+ seller = Seller.new('foo', 'bar', 4.8)
52
40
  seller.should be_kosher
53
41
  end
54
42
 
55
- it "returns false if sellers' average rating is 4.7 or below" do
56
- seller = Fabricate(:bad_seller)
43
+ it "returns false if sellers' average rating is below the min average rating" do
44
+ seller = Seller.new('foo', 'bar', 4.7)
57
45
  seller.should_not be_kosher
58
46
  end
59
47
 
60
48
  it "returns false if seller is blacklisted" do
61
- seller = Fabricate(:good_seller)
49
+ seller = Seller.new('foo', 'bar', 5.0)
62
50
  seller.should be_kosher
63
51
 
64
- seller.merchant_id = Faker::Amazon.merchant_id
65
- Seller.blacklist = [seller.merchant_id]
66
-
52
+ Config.blacklist = [seller.id]
67
53
  seller.should_not be_kosher
68
54
  end
69
55
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'rspec'
4
- require 'fabrication'
5
4
 
6
5
  require File.expand_path('../../lib/kosher', __FILE__)
7
6
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: kosher
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.12
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paper Cavalier
@@ -10,86 +10,31 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-07 00:00:00 +00:00
13
+ date: 2011-03-09 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: json
17
+ name: money
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.5.1
24
+ version: 3.6.1
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: sucker
29
- prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
31
- none: false
32
- requirements:
33
- - - ~>
34
- - !ruby/object:Gem::Version
35
- version: 1.3.1
36
- type: :runtime
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: fabrication
40
- prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ~>
45
- - !ruby/object:Gem::Version
46
- version: 0.9.5
47
- type: :development
48
- version_requirements: *id003
49
27
  - !ruby/object:Gem::Dependency
50
28
  name: rspec
51
29
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
30
+ requirement: &id002 !ruby/object:Gem::Requirement
53
31
  none: false
54
32
  requirements:
55
33
  - - ~>
56
34
  - !ruby/object:Gem::Version
57
35
  version: 2.5.0
58
36
  type: :development
59
- version_requirements: *id004
60
- - !ruby/object:Gem::Dependency
61
- name: ruby-debug19
62
- prerelease: false
63
- requirement: &id005 !ruby/object:Gem::Requirement
64
- none: false
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: 0.11.6
69
- type: :development
70
- version_requirements: *id005
71
- - !ruby/object:Gem::Dependency
72
- name: vcr
73
- prerelease: false
74
- requirement: &id006 !ruby/object:Gem::Requirement
75
- none: false
76
- requirements:
77
- - - ~>
78
- - !ruby/object:Gem::Version
79
- version: 1.7.0
80
- type: :development
81
- version_requirements: *id006
82
- - !ruby/object:Gem::Dependency
83
- name: webmock
84
- prerelease: false
85
- requirement: &id007 !ruby/object:Gem::Requirement
86
- none: false
87
- requirements:
88
- - - ~>
89
- - !ruby/object:Gem::Version
90
- version: 1.6.2
91
- type: :development
92
- version_requirements: *id007
37
+ version_requirements: *id002
93
38
  description: Kosher wraps Amazon in a loving embrace.
94
39
  email: code@papercavalier.com
95
40
  executables: []
@@ -106,34 +51,18 @@ files:
106
51
  - Rakefile
107
52
  - kosher.gemspec
108
53
  - lib/kosher.rb
109
- - lib/kosher/algorithm.rb
110
54
  - lib/kosher/condition.rb
55
+ - lib/kosher/config.rb
111
56
  - lib/kosher/description.rb
112
- - lib/kosher/errors.rb
113
- - lib/kosher/item.rb
114
57
  - lib/kosher/offer.rb
115
- - lib/kosher/request.rb
116
58
  - lib/kosher/seller.rb
117
- - lib/kosher/struct.rb
118
59
  - lib/kosher/version.rb
119
- - spec/fabricators/condition_fabricator.rb
120
- - spec/fabricators/offer_fabricator.rb
121
- - spec/fabricators/seller_fabricator.rb
122
- - spec/fixtures/cassette_library/0143105825.yml
123
- - spec/fixtures/cassette_library/batch-request.yml
124
- - spec/kosher/algorithm_spec.rb
125
60
  - spec/kosher/condition_spec.rb
61
+ - spec/kosher/config_spec.rb
126
62
  - spec/kosher/description_spec.rb
127
- - spec/kosher/item_spec.rb
128
63
  - spec/kosher/offer_spec.rb
129
- - spec/kosher/request_spec.rb
130
64
  - spec/kosher/seller_spec.rb
131
- - spec/kosher/struct_spec.rb
132
65
  - spec/spec_helper.rb
133
- - spec/support/credentials.rb
134
- - spec/support/faker.rb
135
- - spec/support/vcr.rb
136
- - walter_benjamin.jpg
137
66
  has_rdoc: true
138
67
  homepage: https://rubygems.org/gems/kosher
139
68
  licenses: []
@@ -163,20 +92,9 @@ signing_key:
163
92
  specification_version: 3
164
93
  summary: Wraps Amazon in a loving embrace.
165
94
  test_files:
166
- - spec/fabricators/condition_fabricator.rb
167
- - spec/fabricators/offer_fabricator.rb
168
- - spec/fabricators/seller_fabricator.rb
169
- - spec/fixtures/cassette_library/0143105825.yml
170
- - spec/fixtures/cassette_library/batch-request.yml
171
- - spec/kosher/algorithm_spec.rb
172
95
  - spec/kosher/condition_spec.rb
96
+ - spec/kosher/config_spec.rb
173
97
  - spec/kosher/description_spec.rb
174
- - spec/kosher/item_spec.rb
175
98
  - spec/kosher/offer_spec.rb
176
- - spec/kosher/request_spec.rb
177
99
  - spec/kosher/seller_spec.rb
178
- - spec/kosher/struct_spec.rb
179
100
  - spec/spec_helper.rb
180
- - spec/support/credentials.rb
181
- - spec/support/faker.rb
182
- - spec/support/vcr.rb
@@ -1,25 +0,0 @@
1
- module Kosher
2
- class Algorithm
3
- def initialize(response)
4
- raise ResponseNotValidError unless response.valid?
5
-
6
- @response = response
7
- end
8
-
9
- def items
10
- @response.map('Item') do |item|
11
- Item.build(item)
12
- end
13
- end
14
-
15
- def errors
16
- @response.errors.map do |error|
17
- error['Message'].scan(/[0-9A-Z]{10}/).first rescue nil
18
- end.compact
19
- end
20
-
21
- def response
22
- @response
23
- end
24
- end
25
- end
data/lib/kosher/errors.rb DELETED
@@ -1,3 +0,0 @@
1
- module Kosher
2
- class ResponseNotValidError < StandardError; end
3
- end
data/lib/kosher/item.rb DELETED
@@ -1,29 +0,0 @@
1
- module Kosher
2
- class Item < Struct.new(:asin, :offers, :offers_count, :sales_rank)
3
-
4
- class << self
5
- def build(doc)
6
- asin = doc['ASIN']
7
- sales_rank = doc['SalesRank'].to_i
8
- offers_count = doc['Offers']['TotalOffers'].to_i
9
- offers = build_offers(doc['Offers']['Offer'])
10
-
11
- new(asin, offers, offers_count, sales_rank)
12
- end
13
-
14
- private
15
-
16
- def build_offers(offers)
17
- [offers].flatten.compact.map do |offer|
18
-
19
- # Senify Yen because Ruby Money says so
20
- if offer['OfferListing']['Price']['CurrencyCode'] == 'JPY'
21
- offer['OfferListing']['Price']['Amount'] = offer['OfferListing']['Price']['Amount'].to_i * 100
22
- end
23
-
24
- Offer.build(offer)
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,28 +0,0 @@
1
- module Kosher
2
- class Request < Sucker::Request
3
- def initialize(args={})
4
- super
5
- add_parameters
6
- end
7
-
8
- def batchify(asins)
9
- self.<<({ 'ItemLookup.1.ItemId' => asins[0, 10] })
10
- self.<<({ 'ItemLookup.2.ItemId' => asins[10, 10] }) if asins.size > 10
11
- end
12
-
13
- def get
14
- Algorithm.new(super)
15
- end
16
-
17
- private
18
-
19
- def add_parameters
20
- self.<<({
21
- 'Operation' => 'ItemLookup',
22
- 'ItemLookup.Shared.IdType' => 'ASIN',
23
- 'ItemLookup.Shared.Condition' => 'All',
24
- 'ItemLookup.Shared.MerchantId' => 'All',
25
- 'ItemLookup.Shared.ResponseGroup' => ['OfferFull', 'SalesRank'] })
26
- end
27
- end
28
- end
data/lib/kosher/struct.rb DELETED
@@ -1,15 +0,0 @@
1
- require 'json'
2
-
3
- module Kosher
4
- class Struct < ::Struct
5
- def to_map
6
- map = Hash.new
7
- self.members.each { |m| map[m] = self[m] }
8
- map
9
- end
10
-
11
- def to_json(*a)
12
- to_map.to_json(*a)
13
- end
14
- end
15
- end
@@ -1,7 +0,0 @@
1
- Fabricator(:kosher_condition, :class_name => 'kosher/condition') do
2
- grade 1
3
- end
4
-
5
- Fabricator(:unkosher_condition, :class_name => 'kosher/condition') do
6
- grade 5
7
- end
@@ -1,8 +0,0 @@
1
- Fabricator(:offer, :class_name => 'kosher/offer') do
2
- seller { Fabricate(:good_seller) }
3
- description Kosher::Description.new('')
4
- condition { Fabricate(:kosher_condition) }
5
- ships_free false
6
- ships_in 48
7
- cents 100
8
- end
@@ -1,15 +0,0 @@
1
- Fabricator(:seller, :class_name => 'kosher/seller') do
2
- merchant_id Faker::Amazon.merchant_id
3
- end
4
-
5
- Fabricator(:new_seller, :from => :seller) do
6
- average_rating 0.0
7
- end
8
-
9
- Fabricator(:good_seller, :from => :seller) do
10
- average_rating 5.0
11
- end
12
-
13
- Fabricator(:bad_seller, :from => :seller) do
14
- average_rating 4.5
15
- end