walmart_open 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/README.md +0 -101
  2. data/lib/walmart_open/client.rb +0 -17
  3. data/lib/walmart_open/config.rb +1 -17
  4. data/lib/walmart_open/connection_manager.rb +5 -15
  5. data/lib/walmart_open/errors.rb +0 -6
  6. data/lib/walmart_open/request.rb +13 -6
  7. data/lib/walmart_open/requests/feed.rb +2 -2
  8. data/lib/walmart_open/requests/lookup.rb +2 -2
  9. data/lib/walmart_open/requests/search.rb +2 -2
  10. data/lib/walmart_open/requests/taxonomy.rb +2 -2
  11. data/lib/walmart_open/version.rb +1 -1
  12. data/spec/walmart_open/client_spec.rb +0 -18
  13. data/spec/walmart_open/config_spec.rb +2 -9
  14. data/spec/walmart_open/connection_manager_spec.rb +3 -10
  15. metadata +2 -27
  16. data/lib/walmart_open/auth_token.rb +0 -25
  17. data/lib/walmart_open/commerce_request.rb +0 -23
  18. data/lib/walmart_open/order.rb +0 -55
  19. data/lib/walmart_open/order_item.rb +0 -17
  20. data/lib/walmart_open/order_results.rb +0 -40
  21. data/lib/walmart_open/order_xml_builder.rb +0 -62
  22. data/lib/walmart_open/ordered_item.rb +0 -31
  23. data/lib/walmart_open/product_request.rb +0 -28
  24. data/lib/walmart_open/requests/place_order.rb +0 -58
  25. data/lib/walmart_open/requests/token.rb +0 -30
  26. data/lib/walmart_open/shipping_address.rb +0 -46
  27. data/spec/walmart_open/auth_token_spec.rb +0 -53
  28. data/spec/walmart_open/order_item_spec.rb +0 -43
  29. data/spec/walmart_open/order_results_spec.rb +0 -83
  30. data/spec/walmart_open/order_spec.rb +0 -130
  31. data/spec/walmart_open/requests/place_order_spec.rb +0 -151
  32. data/spec/walmart_open/requests/token_spec.rb +0 -49
  33. data/spec/walmart_open/shipping_address_spec.rb +0 -142
data/README.md CHANGED
@@ -6,15 +6,6 @@
6
6
 
7
7
  To use the Products API, you must register for a Walmart Open account and obtain an API key. You can also API calls from their [interacive API tool](https://developer.walmartlabs.com/io-docs). For documentation can be found here: https://developer.walmartlabs.com/docs.
8
8
 
9
- ## Commerce API
10
-
11
- As of right now, the Commerce API is [only available to select partners](https://developer.walmartlabs.com/forum/read/162059). Once you've established a relationship with them, you will need the following to access the Commerce API:
12
- - API Key
13
- - API Secret
14
- - Generated Private/Public Key for message signing. You will need to give them your public key before proceeding.
15
- - A billing record id; Provided to you by Walmart when you give them your public key.
16
- - A [walmart.com](http://www.walmart.com) account that is connected to your Walmart Open account.
17
-
18
9
  ## Installation
19
10
 
20
11
  Add this line to your application's Gemfile:
@@ -42,16 +33,7 @@ client = WalmartOpen::Client.new do |config|
42
33
  # This value defaults to 5.
43
34
  config.product_calls_per_second = 4
44
35
 
45
- ## Commerce API
46
- config.commerce_api_key = "7XQLSmhqTXJHQ5xdGG7ZUh8d"
47
- config.commerce_api_secret = "Mm5BX4c7QC"
48
- config.commerce_private_key = File.read("/path/to/your/private/key/file")
49
-
50
- # This value defaults to 2.
51
- config.commerce_calls_per_second = 1
52
-
53
36
  # Set this to true for development mode.
54
- # This mainly applies to the Commerce API; orders will not actually be placed.
55
37
  config.debug = true
56
38
  end
57
39
  ```
@@ -92,89 +74,6 @@ items = client.feed(:bestsellers, category_id)
92
74
  # when success: items is an array of WalmartOpen::Item items
93
75
  ```
94
76
 
95
- ### Making Commerce API Calls
96
- ```ruby
97
- # Placing an Order
98
- order = WalmartOpen::Order.new(billing_id: 1, partner_order_id: "38",
99
- partner_order_time: "16:15:47",
100
- first_name: "James", last_name: "Fong",
101
- street1: "200 Blossom Ln",
102
- street2: "100 Flynn Avenue",
103
- city: "Mountain View",
104
- state: "CA", zipcode: "94043", country: "USA")
105
- # required fields:
106
- # billing_id: long, first_name: string, street1: string, city: string,
107
- state: string, zipcode: string, country: string, phone: string
108
- # optional fields:
109
- # last_name: string
110
- # partner_order_time: string in format of HH:MM:SS, default to Time.now
111
- # partner_order_id: string, default to "Order-#{HASH_OF_RAND_AND_ORDER_TIME}"
112
-
113
- # add_item method 1, add_item(item_id, quantity = 1, item_price = nil, shipping_price = nil )
114
- order.add_item(10371356)
115
- order.add_item(25174174)
116
-
117
- # add_item method 2, add_item(item, quantity = 1)
118
- order.add_item(client.lookup(10371356))
119
- order.add_item(client.lookup(25174174))
120
-
121
- # order.valid? == true
122
-
123
- res = client.order(order)
124
- #=> OrderResults
125
- # when order succeeds, we see res as example below
126
- # res.error? = true
127
- # res.order_id = "2677911409503"
128
- # res.partner_order_id = "13"
129
- # res.items = [ ordered_item, ...]
130
- # res.total = "237.96" # sum of ordered_item.price for all ordered_items
131
- # res.sales_tax = "20.82"
132
- # res.surcharge = "0.0"
133
- # res.raw_attributes = {"response"=>{"orderId"=>"2677914395453", "partnerOrderId"=>"41", "items"=>{"item"=>{"itemId"=>"10371356", "quantity"=>"1", "itemPrice"=>"22.97"}}, "total"=>"29.95", "itemTotal"=>"22.97", "shipping"=>"4.97", "salesTax"=>"2.01", "surcharge"=>"0.00"}}
134
-
135
- # Note that order_item have class OrderedItem. As an example
136
- # ordered_item.id = 25174174
137
- # ordered_item.quantity = 1
138
- # ordered_item.price = 214.99
139
- # ordered_item.raw_attributes = {"itemId"=>"25174174", "quantity"=>"1", "itemPrice"=>"214.99"}
140
-
141
- # when order fails, we see res as below
142
- # res.error? == true
143
- # res.error = {:code=>"10001", :message=>"Invalid xml"}
144
- # res.error[:code] = "10001"
145
- # res.error[:message] = "Invalid xml"
146
- # res.raw_attributes = {"errors"=>{"error"=>{"code"=>"10001", "message"=>"Invalid xml"}}}
147
- ```
148
-
149
- # WalmartOpen::Item
150
- # example of a WalmartOpen::Item item
151
- # item.id = "15076191"
152
- # item.name = "Apple iPod Touch 4th Generation 32GB with Bonus Accessory Kit"
153
- # item.price = "189.0"
154
- # item.upc = nil
155
- # item.category_node = "3944_96469_164001"
156
- # item.short_description = "The world's most popular portable gaming device ... "
157
- # item.long_description = "<br><b>Apple iPod touch 32GB (4th Gen) ..."
158
- # item.brand = nil
159
- # item.shipping_rate = 0.0
160
- # item.size = nil
161
- # item.color = "Black"
162
- # item.model_number = ?
163
- # item.url = "http://www.walmart.com/ip/Apple-iPod-Touch-8GB-32GB-and-64GB-newest-model/15076191"
164
- # item.raw_attributes = {"itemId" => 15076191, .....}
165
- # item.large_image = "http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_500X500.jpg"
166
- # item.thumbnail_image = "http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_100X100.jpg"
167
- # item.medium_image = "http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_180X180.jpg"
168
-
169
- # item.variants returns an array of product ids when a walmart product has
170
- variants that user can choose, eg a different color or style. An example:
171
- # items.variants = [15076191, 15076192]
172
- # An empty array is returned when a product has no variants.
173
-
174
- ### Authentication failure
175
- In the case of authentication failure during an API call, a
176
- WalmartOpen::AuthenticationError exception will be raised
177
-
178
77
  ## Contributing
179
78
 
180
79
  1. Fork it
@@ -3,15 +3,12 @@ require "walmart_open/connection_manager"
3
3
  require "walmart_open/requests/search"
4
4
  require "walmart_open/requests/lookup"
5
5
  require "walmart_open/requests/taxonomy"
6
- require "walmart_open/requests/token"
7
- require "walmart_open/requests/place_order"
8
6
  require "walmart_open/requests/feed"
9
7
 
10
8
  module WalmartOpen
11
9
  class Client
12
10
  attr_reader :connection
13
11
  attr_reader :config
14
- attr_reader :auth_token
15
12
 
16
13
  def initialize(config_attrs = {})
17
14
  @config = Config.new(config_attrs)
@@ -37,19 +34,5 @@ module WalmartOpen
37
34
  params[:category_id] = category_id if category_id
38
35
  connection.request(Requests::Feed.new(type, params))
39
36
  end
40
-
41
- def order(order_info)
42
- authenticate!
43
-
44
- connection.request(Requests::PlaceOrder.new(order_info))
45
- end
46
-
47
- private
48
-
49
- def authenticate!
50
- if !@auth_token || @auth_token.expired?
51
- @auth_token = connection.request(Requests::Token.new)
52
- end
53
- end
54
37
  end
55
38
  end
@@ -6,14 +6,7 @@ module WalmartOpen
6
6
  :product_domain,
7
7
  :product_version,
8
8
  :product_api_key,
9
- :product_calls_per_second,
10
- :commerce_domain,
11
- :commerce_version,
12
- :commerce_api_key,
13
- :commerce_api_secret,
14
- :commerce_private_key,
15
- :commerce_private_key_password,
16
- :commerce_calls_per_second
9
+ :product_calls_per_second
17
10
 
18
11
  def initialize(options = {})
19
12
  # Default to production mode.
@@ -24,18 +17,9 @@ module WalmartOpen
24
17
  self.product_version = "v1"
25
18
  self.product_calls_per_second = 5
26
19
 
27
- # Set some defaults for Commerce API.
28
- self.commerce_domain = "api.walmartlabs.com"
29
- self.commerce_version = "v1"
30
- self.commerce_calls_per_second = 2
31
-
32
20
  options.each do |key, value|
33
21
  public_send("#{key}=", value)
34
22
  end
35
23
  end
36
-
37
- def private_key
38
- @private_key ||= OpenSSL::PKey.read(commerce_private_key, commerce_private_key_password)
39
- end
40
24
  end
41
25
  end
@@ -2,7 +2,7 @@ module WalmartOpen
2
2
  class ConnectionManager
3
3
  def initialize(client)
4
4
  @client = client
5
- @calls = {}
5
+ @calls = []
6
6
  end
7
7
 
8
8
  def request(request_obj)
@@ -14,14 +14,12 @@ module WalmartOpen
14
14
  private
15
15
 
16
16
  def throttle(request_obj)
17
- type = request_type(request_obj)
18
- @calls[type] ||= []
19
- calls = @calls[type]
17
+ calls = @calls
20
18
 
21
19
  now = Time.now
22
20
  calls.delete_if { |time| now.to_f - time.to_f >= 1 }
23
21
 
24
- if calls.size >= calls_per_second(type)
22
+ if calls.size >= calls_per_second
25
23
  sleep((calls.first + 1) - now)
26
24
  end
27
25
 
@@ -32,16 +30,8 @@ module WalmartOpen
32
30
  ret_val
33
31
  end
34
32
 
35
- def request_type(request_obj)
36
- case request_obj
37
- when ProductRequest then :product
38
- when CommerceRequest then :commerce
39
- else raise "Unknown request type"
40
- end
41
- end
42
-
43
- def calls_per_second(type)
44
- @client.config.public_send("#{type}_calls_per_second")
33
+ def calls_per_second
34
+ @client.config.public_send("product_calls_per_second")
45
35
  end
46
36
  end
47
37
  end
@@ -5,12 +5,6 @@ module WalmartOpen
5
5
  class ServerError < WalmartOpenError
6
6
  end
7
7
 
8
- class AuthenticationError < WalmartOpenError
9
- end
10
-
11
8
  class ItemNotFoundError < WalmartOpenError
12
9
  end
13
-
14
- class OrderError < WalmartOpenError
15
- end
16
10
  end
@@ -4,7 +4,7 @@ require "walmart_open/errors"
4
4
 
5
5
  module WalmartOpen
6
6
  class Request
7
- attr_reader :path, :params
7
+ attr_accessor :path, :params
8
8
 
9
9
  def submit(client)
10
10
  raise "@path must be specified" unless path
@@ -16,22 +16,29 @@ module WalmartOpen
16
16
 
17
17
  private
18
18
 
19
- attr_writer :path, :params
20
-
21
19
  def request_method
22
20
  :get
23
21
  end
24
22
 
25
23
  def build_url(client)
26
- raise NotImplementedError, "build_url must be implemented by subclass"
24
+ url = "https://#{client.config.product_domain}"
25
+ url << "/#{client.config.product_version}"
26
+ url << "/#{path}"
27
+ url << params_to_query_string(build_params(client))
27
28
  end
28
29
 
29
30
  def build_params(client)
30
- # noop
31
+ {
32
+ format: "json",
33
+ api_key: client.config.product_api_key
34
+ }.merge(params || {})
31
35
  end
32
36
 
37
+ # Walmart API unofficially supports HTTPS so we rather hit that instead of
38
+ # HTTP. However, their SSL certificate is unverifiable so we have to tell
39
+ # HTTParty not to verify (otherwise it will complain).
33
40
  def request_options(client)
34
- {}
41
+ { verify: false }
35
42
  end
36
43
 
37
44
  # Subclasses can override this method to return a different response.
@@ -1,8 +1,8 @@
1
- require "walmart_open/product_request"
1
+ require "walmart_open/request"
2
2
 
3
3
  module WalmartOpen
4
4
  module Requests
5
- class Feed < ProductRequest
5
+ class Feed < Request
6
6
  TYPES = [
7
7
  :preorder,
8
8
  :bestsellers,
@@ -1,10 +1,10 @@
1
- require "walmart_open/product_request"
1
+ require "walmart_open/request"
2
2
  require "walmart_open/item"
3
3
  require "walmart_open/errors"
4
4
 
5
5
  module WalmartOpen
6
6
  module Requests
7
- class Lookup < ProductRequest
7
+ class Lookup < Request
8
8
  def initialize(item_id, params = {})
9
9
  self.path = "items/#{item_id}"
10
10
  self.params = params
@@ -1,9 +1,9 @@
1
- require "walmart_open/product_request"
1
+ require "walmart_open/request"
2
2
  require "walmart_open/search_results"
3
3
 
4
4
  module WalmartOpen
5
5
  module Requests
6
- class Search < ProductRequest
6
+ class Search < Request
7
7
  def initialize(query, params = {})
8
8
  self.path = "search"
9
9
  self.params = params.merge(query: query)
@@ -1,8 +1,8 @@
1
- require "walmart_open/product_request"
1
+ require "walmart_open/request"
2
2
 
3
3
  module WalmartOpen
4
4
  module Requests
5
- class Taxonomy < ProductRequest
5
+ class Taxonomy < Request
6
6
  def initialize
7
7
  self.path = "taxonomy"
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module WalmartOpen
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -4,8 +4,6 @@ require "walmart_open/config"
4
4
  require "walmart_open/requests/search"
5
5
  require "walmart_open/requests/lookup"
6
6
  require "walmart_open/requests/taxonomy"
7
- require "walmart_open/requests/token"
8
- require "walmart_open/requests/place_order"
9
7
  require "walmart_open/requests/feed"
10
8
 
11
9
  describe WalmartOpen::Client do
@@ -91,20 +89,4 @@ describe WalmartOpen::Client do
91
89
  client.feed(type, category_id)
92
90
  end
93
91
  end
94
-
95
- context "#order" do
96
- it "delegates the request and returns the response" do
97
- client = WalmartOpen::Client.new
98
- request = double
99
- order_info = double
100
- auth_token = double
101
-
102
- expect(WalmartOpen::Requests::PlaceOrder).to receive(:new).and_return(request)
103
- expect(WalmartOpen::Requests::Token).to receive(:new).and_return(auth_token)
104
- expect(client.connection).to receive(:request).with(auth_token).and_return(auth_token)
105
- expect(client.connection).to receive(:request).with(request)
106
-
107
- client.order(order_info)
108
- end
109
- end
110
92
  end
@@ -11,25 +11,18 @@ describe WalmartOpen::Config do
11
11
  expect(config.debug).to be(false)
12
12
  expect(config.product_domain).to eq("walmartlabs.api.mashery.com")
13
13
  expect(config.product_version).to eq("v1")
14
- expect(config.commerce_domain).to eq("api.walmartlabs.com")
15
- expect(config.commerce_version).to eq("v1")
16
14
  expect(config.product_calls_per_second).to be(5)
17
- expect(config.commerce_calls_per_second).to be(2)
18
15
  end
19
16
 
20
17
  it "allows setting configs" do
21
18
  config = WalmartOpen::Config.new({debug: true, product_domain: "test",
22
- commerce_domain: "test", product_version: "test",
23
- commerce_version: "test", product_calls_per_second: 1,
24
- commerce_calls_per_second: 1})
19
+ product_version: "test", product_calls_per_second: 1,
20
+ })
25
21
 
26
22
  expect(config.debug).to be(true)
27
23
  expect(config.product_domain).to eq("test")
28
24
  expect(config.product_version).to eq("test")
29
- expect(config.commerce_domain).to eq("test")
30
- expect(config.commerce_version).to eq("test")
31
25
  expect(config.product_calls_per_second).to be(1)
32
- expect(config.commerce_calls_per_second).to be(1)
33
26
  end
34
27
  end
35
28
  end
@@ -1,15 +1,14 @@
1
1
  require "spec_helper"
2
2
  require "walmart_open/connection_manager"
3
3
  require "walmart_open/client"
4
- require "walmart_open/product_request"
4
+ require "walmart_open/request"
5
5
  require "timecop"
6
6
 
7
7
  describe WalmartOpen::ConnectionManager do
8
8
  context "#request" do
9
9
  let(:client) do
10
10
  WalmartOpen::Client.new(
11
- product_calls_per_second: 1,
12
- commerce_calls_per_second: 1
11
+ product_calls_per_second: 1
13
12
  )
14
13
  end
15
14
 
@@ -44,13 +43,7 @@ describe WalmartOpen::ConnectionManager do
44
43
  end
45
44
 
46
45
  context "when product request" do
47
- let(:request) { WalmartOpen::ProductRequest.new }
48
-
49
- include_examples "a request"
50
- end
51
-
52
- context "when commerce request" do
53
- let(:request) { WalmartOpen::CommerceRequest.new }
46
+ let(:request) { WalmartOpen::Request.new }
54
47
 
55
48
  include_examples "a request"
56
49
  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.8
5
+ version: 0.1.0
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-03-17 00:00:00.000000000 Z
13
+ date: 2014-08-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
@@ -148,49 +148,31 @@ executables: []
148
148
  extensions: []
149
149
  extra_rdoc_files: []
150
150
  files:
151
- - lib/walmart_open/auth_token.rb
152
151
  - lib/walmart_open/client.rb
153
- - lib/walmart_open/commerce_request.rb
154
152
  - lib/walmart_open/config.rb
155
153
  - lib/walmart_open/connection_manager.rb
156
154
  - lib/walmart_open/errors.rb
157
155
  - lib/walmart_open/item.rb
158
- - lib/walmart_open/order.rb
159
- - lib/walmart_open/order_item.rb
160
- - lib/walmart_open/order_results.rb
161
- - lib/walmart_open/order_xml_builder.rb
162
- - lib/walmart_open/ordered_item.rb
163
- - lib/walmart_open/product_request.rb
164
156
  - lib/walmart_open/request.rb
165
157
  - lib/walmart_open/requests/feed.rb
166
158
  - lib/walmart_open/requests/lookup.rb
167
- - lib/walmart_open/requests/place_order.rb
168
159
  - lib/walmart_open/requests/search.rb
169
160
  - lib/walmart_open/requests/taxonomy.rb
170
- - lib/walmart_open/requests/token.rb
171
161
  - lib/walmart_open/search_results.rb
172
- - lib/walmart_open/shipping_address.rb
173
162
  - lib/walmart_open/stock_string.rb
174
163
  - lib/walmart_open/version.rb
175
164
  - lib/walmart_open.rb
176
165
  - spec/spec_helper.rb
177
- - spec/walmart_open/auth_token_spec.rb
178
166
  - spec/walmart_open/client_spec.rb
179
167
  - spec/walmart_open/config_spec.rb
180
168
  - spec/walmart_open/connection_manager_spec.rb
181
169
  - spec/walmart_open/item_spec.rb
182
- - spec/walmart_open/order_item_spec.rb
183
- - spec/walmart_open/order_results_spec.rb
184
- - spec/walmart_open/order_spec.rb
185
170
  - spec/walmart_open/request_spec.rb
186
171
  - spec/walmart_open/requests/feed_spec.rb
187
172
  - spec/walmart_open/requests/lookup_spec.rb
188
- - spec/walmart_open/requests/place_order_spec.rb
189
173
  - spec/walmart_open/requests/search_spec.rb
190
174
  - spec/walmart_open/requests/taxonomy_spec.rb
191
- - spec/walmart_open/requests/token_spec.rb
192
175
  - spec/walmart_open/search_results_spec.rb
193
- - spec/walmart_open/shipping_address_spec.rb
194
176
  - spec/walmart_open/stock_string_spec.rb
195
177
  - LICENSE.txt
196
178
  - Rakefile
@@ -222,21 +204,14 @@ specification_version: 3
222
204
  summary: Ruby implementation for Walmart Open API
223
205
  test_files:
224
206
  - spec/spec_helper.rb
225
- - spec/walmart_open/auth_token_spec.rb
226
207
  - spec/walmart_open/client_spec.rb
227
208
  - spec/walmart_open/config_spec.rb
228
209
  - spec/walmart_open/connection_manager_spec.rb
229
210
  - spec/walmart_open/item_spec.rb
230
- - spec/walmart_open/order_item_spec.rb
231
- - spec/walmart_open/order_results_spec.rb
232
- - spec/walmart_open/order_spec.rb
233
211
  - spec/walmart_open/request_spec.rb
234
212
  - spec/walmart_open/requests/feed_spec.rb
235
213
  - spec/walmart_open/requests/lookup_spec.rb
236
- - spec/walmart_open/requests/place_order_spec.rb
237
214
  - spec/walmart_open/requests/search_spec.rb
238
215
  - spec/walmart_open/requests/taxonomy_spec.rb
239
- - spec/walmart_open/requests/token_spec.rb
240
216
  - spec/walmart_open/search_results_spec.rb
241
- - spec/walmart_open/shipping_address_spec.rb
242
217
  - spec/walmart_open/stock_string_spec.rb