walmart_open 0.0.7 → 0.0.8

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.
@@ -1,3 +1,5 @@
1
+ require "walmart_open/stock_string"
2
+
1
3
  module WalmartOpen
2
4
  class Item
3
5
  API_ATTRIBUTES_MAPPING = {
@@ -35,6 +37,12 @@ module WalmartOpen
35
37
  raw_attributes["variants"].to_a
36
38
  end
37
39
 
40
+ def stock
41
+ if raw_attributes["stock"]
42
+ @_stock ||= StockString.new(raw_attributes["stock"])
43
+ end
44
+ end
45
+
38
46
  private
39
47
 
40
48
  def extract_known_attributes
@@ -0,0 +1,16 @@
1
+ module WalmartOpen
2
+ class StockString < String
3
+ METHODS = {
4
+ available: "Available",
5
+ limited: "Limited Supply",
6
+ few: "Last few items",
7
+ not_available: "Not available"
8
+ }
9
+
10
+ METHODS.each do |method_name, value|
11
+ define_method("#{method_name}?") do
12
+ self.casecmp(value) == 0
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module WalmartOpen
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "spec_helper"
2
2
  require "walmart_open/item"
3
+ require "walmart_open/stock_string"
3
4
 
4
5
  describe WalmartOpen::Item do
5
6
  let(:item_attrs) do
@@ -69,4 +70,21 @@ describe WalmartOpen::Item do
69
70
  end
70
71
  end
71
72
  end
73
+
74
+ context "#stock" do
75
+ let(:item) { WalmartOpen::Item.new(item_attrs) }
76
+
77
+ it "returns a memoized StockString" do
78
+ stock = item.stock
79
+
80
+ expect(stock).to be_a(WalmartOpen::StockString)
81
+ expect(item.stock.object_id).to eq(stock.object_id)
82
+ end
83
+
84
+ it "returns nil if stock attribute is nil" do
85
+ item_attrs.merge!("stock" => nil)
86
+
87
+ expect(item.stock).to be_nil
88
+ end
89
+ end
72
90
  end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+ require "walmart_open/stock_string"
3
+
4
+ describe WalmartOpen::StockString do
5
+ shared_examples "query method" do |method_name, humanized|
6
+ it "returns true when #{humanized}" do
7
+ expect(stock.public_send(method_name)).to eq(true)
8
+ expect(stock.downcase.public_send(method_name)).to eq(true)
9
+ expect(stock.upcase.public_send(method_name)).to eq(true)
10
+ end
11
+
12
+ it "returns true when something else" do
13
+ bad_stock = WalmartOpen::StockString.new("Something else")
14
+ expect(bad_stock.public_send(method_name)).to eq(false)
15
+ end
16
+ end
17
+
18
+ context "#available?" do
19
+ let(:stock) { WalmartOpen::StockString.new("Available") }
20
+
21
+ include_examples "query method", :available?, "available"
22
+ end
23
+
24
+ context "#limited?" do
25
+ let(:stock) { WalmartOpen::StockString.new("Limited Supply") }
26
+
27
+ include_examples "query method", :limited?, "limited"
28
+ end
29
+
30
+ context "#few?" do
31
+ let(:stock) { WalmartOpen::StockString.new("Last few items") }
32
+
33
+ include_examples "query method", :few?, "few"
34
+ end
35
+
36
+ context "#not_available?" do
37
+ let(:stock) { WalmartOpen::StockString.new("Not available") }
38
+
39
+ include_examples "query method", :not_available?, "not available"
40
+ end
41
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: walmart_open
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ngan Pham
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-02-24 00:00:00.000000000 Z
13
+ date: 2014-03-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
@@ -83,13 +83,13 @@ dependencies:
83
83
  requirements:
84
84
  - - ~>
85
85
  - !ruby/object:Gem::Version
86
- version: 3.0.0.beta1
86
+ version: 3.0.0.beta2
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  none: false
89
89
  requirements:
90
90
  - - ~>
91
91
  - !ruby/object:Gem::Version
92
- version: 3.0.0.beta1
92
+ version: 3.0.0.beta2
93
93
  type: :development
94
94
  prerelease: false
95
95
  - !ruby/object:Gem::Dependency
@@ -170,6 +170,7 @@ files:
170
170
  - lib/walmart_open/requests/token.rb
171
171
  - lib/walmart_open/search_results.rb
172
172
  - lib/walmart_open/shipping_address.rb
173
+ - lib/walmart_open/stock_string.rb
173
174
  - lib/walmart_open/version.rb
174
175
  - lib/walmart_open.rb
175
176
  - spec/spec_helper.rb
@@ -190,6 +191,7 @@ files:
190
191
  - spec/walmart_open/requests/token_spec.rb
191
192
  - spec/walmart_open/search_results_spec.rb
192
193
  - spec/walmart_open/shipping_address_spec.rb
194
+ - spec/walmart_open/stock_string_spec.rb
193
195
  - LICENSE.txt
194
196
  - Rakefile
195
197
  - README.md
@@ -237,3 +239,4 @@ test_files:
237
239
  - spec/walmart_open/requests/token_spec.rb
238
240
  - spec/walmart_open/search_results_spec.rb
239
241
  - spec/walmart_open/shipping_address_spec.rb
242
+ - spec/walmart_open/stock_string_spec.rb