shopify_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +30 -0
  6. data/Rakefile +11 -0
  7. data/lib/shopify_client.rb +10 -0
  8. data/lib/shopify_client/api/custom_collection.rb +22 -0
  9. data/lib/shopify_client/api/order.rb +22 -0
  10. data/lib/shopify_client/api/product.rb +21 -0
  11. data/lib/shopify_client/api/recurring_application_charge.rb +43 -0
  12. data/lib/shopify_client/api/script_tag.rb +35 -0
  13. data/lib/shopify_client/api/shop.rb +16 -0
  14. data/lib/shopify_client/api/smart_collection.rb +22 -0
  15. data/lib/shopify_client/api/webhook.rb +34 -0
  16. data/lib/shopify_client/base.rb +57 -0
  17. data/lib/shopify_client/client.rb +91 -0
  18. data/lib/shopify_client/custom_collection.rb +28 -0
  19. data/lib/shopify_client/error.rb +13 -0
  20. data/lib/shopify_client/error/client_error.rb +23 -0
  21. data/lib/shopify_client/error/server_error.rb +15 -0
  22. data/lib/shopify_client/image.rb +13 -0
  23. data/lib/shopify_client/order.rb +29 -0
  24. data/lib/shopify_client/product.rb +22 -0
  25. data/lib/shopify_client/recurring_application_charge.rb +20 -0
  26. data/lib/shopify_client/request/content_type.rb +20 -0
  27. data/lib/shopify_client/response/parse_json.rb +22 -0
  28. data/lib/shopify_client/response/raise_error.rb +23 -0
  29. data/lib/shopify_client/script_tag.rb +19 -0
  30. data/lib/shopify_client/shop.rb +22 -0
  31. data/lib/shopify_client/smart_collection.rb +28 -0
  32. data/lib/shopify_client/version.rb +3 -0
  33. data/lib/shopify_client/webhook.rb +21 -0
  34. data/shopify_client.gemspec +28 -0
  35. data/test/fixtures/custom_collection.json +18 -0
  36. data/test/fixtures/custom_collections.json +19 -0
  37. data/test/fixtures/order.json +274 -0
  38. data/test/fixtures/orders.json +276 -0
  39. data/test/fixtures/product.json +140 -0
  40. data/test/fixtures/products.json +170 -0
  41. data/test/fixtures/recurring_application_charge.json +18 -0
  42. data/test/fixtures/recurring_application_charges.json +34 -0
  43. data/test/fixtures/script_tag.json +9 -0
  44. data/test/fixtures/script_tags.json +18 -0
  45. data/test/fixtures/shop.json +37 -0
  46. data/test/fixtures/smart_collection.json +25 -0
  47. data/test/fixtures/smart_collections.json +26 -0
  48. data/test/fixtures/webhook.json +10 -0
  49. data/test/fixtures/webhooks.json +20 -0
  50. data/test/shopify_client/api/custom_collection_spec.rb +54 -0
  51. data/test/shopify_client/api/order_spec.rb +68 -0
  52. data/test/shopify_client/api/product_spec.rb +66 -0
  53. data/test/shopify_client/api/recurring_application_charge_spec.rb +138 -0
  54. data/test/shopify_client/api/script_tag_spec.rb +109 -0
  55. data/test/shopify_client/api/shop_spec.rb +30 -0
  56. data/test/shopify_client/api/smart_collection_spec.rb +54 -0
  57. data/test/shopify_client/api/webhook_spec.rb +109 -0
  58. data/test/shopify_client/base_spec.rb +12 -0
  59. data/test/shopify_client/client_spec.rb +12 -0
  60. data/test/shopify_client/custom_collection_spec.rb +41 -0
  61. data/test/shopify_client/error/client_error_spec.rb +19 -0
  62. data/test/shopify_client/error/server_error_spec.rb +20 -0
  63. data/test/shopify_client/order_spec.rb +30 -0
  64. data/test/shopify_client/product_spec.rb +21 -0
  65. data/test/shopify_client/recurring_application_charge_spec.rb +20 -0
  66. data/test/shopify_client/script_tag_spec.rb +22 -0
  67. data/test/shopify_client/shop_spec.rb +14 -0
  68. data/test/shopify_client/smart_collection_spec.rb +41 -0
  69. data/test/shopify_client/webhook_spec.rb +22 -0
  70. data/test/shopify_client_spec.rb +10 -0
  71. data/test/test_helper.rb +13 -0
  72. metadata +235 -0
@@ -0,0 +1,109 @@
1
+ describe ShopifyClient::API::ScriptTag do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "#script_tags" do
8
+ it "requests all script_tags" do
9
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/script_tags.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 200, body: '')
12
+
13
+ @client.script_tags
14
+
15
+ assert_requested stubbed
16
+ end
17
+
18
+ it "filters by topic" do
19
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/script_tags.json?topic=orders/create").
20
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
21
+ to_return(status: 200, body: '')
22
+
23
+ @client.script_tags(:topic => 'orders/create')
24
+
25
+ assert_requested stubbed
26
+ end
27
+
28
+
29
+ it "returns an ShopifyClient::ScriptTag array" do
30
+ stub_request(:get, "https://example.myshopify.com/admin/script_tags.json").
31
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
32
+ to_return(status: 200, body: fixture('script_tags.json'))
33
+
34
+ script_tags = @client.script_tags
35
+
36
+ script_tags.size.must_equal 2
37
+ script_tags.first.must_be_instance_of ShopifyClient::ScriptTag
38
+ end
39
+
40
+ end
41
+
42
+ describe "#script_tag" do
43
+ it "requests an specific script_tag" do
44
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/script_tags/123.json").
45
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
46
+ to_return(status: 200, body: '')
47
+
48
+ @client.script_tag(123)
49
+
50
+ assert_requested stubbed
51
+ end
52
+
53
+ it "returns a ShopifyClient::ScriptTag" do
54
+ stub_request(:get, "https://example.myshopify.com/admin/script_tags/123.json").
55
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
56
+ to_return(status: 200, body: fixture('script_tag.json'))
57
+
58
+ script_tag = @client.script_tag(123)
59
+
60
+ script_tag.must_be_instance_of ShopifyClient::ScriptTag
61
+ script_tag.src.must_equal "http://js-aplenty.com/foo.js"
62
+ end
63
+ end
64
+
65
+ describe "#create_script_tag" do
66
+ before do
67
+ @attributes = { event: "onload", src: "http://js-aplenty.com/foo.js" }
68
+ @request = stub_request(:post, "https://example.myshopify.com/admin/script_tags.json").
69
+ with(headers: { 'X-Shopify-Access-Token' => "token" },
70
+ body: { script_tag: @attributes }).
71
+ to_return(status: 200, body: fixture('script_tag.json'))
72
+ end
73
+
74
+ it "posts with script_tag params" do
75
+ @client.create_script_tag(@attributes)
76
+
77
+ assert_requested @request
78
+ end
79
+
80
+ it "returns an initialized ScriptTag" do
81
+ script_tag = @client.create_script_tag(@attributes)
82
+
83
+ script_tag.must_be_instance_of ShopifyClient::ScriptTag
84
+ script_tag.id.must_equal 596726825
85
+ end
86
+
87
+ end
88
+
89
+ describe "#destroy_script_tag" do
90
+ before do
91
+ @request = stub_request(:delete, "https://example.myshopify.com/admin/script_tags/123.json").
92
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
93
+ to_return(status: 200)
94
+ end
95
+
96
+ it "deletes to destroy a script_tag" do
97
+ @client.destroy_script_tag(123)
98
+
99
+ assert_requested @request
100
+ end
101
+
102
+ it "can receive a ShopifyClient::ScriptTag object" do
103
+ @client.destroy_script_tag(ShopifyClient::ScriptTag.new(id: 123))
104
+
105
+ assert_requested @request
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,30 @@
1
+ describe ShopifyClient::API::Shop do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "#shop" do
8
+ it "requests the current shop" do
9
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/shop.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 200, body: '')
12
+
13
+ @client.shop
14
+
15
+ assert_requested stubbed
16
+ end
17
+
18
+ it "returns a ShopifyClient::Shop instance with data" do
19
+ stub_request(:get, "https://example.myshopify.com/admin/shop.json").
20
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
21
+ to_return(status: 200, body: fixture('shop.json'))
22
+
23
+ shop = @client.shop
24
+
25
+ shop.must_be_instance_of ShopifyClient::Shop
26
+ shop.name.must_equal "Apple Computers"
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,54 @@
1
+ describe ShopifyClient::API::SmartCollection do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "#smart_collections" do
8
+ it "requests all smart collections" do
9
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/smart_collections.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 200, body: '')
12
+
13
+ @client.smart_collections
14
+
15
+ assert_requested stubbed
16
+ end
17
+
18
+ it "returns an ShopifyClient::SmartCollection array" do
19
+ stub_request(:get, "https://example.myshopify.com/admin/smart_collections.json").
20
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
21
+ to_return(status: 200, body: fixture('smart_collections.json'))
22
+
23
+ smart_collections = @client.smart_collections
24
+
25
+ smart_collections.size.must_equal 1
26
+ smart_collections.first.must_be_instance_of ShopifyClient::SmartCollection
27
+ end
28
+ end
29
+
30
+ describe "#smart_collection" do
31
+ it "requests an specific smart collection" do
32
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/smart_collections/123.json").
33
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
34
+ to_return(status: 200, body: '')
35
+
36
+ @client.smart_collection(123)
37
+
38
+ assert_requested stubbed
39
+ end
40
+
41
+ it "returns a ShopifyClient::SmartCollection with data" do
42
+ stub_request(:get, "https://example.myshopify.com/admin/smart_collections/123.json").
43
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
44
+ to_return(status: 200, body: fixture('smart_collection.json'))
45
+
46
+ smart_collection = @client.smart_collection(123)
47
+
48
+ smart_collection.must_be_instance_of ShopifyClient::SmartCollection
49
+ smart_collection.handle.must_equal 'smart-ipods'
50
+ end
51
+ end
52
+
53
+ end
54
+
@@ -0,0 +1,109 @@
1
+ describe ShopifyClient::API::Webhook do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "#webhooks" do
8
+ it "requests all webhooks" do
9
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/webhooks.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 200, body: '')
12
+
13
+ @client.webhooks
14
+
15
+ assert_requested stubbed
16
+ end
17
+
18
+ it "filters by topic" do
19
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/webhooks.json?topic=orders/create").
20
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
21
+ to_return(status: 200, body: '')
22
+
23
+ @client.webhooks(:topic => 'orders/create')
24
+
25
+ assert_requested stubbed
26
+ end
27
+
28
+
29
+ it "returns an ShopifyClient::Webhook array" do
30
+ stub_request(:get, "https://example.myshopify.com/admin/webhooks.json").
31
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
32
+ to_return(status: 200, body: fixture('webhooks.json'))
33
+
34
+ webhooks = @client.webhooks
35
+
36
+ webhooks.size.must_equal 2
37
+ webhooks.first.must_be_instance_of ShopifyClient::Webhook
38
+ end
39
+
40
+ end
41
+
42
+ describe "#webhook" do
43
+ it "requests an specific webhook" do
44
+ stubbed = stub_request(:get, "https://example.myshopify.com/admin/webhooks/123.json").
45
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
46
+ to_return(status: 200, body: '')
47
+
48
+ @client.webhook(123)
49
+
50
+ assert_requested stubbed
51
+ end
52
+
53
+ it "returns a ShopifyClient::Webhook" do
54
+ stub_request(:get, "https://example.myshopify.com/admin/webhooks/123.json").
55
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
56
+ to_return(status: 200, body: fixture('webhook.json'))
57
+
58
+ webhook = @client.webhook(123)
59
+
60
+ webhook.must_be_instance_of ShopifyClient::Webhook
61
+ webhook.format.must_equal "json"
62
+ end
63
+ end
64
+
65
+ describe "#create_webhook" do
66
+ before do
67
+ @attributes = { :topic => "orders/create", :address => "http://whatever.hostname.com", :format => "json" }
68
+ @request = stub_request(:post, "https://example.myshopify.com/admin/webhooks.json").
69
+ with(headers: { 'X-Shopify-Access-Token' => "token" },
70
+ body: { webhook: @attributes }).
71
+ to_return(status: 200, body: fixture('webhook.json'))
72
+ end
73
+
74
+ it "posts with webhook params" do
75
+ @client.create_webhook(@attributes)
76
+
77
+ assert_requested @request
78
+ end
79
+
80
+ it "returns an initialized Webhook" do
81
+ webhook = @client.create_webhook(@attributes)
82
+
83
+ webhook.must_be_instance_of ShopifyClient::Webhook
84
+ webhook.id.must_equal 4759306
85
+ end
86
+
87
+ end
88
+
89
+ describe "#destroy_webhook" do
90
+ before do
91
+ @request = stub_request(:delete, "https://example.myshopify.com/admin/webhooks/123.json").
92
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
93
+ to_return(status: 200)
94
+ end
95
+
96
+ it "deletes to destroy a webhook" do
97
+ @client.destroy_webhook(123)
98
+
99
+ assert_requested @request
100
+ end
101
+
102
+ it "can receive a ShopifyClient::Webhook object" do
103
+ @client.destroy_webhook(ShopifyClient::Webhook.new(id: 123))
104
+
105
+ assert_requested @request
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,12 @@
1
+ describe ShopifyClient::Base do
2
+
3
+ before do
4
+ @base = ShopifyClient::Base.new(:id => 1)
5
+ end
6
+
7
+ describe "#attributes" do
8
+ it "returns a hash of attributes" do
9
+ @base.attributes.must_equal({:id => 1})
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ describe ShopifyClient::Client do
2
+
3
+ describe ".normalize_url" do
4
+ %w{ example.myshopify.com http://example.myshopify.com https://example.myshopify.com example.myshopify.com/admin }.each do |url|
5
+ it "normalizes https:// and /admin" do
6
+ normalized = ShopifyClient::Client.normalize_url url
7
+ normalized.must_equal "https://example.myshopify.com/admin"
8
+ end
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,41 @@
1
+ describe ShopifyClient::CustomCollection do
2
+
3
+ before do
4
+ @custom_collection = ShopifyClient::CustomCollection.new({ :body_html => "<p>The best selling ipod ever</p>",
5
+ :handle => "ipods",
6
+ :id => 841564295,
7
+ :products_count => 1,
8
+ :image => {
9
+ :created_at => "2013-07-18T13:55:42-04:00",
10
+ :src => "http://cdn.shopify.com/s/files/1/0006/9093/3842/collections/ipod_nano_8gb.jpg"
11
+ }
12
+ })
13
+
14
+ end
15
+
16
+ describe "#handle" do
17
+ it "returns the collection's handle" do
18
+ @custom_collection.handle.must_equal 'ipods'
19
+ end
20
+ end
21
+
22
+ describe "#image" do
23
+ it "returns a ShopifyClient::Image" do
24
+ @custom_collection.image.must_be_instance_of ShopifyClient::Image
25
+ end
26
+ describe "when image attribute is nil" do
27
+ before do
28
+ @custom_collection = ShopifyClient::CustomCollection.new({ :body_html => "<p>The best selling ipod ever</p>",
29
+ :handle => "ipods",
30
+ :id => 841564295,
31
+ :products_count => 1
32
+ })
33
+ end
34
+
35
+ it "returns nil" do
36
+ @custom_collection.image.must_be_nil
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,19 @@
1
+ describe ShopifyClient::Error::ClientError do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "when status is 4xx" do
8
+ it "raises ShopifyClient::Error::ClientError" do
9
+ stub_request(:get, "https://example.myshopify.com/admin/shop.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 401, body: MultiJson.dump({ error: "Unauthorized" }) )
12
+
13
+ assert_raises ShopifyClient::Error::ClientError do
14
+ @client.shop
15
+ end
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,20 @@
1
+ describe ShopifyClient::Error::ServerError do
2
+
3
+ before do
4
+ @client = ShopifyClient.new("example.myshopify.com", "token")
5
+ end
6
+
7
+ describe "when status is 4xx" do
8
+ it "raises ShopifyClient::Error::ServerError" do
9
+ stub_request(:get, "https://example.myshopify.com/admin/shop.json").
10
+ with(headers: { 'X-Shopify-Access-Token' => "token" }).
11
+ to_return(status: 500)
12
+
13
+ assert_raises ShopifyClient::Error::ServerError do
14
+ @client.shop
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+
@@ -0,0 +1,30 @@
1
+ describe ShopifyClient::Order do
2
+
3
+ before do
4
+ @order = ShopifyClient::Order.new({ :buyer_accepts_marketing => false,
5
+ :cancel_reason => nil,
6
+ :cancelled_at => nil,
7
+ :cart_token => "68778783ad298f1c80c3bafcddeea02f",
8
+ :checkout_token => nil,
9
+ :closed_at => nil,
10
+ :confirmed => false,
11
+ :created_at => "2008-01-10T11:00:00-05:00",
12
+ :currency => "USD",
13
+ :email => "bob.norman@hostmail.com",
14
+ :financial_status => "authorized",
15
+ :fulfillment_status => nil,
16
+ :gateway => "authorize_net",
17
+ :id => 450789469,
18
+ :landing_site => "http://www.example.com?source=abc"
19
+ })
20
+
21
+ end
22
+
23
+ describe "#financial_status" do
24
+ it "returns the order's financial_status" do
25
+ @order.financial_status.must_equal 'authorized'
26
+ end
27
+ end
28
+
29
+ end
30
+