walmart_open 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +171 -0
  4. data/Rakefile +1 -0
  5. data/lib/walmart_open.rb +6 -0
  6. data/lib/walmart_open/auth_token.rb +25 -0
  7. data/lib/walmart_open/client.rb +48 -0
  8. data/lib/walmart_open/commerce_request.rb +23 -0
  9. data/lib/walmart_open/config.rb +41 -0
  10. data/lib/walmart_open/connection_manager.rb +47 -0
  11. data/lib/walmart_open/errors.rb +13 -0
  12. data/lib/walmart_open/item.rb +41 -0
  13. data/lib/walmart_open/order.rb +53 -0
  14. data/lib/walmart_open/order_item.rb +17 -0
  15. data/lib/walmart_open/order_results.rb +40 -0
  16. data/lib/walmart_open/order_xml_builder.rb +62 -0
  17. data/lib/walmart_open/ordered_item.rb +31 -0
  18. data/lib/walmart_open/product_request.rb +28 -0
  19. data/lib/walmart_open/request.rb +64 -0
  20. data/lib/walmart_open/requests/lookup.rb +27 -0
  21. data/lib/walmart_open/requests/place_order.rb +58 -0
  22. data/lib/walmart_open/requests/search.rb +19 -0
  23. data/lib/walmart_open/requests/taxonomy.rb +17 -0
  24. data/lib/walmart_open/requests/token.rb +30 -0
  25. data/lib/walmart_open/search_results.rb +25 -0
  26. data/lib/walmart_open/shipping_address.rb +36 -0
  27. data/lib/walmart_open/version.rb +3 -0
  28. data/spec/spec_helper.rb +16 -0
  29. data/spec/walmart_open/auth_token_spec.rb +53 -0
  30. data/spec/walmart_open/client_spec.rb +92 -0
  31. data/spec/walmart_open/config_spec.rb +35 -0
  32. data/spec/walmart_open/connection_manager_spec.rb +58 -0
  33. data/spec/walmart_open/item_spec.rb +52 -0
  34. data/spec/walmart_open/order_item_spec.rb +18 -0
  35. data/spec/walmart_open/order_results_spec.rb +83 -0
  36. data/spec/walmart_open/order_spec.rb +124 -0
  37. data/spec/walmart_open/request_spec.rb +79 -0
  38. data/spec/walmart_open/requests/lookup_spec.rb +91 -0
  39. data/spec/walmart_open/requests/place_order_spec.rb +149 -0
  40. data/spec/walmart_open/requests/search_spec.rb +86 -0
  41. data/spec/walmart_open/requests/taxonomy_spec.rb +69 -0
  42. data/spec/walmart_open/requests/token_spec.rb +49 -0
  43. data/spec/walmart_open/search_results_spec.rb +60 -0
  44. data/spec/walmart_open/shipping_address_spec.rb +63 -0
  45. metadata +204 -0
@@ -0,0 +1,25 @@
1
+ require "walmart_open/item"
2
+
3
+ module WalmartOpen
4
+ class SearchResults
5
+ attr_reader :query,
6
+ :total,
7
+ :items,
8
+ :start,
9
+ :page
10
+
11
+ def initialize(response)
12
+ @query = response["query"]
13
+ @total = response["totalResults"]
14
+ @start = response["start"]
15
+ # TODO: set the page!
16
+ # @page = ...
17
+
18
+ @items = []
19
+
20
+ response["items"].each do |item|
21
+ @items << Item.new(item)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ module WalmartOpen
2
+ class ShippingAddress
3
+ API_ATTRIBUTES_MAPPING = {
4
+ street1: "street1",
5
+ street2: "street2",
6
+ city: "city",
7
+ state: "state",
8
+ zipcode: "zipcode",
9
+ country: "country",
10
+ }
11
+
12
+ API_ATTRIBUTES_MAPPING.each_value do |attr_name|
13
+ attr_reader attr_name
14
+ end
15
+
16
+ attr_reader :raw_attributes
17
+
18
+ def initialize(params)
19
+ @raw_attributes = params
20
+ extract_known_attributes
21
+ end
22
+
23
+ def valid?
24
+ !!(street1 && city && state && zipcode && country)
25
+ end
26
+
27
+ private
28
+ def extract_known_attributes
29
+ API_ATTRIBUTES_MAPPING.each do |api_attr, attr|
30
+ next unless raw_attributes.has_key?(api_attr)
31
+
32
+ instance_variable_set("@#{attr}", raw_attributes[api_attr])
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module WalmartOpen
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "pathname"
2
+ ROOT_PATH = Pathname.new(__FILE__).join("../..").expand_path
3
+ $LOAD_PATH.unshift(ROOT_PATH.join("lib"))
4
+
5
+ RSpec.configure do |config|
6
+ # Run specs in random order to surface order dependencies. If you find an
7
+ # order dependency and want to debug it, you can fix the order by providing
8
+ # the seed, which is printed after each run.
9
+ # --seed 1234
10
+ config.order = "random"
11
+
12
+ # Disable the should syntax compeletely; we use the expect syntax only.
13
+ config.expect_with :rspec do |c|
14
+ c.syntax = :expect
15
+ end
16
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+ require "walmart_open/auth_token"
3
+ require "timecop"
4
+
5
+ describe WalmartOpen::AuthToken do
6
+ let(:auth_token_attrs) do
7
+ {
8
+ "token_type" => "bearer",
9
+ "mapi" => "8tbvkxd6gu6zjzp6qbyeewb6",
10
+ "access_token" => "k5pzg6jqtetygmrkm5y6qqnr",
11
+ "expires_in" => 600
12
+ }
13
+ end
14
+
15
+ let(:auth_token) do
16
+ WalmartOpen::AuthToken.new(auth_token_attrs, Time.now)
17
+ end
18
+
19
+ context ".new" do
20
+ it "initializes with attributes" do
21
+ Timecop.freeze(Time.now) do
22
+ expect(auth_token.expiration_time).to eq(Time.now + auth_token_attrs["expires_in"])
23
+ expect(auth_token.token_type).to eq(auth_token_attrs["token_type"])
24
+ expect(auth_token.access_token).to eq(auth_token_attrs["access_token"])
25
+ expect(auth_token.time).to eq(Time.now)
26
+ end
27
+ end
28
+ end
29
+
30
+ context "#expired?" do
31
+ it "returns true when auth token has expired" do
32
+ Timecop.freeze(Time.now) do
33
+ auth_token
34
+
35
+ Timecop.freeze(Time.now + auth_token_attrs["expires_in"] + WalmartOpen::AuthToken::BUFFER_TIME) do
36
+ expect(auth_token).to be_expired
37
+ end
38
+ end
39
+ end
40
+
41
+ it "returns false when auth token has not expired" do
42
+ Timecop.freeze(Time.now) do
43
+ expect(auth_token).not_to be_expired
44
+ end
45
+ end
46
+ end
47
+
48
+ context "#authorization_header" do
49
+ it "returns authentication_header" do
50
+ expect(auth_token.authorization_header).to eq("Bearer k5pzg6jqtetygmrkm5y6qqnr")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,92 @@
1
+ require "spec_helper"
2
+ require "walmart_open/client"
3
+ require "walmart_open/config"
4
+ require "walmart_open/requests/search"
5
+ require "walmart_open/order"
6
+
7
+ describe WalmartOpen::Client do
8
+ context ".new" do
9
+ context "when block is given" do
10
+ it "yields the config" do
11
+ yielded_config = nil
12
+
13
+ client = WalmartOpen::Client.new do |config|
14
+ yielded_config = config
15
+ end
16
+
17
+ expect(yielded_config).to be_kind_of(WalmartOpen::Config)
18
+ expect(yielded_config).to eq(client.config)
19
+ end
20
+ end
21
+
22
+ context "when block is not given" do
23
+ it "does not complain" do
24
+ client = WalmartOpen::Client.new
25
+ end
26
+ end
27
+ end
28
+
29
+ context "#search" do
30
+ it "delegates the request and returns the response" do
31
+ client = WalmartOpen::Client.new
32
+ query = double
33
+ params = double
34
+ request = double
35
+
36
+ expect(WalmartOpen::Requests::Search).to receive(:new) do |query_arg, params_arg|
37
+ expect(query_arg).to eq(query)
38
+ expect(params_arg).to eq(params)
39
+ request
40
+ end
41
+ expect(client.connection).to receive(:request).with(request)
42
+
43
+ client.search(query, params)
44
+ end
45
+ end
46
+
47
+ context "#lookup" do
48
+ it "delegates the request and returns the response" do
49
+ client = WalmartOpen::Client.new
50
+ item_id = double
51
+ params = double
52
+ request = double
53
+
54
+ expect(WalmartOpen::Requests::Lookup).to receive(:new) do |item_id_arg, params_arg|
55
+ expect(item_id_arg).to eq(item_id)
56
+ expect(params_arg).to eq(params)
57
+ request
58
+ end
59
+ expect(client.connection).to receive(:request).with(request)
60
+
61
+ client.lookup(item_id, params)
62
+ end
63
+ end
64
+
65
+ context "#taxonomy" do
66
+ it "delegates the request and returns the response" do
67
+ client = WalmartOpen::Client.new
68
+ request = double
69
+
70
+ expect(WalmartOpen::Requests::Taxonomy).to receive(:new).and_return(request)
71
+ expect(client.connection).to receive(:request).with(request)
72
+
73
+ client.taxonomy
74
+ end
75
+ end
76
+
77
+ context "#order" do
78
+ it "delegates the request and returns the response" do
79
+ client = WalmartOpen::Client.new
80
+ request = double
81
+ order_info = double
82
+ auth_token = double
83
+
84
+ expect(WalmartOpen::Requests::PlaceOrder).to receive(:new).and_return(request)
85
+ expect(WalmartOpen::Requests::Token).to receive(:new).and_return(auth_token)
86
+ expect(client.connection).to receive(:request).with(auth_token).and_return(auth_token)
87
+ expect(client.connection).to receive(:request).with(request)
88
+
89
+ client.order(order_info)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+ require "walmart_open/config"
3
+ require "walmart_open/client"
4
+ require "walmart_open/errors"
5
+
6
+ describe WalmartOpen::Config do
7
+ context ".new" do
8
+ it "sets default configs" do
9
+ config = WalmartOpen::Config.new
10
+
11
+ expect(config.debug).to be(false)
12
+ expect(config.product_domain).to eq("walmartlabs.api.mashery.com")
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
+ expect(config.product_calls_per_second).to be(5)
17
+ expect(config.commerce_calls_per_second).to be(2)
18
+ end
19
+
20
+ it "allows setting configs" do
21
+ 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})
25
+
26
+ expect(config.debug).to be(true)
27
+ expect(config.product_domain).to eq("test")
28
+ expect(config.product_version).to eq("test")
29
+ expect(config.commerce_domain).to eq("test")
30
+ expect(config.commerce_version).to eq("test")
31
+ expect(config.product_calls_per_second).to be(1)
32
+ expect(config.commerce_calls_per_second).to be(1)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+ require "walmart_open/connection_manager"
3
+ require "walmart_open/client"
4
+ require "walmart_open/product_request"
5
+ require "timecop"
6
+
7
+ describe WalmartOpen::ConnectionManager do
8
+ context "#request" do
9
+ let(:client) do
10
+ WalmartOpen::Client.new(
11
+ product_calls_per_second: 1,
12
+ commerce_calls_per_second: 1
13
+ )
14
+ end
15
+
16
+ let(:connection_manager) { client.connection }
17
+
18
+ shared_examples "a request" do
19
+ it "does not sleep when under calls per second threshold" do
20
+ expect(request).to receive(:submit).twice
21
+ expect(connection_manager).not_to receive(:sleep)
22
+
23
+ Timecop.freeze(Time.now) do
24
+ connection_manager.request(request)
25
+
26
+ Timecop.freeze(Time.now + 1) do
27
+ connection_manager.request(request)
28
+ end
29
+ end
30
+ end
31
+
32
+ it "sleeps when exceeds calls per second threshold" do
33
+ expect(request).to receive(:submit).at_least(:once)
34
+ expect(connection_manager).to receive(:sleep).with(0.5)
35
+
36
+ Timecop.freeze(Time.now) do
37
+ connection_manager.request(request)
38
+
39
+ Timecop.freeze(Time.now + 0.5) do
40
+ connection_manager.request(request)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ 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 }
54
+
55
+ include_examples "a request"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+ require "walmart_open/item"
3
+
4
+ describe WalmartOpen::Item do
5
+ let(:item_attrs) do
6
+ {
7
+ "itemId" => 10371356,
8
+ "parentItemId" => 10371356,
9
+ "name" => "Microplush Blanket",
10
+ "salePrice" => 22.97,
11
+ "upc" => "801418711959",
12
+ "categoryPath" => "Home/Bedding/Blankets & Throws",
13
+ "shortDescription" => "Cuddle up in bed, on the couch, or even on a sandy beach with this super-soft and thick microfiber blanket. It's luxuriously crafted to provide sumptuous warmth and comfort, even in the coldest environment. Constructed from 100% microfiber, the blanket yarns feel plush and cozy against your skin. This lavish microplush blanket is available in 7 beautiful colors, and can be elegantly matched to any home decor. It works well as an outdoor blanket too &mdash; it's perfect for picnics, long car rides, and trips to the ballpark. Plus, this microplush blanket is easy to take care of without hassle, and saves valuable energy over electric blankets. Now you can bundle up and stay warm anywhere, indoors or out. &lt;p&gt;Soft microplush blanket is available in Twin, Full, Queen and King Sizes.&lt;/p&gt;", "longDescription"=>"&lt;p&gt;&lt;b&gt;Microplush Blanket, 7 Colors:&lt;/b&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Super-soft, thick and plush&lt;/li&gt;&lt;li&gt;100% microfiber blanket yarns are soft and plush to the touch&lt;/li&gt;&lt;li&gt;Microplush blanket is suitable for all-year use, and can be used indoors or outdoors without hassle&lt;/li&gt;&lt;li&gt;Luxurious microplush blanket is easy to clean just machine wash and tumble dry&lt;/li&gt;&lt;li&gt;Twin: 90&quot; x 66&quot;&lt;/li&gt;&lt;li&gt;Full: 90&quot; x 80&quot;&lt;/li&gt;&lt;li&gt;Queen: 90&quot; x 90&quot;&lt;/li&gt;&lt;li&gt;King: 90&quot; x 102&quot;&lt;/li&gt;&lt;/ul&gt;",
14
+ "brandName" => "Sun Yin",
15
+ "thumbnailImage" => "http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_100X100.jpg",
16
+ "mediumImage" => "http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_180X180.jpg", "largeImage"=>"http://i.walmartimages.com/i/p/00/80/14/18/71/0080141871195_Color_Burgundy_SW_500X500.jpg",
17
+ "productTrackingUrl" => "http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1081&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FMicroplush-Blanket%252F10371356%253Faffilsrc%253Dapi",
18
+ "ninetySevenCentShipping" => false,
19
+ "standardShipRate" => 4.97,
20
+ "twoThreeDayShippingRate" => 10.97,
21
+ "overnightShippingRate" => 14.97,
22
+ "size" => "Twin",
23
+ "color" => "Burgundy",
24
+ "marketplace" => false,
25
+ "shipToStore" => true,
26
+ "freeShipToStore" => true,
27
+ "modelNumber" => "BKT051166T",
28
+ "productUrl" => "http://www.walmart.com/ip/Microplush-Blanket/10371356",
29
+ "customerRating" => "4.446",
30
+ "numReviews" => 621,
31
+ "customerRatingImage" => "http://i2.walmartimages.com/i/CustRating/4_4.gif",
32
+ "categoryNode" => "4044_539103_4756",
33
+ "rollBack" => false,
34
+ "bundle" => false,
35
+ "clearance" => false,
36
+ "preOrder" => false,
37
+ "stock" => "Available",
38
+ "availableOnline" => true
39
+ }
40
+ end
41
+
42
+ context ".new" do
43
+ it "sets attributes" do
44
+ item = WalmartOpen::Item.new(item_attrs)
45
+
46
+ WalmartOpen::Item::API_ATTRIBUTES_MAPPING.each do |key, value|
47
+ expect(item.send(value)).to eq(item_attrs[key])
48
+ end
49
+ expect(item.raw_attributes).to eq(item_attrs)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+ require "walmart_open/order_item"
3
+
4
+ describe WalmartOpen::OrderItem do
5
+ context ".new" do
6
+ it "sets value correctly" do
7
+ item_id = 10371356
8
+ item_price = 1.23
9
+ order_item = WalmartOpen::OrderItem.new(item_id, item_price)
10
+
11
+ expect(order_item.item_id).to eq(item_id)
12
+ expect(order_item.item_price).to eq(item_price)
13
+ expect(order_item.shipping_price).to eq(0)
14
+ expect(order_item.quantity).to eq(1)
15
+ expect(order_item).to be_valid
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+ require "walmart_open/order_results"
3
+
4
+ describe WalmartOpen::OrderResults do
5
+ let(:order_results_1_item_attrs) do
6
+ {
7
+ "response" => {
8
+ "orderId" =>"2677921715556",
9
+ "partnerOrderId" =>"43",
10
+ "items" => {
11
+ "item" => {
12
+ "itemId"=>"10371356",
13
+ "quantity"=>"1",
14
+ "itemPrice"=>"22.97"
15
+ }
16
+ },
17
+ "total" => "29.95",
18
+ "itemTotal" => "22.97",
19
+ "shipping" =>"4.97",
20
+ "salesTax" =>"2.01",
21
+ "surcharge" =>"0.00"
22
+ }
23
+ }
24
+ end
25
+
26
+ let(:order_results_2_item_attrs) do
27
+ {
28
+ "response" => {
29
+ "orderId" => "2677922016720",
30
+ "partnerOrderId" => "44",
31
+ "items" => {
32
+ "item" => [
33
+ {
34
+ "itemId" => "20658394",
35
+ "quantity" => "1",
36
+ "itemPrice" => "12.99"
37
+ },
38
+ {
39
+ "itemId" => "10371356",
40
+ "quantity" => "1",
41
+ "itemPrice" => "22.97"
42
+ }
43
+ ]
44
+ },
45
+ "total" => "39.11",
46
+ "itemTotal" => "35.96",
47
+ "shipping" => "0",
48
+ "salesTax" => "3.15",
49
+ "surcharge" => "0.00"
50
+ }
51
+ }
52
+ end
53
+
54
+ context ".new" do
55
+ it "sets value correctly with one item" do
56
+ res = WalmartOpen::OrderResults.new(order_results_1_item_attrs)
57
+
58
+ expect(res.order_id).to eq(order_results_1_item_attrs["response"]["orderId"])
59
+ expect(res.partner_order_id).to eq(order_results_1_item_attrs["response"]["partnerOrderId"])
60
+ expect(res.total).to eq(order_results_1_item_attrs["response"]["itemTotal"])
61
+ expect(res.shipping).to eq(order_results_1_item_attrs["response"]["shipping"])
62
+ expect(res.sales_tax).to eq(order_results_1_item_attrs["response"]["salesTax"])
63
+ expect(res.surcharge).to eq(order_results_1_item_attrs["response"]["surcharge"])
64
+ expect(res.raw_attributes).to eq(order_results_1_item_attrs)
65
+ expect(res.items.count).to eq(1)
66
+ expect(res).not_to be_error
67
+ end
68
+
69
+ it "sets value correctly with multiple items" do
70
+ res = WalmartOpen::OrderResults.new(order_results_2_item_attrs)
71
+
72
+ expect(res.order_id).to eq(order_results_2_item_attrs["response"]["orderId"])
73
+ expect(res.partner_order_id).to eq(order_results_2_item_attrs["response"]["partnerOrderId"])
74
+ expect(res.total).to eq(order_results_2_item_attrs["response"]["itemTotal"])
75
+ expect(res.shipping).to eq(order_results_2_item_attrs["response"]["shipping"])
76
+ expect(res.sales_tax).to eq(order_results_2_item_attrs["response"]["salesTax"])
77
+ expect(res.surcharge).to eq(order_results_2_item_attrs["response"]["surcharge"])
78
+ expect(res.raw_attributes).to eq(order_results_2_item_attrs)
79
+ expect(res.items.count).to eq(2)
80
+ expect(res).not_to be_error
81
+ end
82
+ end
83
+ end