shopify_api 3.2.5 → 3.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -3
- data.tar.gz.sig +0 -0
- data/.travis.yml +1 -1
- data/CHANGELOG +4 -0
- data/Gemfile.lock +1 -1
- data/Gemfile_ar40threadsafe +2 -0
- data/README.md +201 -0
- data/lib/shopify_api/cli.rb +1 -1
- data/lib/shopify_api/resources/customer_saved_search.rb +1 -1
- data/lib/shopify_api/resources/policy.rb +7 -0
- data/lib/shopify_api/resources/recurring_application_charge.rb +2 -2
- data/lib/shopify_api/resources/shop.rb +2 -2
- data/lib/shopify_api/version.rb +1 -1
- data/shopify_api.gemspec +1 -1
- data/test/cli_test.rb +3 -3
- data/test/customer_saved_search_test.rb +2 -2
- data/test/fixtures/policies.json +8 -0
- data/test/policy_test.rb +19 -0
- data/test/recurring_application_charge_test.rb +8 -0
- metadata +7 -4
- metadata.gz.sig +2 -2
- data/README.rdoc +0 -170
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1e5927ed74df0622bc45489339508c57b37eb21
|
4
|
+
data.tar.gz: b627aaacb19ff2af728755054308c4508fb65bcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bfee60f5326d52653166545615a09e15e9ecd498aae0a5c0e4e14d1b385b9d739525baf9279d6cdf6189c91d04c00266aabdbc7cc8efa7c6b385ebe18ec88be
|
7
|
+
data.tar.gz: a7a02acd9edd9fdba3a8f2d71df4685ef338aeca9f098c2c08e66ec4a4d0638ee5983e1989530d253ba9c2dea5c5cf536b9beb9a9c9c81f95f106b846d846fe3
|
checksums.yaml.gz.sig
CHANGED
@@ -1,3 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
P�>�2�� ���.�꜑)����4��#���Q�iS�1!}z�{�6��O�u|��烅9����mhfZ~L�6J������h�F2_3$f�J��^}�~�(MP�
|
1
|
+
\|�> P��Z��&�1�1#�x,Z�>���HOHN�������!���1H9W�c���H����b��qb����숅���H�Y��YRoO�[�|��Il�BE鹣5�f<Ex�R
|
2
|
+
���\����h�ݷ�e2'W�YW���50� �gBBC��nR����(������G9�e(�<�l����i�����%�;�"3��i�G��5��#�MT��5�k�r�CDJ3����X��7�ڗ��
|
data.tar.gz.sig
CHANGED
Binary file
|
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
data/Gemfile.lock
CHANGED
data/Gemfile_ar40threadsafe
CHANGED
data/README.md
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/Shopify/shopify_api.svg?branch=master)](https://travis-ci.org/Shopify/shopify_api)
|
2
|
+
# Shopify API
|
3
|
+
|
4
|
+
The Shopify API gem allows Ruby developers to programmatically access the admin section of Shopify stores.
|
5
|
+
|
6
|
+
The API is implemented as JSON over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
### Requirements
|
11
|
+
|
12
|
+
All API usage happens through Shopify applications, created by either shop owners for their own shops, or by Shopify Partners for use by other shop owners:
|
13
|
+
|
14
|
+
* Shop owners can create applications for themselves through their own admin: http://docs.shopify.com/api/tutorials/creating-a-private-app
|
15
|
+
* Shopify Partners create applications through their admin: http://app.shopify.com/services/partners
|
16
|
+
|
17
|
+
For more information and detailed documentation about the API visit http://api.shopify.com
|
18
|
+
|
19
|
+
### Installation
|
20
|
+
|
21
|
+
To easily install or upgrade to the latest release, use [gem](http://rubygems.org/)
|
22
|
+
|
23
|
+
```bash
|
24
|
+
gem install shopify_api
|
25
|
+
```
|
26
|
+
|
27
|
+
### Getting Started
|
28
|
+
|
29
|
+
ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveResource has to be configured with a fully authorized URL of a particular store first. To obtain that URL you can follow these steps:
|
30
|
+
|
31
|
+
1. First create a new application in either the partners admin or your store admin. For a private App you'll need the API_KEY and the PASSWORD otherwise you'll need the API_KEY and SHARED_SECRET.
|
32
|
+
|
33
|
+
2. For a private App you just need to set the base site url as follows:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.myshopify.com/admin"
|
37
|
+
ShopifyAPI::Base.site = shop_url
|
38
|
+
```
|
39
|
+
|
40
|
+
That's it, you're done, skip to step 6 and start using the API!
|
41
|
+
|
42
|
+
For a partner app you will need to supply two parameters to the Session class before you instantiate it:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
|
46
|
+
```
|
47
|
+
|
48
|
+
3. In order to access a shop's data, apps need an access token from that specific shop. This is a two-stage process. Before interacting with a shop for the first time an app should redirect the user to the following URL:
|
49
|
+
|
50
|
+
```
|
51
|
+
GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
|
52
|
+
```
|
53
|
+
|
54
|
+
with the following parameters:
|
55
|
+
|
56
|
+
* ``client_id``– Required – The API key for your app
|
57
|
+
* ``scope`` – Required – The list of required scopes (explained here: http://docs.shopify.com/api/tutorials/oauth)
|
58
|
+
* ``redirect_uri`` – Optional – The URL that the merchant will be sent to once authentication is complete. Defaults to the URL specified in the application settings and must be the same host as that URL.
|
59
|
+
|
60
|
+
We've added the create_permission_url method to make this easier, first instantiate your session object:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
|
64
|
+
```
|
65
|
+
|
66
|
+
Then call:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
scope = ["write_products"]
|
70
|
+
permission_url = session.create_permission_url(scope)
|
71
|
+
```
|
72
|
+
|
73
|
+
or if you want a custom redirect_uri:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")
|
77
|
+
```
|
78
|
+
|
79
|
+
4. Once authorized, the shop redirects the owner to the return URL of your application with a parameter named 'code'. This is a temporary token that the app can exchange for a permanent access token. Make the following call:
|
80
|
+
|
81
|
+
```
|
82
|
+
POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token
|
83
|
+
```
|
84
|
+
|
85
|
+
with the following parameters:
|
86
|
+
|
87
|
+
* ``client_id`` – Required – The API key for your app
|
88
|
+
* ``client_secret`` – Required – The shared secret for your app
|
89
|
+
* ``code`` – Required – The token you received in step 3
|
90
|
+
|
91
|
+
and you'll get your permanent access token back in the response.
|
92
|
+
|
93
|
+
There is a method to make the request and get the token for you. Pass
|
94
|
+
all the params received from the previous call and the method will verify
|
95
|
+
the params, extract the temp code and then request your token:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
token = session.request_token(params)
|
99
|
+
```
|
100
|
+
|
101
|
+
This method will save the token to the session object and return it. For future sessions simply pass the token in when creating the session object:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)
|
105
|
+
```
|
106
|
+
|
107
|
+
5. The session must be activated before use:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
ShopifyAPI::Base.activate_session(session)
|
111
|
+
```
|
112
|
+
|
113
|
+
6. Now you're ready to make authorized API requests to your shop! Data is returned as ActiveResource instances:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
shop = ShopifyAPI::Shop.current
|
117
|
+
|
118
|
+
# Get a specific product
|
119
|
+
product = ShopifyAPI::Product.find(179761209)
|
120
|
+
|
121
|
+
# Create a new product
|
122
|
+
new_product = ShopifyAPI::Product.new
|
123
|
+
new_product.title = "Burton Custom Freestlye 151"
|
124
|
+
new_product.product_type = "Snowboard"
|
125
|
+
new_product.vendor = "Burton"
|
126
|
+
new_product.save
|
127
|
+
|
128
|
+
# Update a product
|
129
|
+
product.handle = "burton-snowboard"
|
130
|
+
product.save
|
131
|
+
```
|
132
|
+
|
133
|
+
Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
products = ShopifyAPI::Session.temp("SHOP_NAME.myshopify.com", token) { ShopifyAPI::Product.find(:all) }
|
137
|
+
```
|
138
|
+
|
139
|
+
7. If you want to work with another shop, you'll first need to clear the session:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
ShopifyAPI::Base.clear_session
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
### Console
|
147
|
+
|
148
|
+
This package also includes the ``shopify`` executable to make it easy to open up an interactive console to use the API with a shop.
|
149
|
+
|
150
|
+
1. Obtain a private API key and password to use with your shop (step 2 in "Getting Started")
|
151
|
+
|
152
|
+
2. Use the ``shopify`` script to save the credentials for the shop to quickly log in.
|
153
|
+
|
154
|
+
```bash
|
155
|
+
shopify add yourshopname
|
156
|
+
```
|
157
|
+
|
158
|
+
Follow the prompts for the shop domain, API key and password.
|
159
|
+
|
160
|
+
3. Start the console for the connection.
|
161
|
+
|
162
|
+
```bash
|
163
|
+
shopify console
|
164
|
+
```
|
165
|
+
|
166
|
+
4. To see the full list of commands, type:
|
167
|
+
|
168
|
+
```bash
|
169
|
+
shopify help
|
170
|
+
```
|
171
|
+
|
172
|
+
## Threadsafety
|
173
|
+
|
174
|
+
ActiveResource is inherently non-threadsafe, because class variables like ActiveResource::Base.site and ActiveResource::Base.headers are shared between threads. This can cause conflicts when using threaded libraries, like Sidekiq.
|
175
|
+
|
176
|
+
We have a forked version of ActiveResource that stores these class variables in threadlocal variables. Using this forked version will allow ShopifyAPI to be used in a threaded environment.
|
177
|
+
|
178
|
+
To enable threadsafety with ShopifyAPI, add the following to your Gemfile:
|
179
|
+
|
180
|
+
```
|
181
|
+
gem 'activeresource', git: 'git://github.com/Shopify/activeresource', branch: 'threadsafe'
|
182
|
+
gem 'shopify_api', '>= 3.2.1'
|
183
|
+
```
|
184
|
+
|
185
|
+
## Using Development Version
|
186
|
+
|
187
|
+
Download the source code and run:
|
188
|
+
|
189
|
+
```bash
|
190
|
+
rake install
|
191
|
+
```
|
192
|
+
|
193
|
+
## Additional Resources
|
194
|
+
|
195
|
+
API Docs: http://docs.shopify.com/api
|
196
|
+
|
197
|
+
Ask questions on the forums: http://ecommerce.shopify.com/c/shopify-apis-and-technology
|
198
|
+
|
199
|
+
## Copyright
|
200
|
+
|
201
|
+
Copyright (c) 2014 "Shopify Inc.". See LICENSE for details.
|
data/lib/shopify_api/cli.rb
CHANGED
@@ -27,7 +27,7 @@ module ShopifyAPI
|
|
27
27
|
config['domain'] = ask("Domain? (leave blank for #{connection}.myshopify.com)")
|
28
28
|
config['domain'] = "#{connection}.myshopify.com" if config['domain'].blank?
|
29
29
|
config['domain'] = "#{config['domain']}.myshopify.com" unless config['domain'].match(/[.:]/)
|
30
|
-
puts "\nopen https://#{config['domain']}/admin/
|
30
|
+
puts "\nopen https://#{config['domain']}/admin/apps/private in your browser to create a private app and get API credentials\n"
|
31
31
|
config['api_key'] = ask("API key?")
|
32
32
|
config['password'] = ask("Password?")
|
33
33
|
if ask("Would you like to use pry as your shell? (y/n)") === "y"
|
@@ -3,7 +3,7 @@ require 'shopify_api/resources/customer'
|
|
3
3
|
module ShopifyAPI
|
4
4
|
class CustomerSavedSearch < Base
|
5
5
|
def customers(params = {})
|
6
|
-
Customer.
|
6
|
+
Customer.search(params.merge({:saved_search_id => self.id}))
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -4,11 +4,11 @@ module ShopifyAPI
|
|
4
4
|
|
5
5
|
class << self
|
6
6
|
def current
|
7
|
-
all.find { |c| c.status == 'active' }
|
7
|
+
(all || []).find { |c| c.status == 'active' }
|
8
8
|
end
|
9
9
|
|
10
10
|
[:pending, :cancelled, :accepted, :declined].each do |status|
|
11
|
-
define_method(status) { all.select { |c| c.status == status.to_s } }
|
11
|
+
define_method(status) { (all || []).select { |c| c.status == status.to_s } }
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
data/lib/shopify_api/version.rb
CHANGED
data/shopify_api.gemspec
CHANGED
data/test/cli_test.rb
CHANGED
@@ -28,7 +28,7 @@ class CliTest < Test::Unit::TestCase
|
|
28
28
|
test "add with blank domain" do
|
29
29
|
standard_add_shop_prompts
|
30
30
|
$stdin.expects(:gets).times(4).returns("", "key", "pass", "y")
|
31
|
-
@cli.expects(:puts).with("\nopen https://foo.myshopify.com/admin/
|
31
|
+
@cli.expects(:puts).with("\nopen https://foo.myshopify.com/admin/apps/private in your browser to create a private app and get API credentials\n")
|
32
32
|
@cli.expects(:puts).with("Default connection is foo")
|
33
33
|
|
34
34
|
@cli.add('foo')
|
@@ -45,7 +45,7 @@ class CliTest < Test::Unit::TestCase
|
|
45
45
|
test "add with explicit domain" do
|
46
46
|
standard_add_shop_prompts
|
47
47
|
$stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "y")
|
48
|
-
@cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/
|
48
|
+
@cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/apps/private in your browser to create a private app and get API credentials\n")
|
49
49
|
@cli.expects(:puts).with("Default connection is foo")
|
50
50
|
|
51
51
|
@cli.add('foo')
|
@@ -57,7 +57,7 @@ class CliTest < Test::Unit::TestCase
|
|
57
57
|
test "add with irb as shell" do
|
58
58
|
standard_add_shop_prompts
|
59
59
|
$stdin.expects(:gets).times(4).returns("bar.myshopify.com", "key", "pass", "fuuuuuuu")
|
60
|
-
@cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/
|
60
|
+
@cli.expects(:puts).with("\nopen https://bar.myshopify.com/admin/apps/private in your browser to create a private app and get API credentials\n")
|
61
61
|
@cli.expects(:puts).with("Default connection is foo")
|
62
62
|
|
63
63
|
@cli.add('foo')
|
@@ -7,13 +7,13 @@ class CustomerSavedSearchTest < Test::Unit::TestCase
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def test_get_customers_from_customer_saved_search
|
10
|
-
fake 'customers.json?
|
10
|
+
fake 'customers/search.json?saved_search_id=8899730', :body => load_fixture('customer_saved_search_customers'), :extension => false
|
11
11
|
assert_equal 1, @customer_saved_search.customers.length
|
12
12
|
assert_equal 112223902, @customer_saved_search.customers.first.id
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_get_customers_from_customer_saved_search_with_params
|
16
|
-
fake 'customers.json?
|
16
|
+
fake 'customers/search.json?saved_search_id=8899730&limit=1', :body => load_fixture('customer_saved_search_customers'), :extension => false
|
17
17
|
customers = @customer_saved_search.customers(:limit => 1)
|
18
18
|
assert_equal 1, customers.length
|
19
19
|
assert_equal 112223902, customers.first.id
|
data/test/policy_test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PolicyTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
super
|
6
|
+
fake 'shop'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_getting_the_list_of_policies
|
10
|
+
fake 'policies'
|
11
|
+
|
12
|
+
policies = ShopifyAPI::Policy.all
|
13
|
+
assert_equal 1, policies.length
|
14
|
+
|
15
|
+
policy = policies.first
|
16
|
+
assert_equal 'Privacy Policy', policy.title
|
17
|
+
assert_equal 'Your privacy is important to us', policy.body
|
18
|
+
end
|
19
|
+
end
|
@@ -104,4 +104,12 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
|
|
104
104
|
assert_equal [], ShopifyAPI::RecurringApplicationCharge.pending
|
105
105
|
end
|
106
106
|
|
107
|
+
def test_recurring_application_charge_not_found_error
|
108
|
+
fake "recurring_application_charges", :body => '{"errors":"Not Found"}', :status => 404
|
109
|
+
|
110
|
+
assert_equal nil, ShopifyAPI::RecurringApplicationCharge.all
|
111
|
+
assert_equal nil, ShopifyAPI::RecurringApplicationCharge.current
|
112
|
+
assert_equal [], ShopifyAPI::RecurringApplicationCharge.pending
|
113
|
+
end
|
114
|
+
|
107
115
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shopify_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
fl3hbtVFTqbOlwL9vy1fudXcolIE/ZTcxQ+er07ZFZdKCXayR9PPs64heamfn0fp
|
31
31
|
TConQSX2BnZdhIEYW+cKzEC/bLc=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2014-
|
33
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: activeresource
|
@@ -140,7 +140,7 @@ executables:
|
|
140
140
|
extensions: []
|
141
141
|
extra_rdoc_files:
|
142
142
|
- LICENSE
|
143
|
-
- README.
|
143
|
+
- README.md
|
144
144
|
files:
|
145
145
|
- ".document"
|
146
146
|
- ".gitignore"
|
@@ -154,7 +154,7 @@ files:
|
|
154
154
|
- Gemfile_ar32
|
155
155
|
- Gemfile_ar40threadsafe
|
156
156
|
- LICENSE
|
157
|
-
- README.
|
157
|
+
- README.md
|
158
158
|
- RELEASING
|
159
159
|
- Rakefile
|
160
160
|
- bin/shopify
|
@@ -203,6 +203,7 @@ files:
|
|
203
203
|
- lib/shopify_api/resources/order_risk.rb
|
204
204
|
- lib/shopify_api/resources/page.rb
|
205
205
|
- lib/shopify_api/resources/payment_details.rb
|
206
|
+
- lib/shopify_api/resources/policy.rb
|
206
207
|
- lib/shopify_api/resources/product.rb
|
207
208
|
- lib/shopify_api/resources/province.rb
|
208
209
|
- lib/shopify_api/resources/receipt.rb
|
@@ -259,6 +260,7 @@ files:
|
|
259
260
|
- test/fixtures/order_risk.json
|
260
261
|
- test/fixtures/order_risks.json
|
261
262
|
- test/fixtures/orders.json
|
263
|
+
- test/fixtures/policies.json
|
262
264
|
- test/fixtures/product.json
|
263
265
|
- test/fixtures/recurring_application_charge.json
|
264
266
|
- test/fixtures/recurring_application_charges.json
|
@@ -278,6 +280,7 @@ files:
|
|
278
280
|
- test/metafield_test.rb
|
279
281
|
- test/order_risk_test.rb
|
280
282
|
- test/order_test.rb
|
283
|
+
- test/policy_test.rb
|
281
284
|
- test/product_test.rb
|
282
285
|
- test/recurring_application_charge_test.rb
|
283
286
|
- test/script_tag_test.rb
|
metadata.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
x�$㭔�ώI
|
2
|
+
��>�C!e���GB��N:�SNt�2�6�m>@��1rke�)��tR��Ȑ�ڍ���hb��CnM��~܇~e@��"���hˢI�z�:SHH��MM����������=�ƪ�����RDŽ�܋� �5��OSA�pq�{$!�*�J�����/+��G���#�tj��R��Q�_���HT�uכd�q�;��}��):�;����
|
data/README.rdoc
DELETED
@@ -1,170 +0,0 @@
|
|
1
|
-
{<img src="https://travis-ci.org/Shopify/shopify_api.png?branch=master" alt="Build Status" />}[https://travis-ci.org/Shopify/shopify_api]
|
2
|
-
= Shopify API
|
3
|
-
|
4
|
-
The Shopify API gem allows Ruby developers to programmatically access the admin section of Shopify stores.
|
5
|
-
|
6
|
-
The API is implemented as JSON over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
|
7
|
-
|
8
|
-
|
9
|
-
== Usage
|
10
|
-
|
11
|
-
=== Requirements
|
12
|
-
|
13
|
-
All API usage happens through Shopify applications, created by either shop owners for their own shops, or by Shopify Partners for use by other shop owners:
|
14
|
-
|
15
|
-
* Shop owners can create applications for themselves through their own admin: http://docs.shopify.com/api/tutorials/creating-a-private-app
|
16
|
-
* Shopify Partners create applications through their admin: http://app.shopify.com/services/partners
|
17
|
-
|
18
|
-
For more information and detailed documentation about the API visit http://api.shopify.com
|
19
|
-
|
20
|
-
|
21
|
-
=== Installation
|
22
|
-
|
23
|
-
To easily install or upgrade to the latest release, use {gem}[http://rubygems.org/]
|
24
|
-
|
25
|
-
gem install shopify_api
|
26
|
-
|
27
|
-
|
28
|
-
=== Getting Started
|
29
|
-
|
30
|
-
ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveResource has to be configured with a fully authorized URL of a particular store first. To obtain that URL you can follow these steps:
|
31
|
-
|
32
|
-
1. First create a new application in either the partners admin or your store admin. For a private App you'll need the API_KEY and the PASSWORD otherwise you'll need the API_KEY and SHARED_SECRET.
|
33
|
-
|
34
|
-
2. For a private App you just need to set the base site url as follows:
|
35
|
-
|
36
|
-
shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.myshopify.com/admin"
|
37
|
-
ShopifyAPI::Base.site = shop_url
|
38
|
-
|
39
|
-
That's it, you're done, skip to step 7 and start using the API!
|
40
|
-
|
41
|
-
For a partner app you will need to supply two parameters to the Session class before you instantiate it:
|
42
|
-
|
43
|
-
ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
|
44
|
-
|
45
|
-
3. In order to access a shop's data, apps need an access token from that specific shop. This is a two-stage process. Before interacting with a shop for the first time an app should redirect the user to the following URL:
|
46
|
-
|
47
|
-
GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
|
48
|
-
|
49
|
-
with the following parameters:
|
50
|
-
|
51
|
-
* client_id – Required – The API key for your app
|
52
|
-
* scope – Required – The list of required scopes (explained here: http://docs.shopify.com/api/tutorials/oauth)
|
53
|
-
* redirect_uri – Optional – The URL that the merchant will be sent to once authentication is complete. Defaults to the URL specified in the application settings and must be the same host as that URL.
|
54
|
-
|
55
|
-
We've added the create_permision_url method to make this easier, first instantiate your session object:
|
56
|
-
|
57
|
-
session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
|
58
|
-
|
59
|
-
Then call:
|
60
|
-
|
61
|
-
scope = ["write_products"]
|
62
|
-
permission_url = session.create_permission_url(scope)
|
63
|
-
|
64
|
-
or if you want a custom redirect_uri:
|
65
|
-
|
66
|
-
permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")
|
67
|
-
|
68
|
-
4. Once authorized, the shop redirects the owner to the return URL of your application with a parameter named 'code'. This is a temporary token that the app can exchange for a permanent access token. Make the following call:
|
69
|
-
|
70
|
-
POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token
|
71
|
-
|
72
|
-
with the following parameters:
|
73
|
-
|
74
|
-
* client_id – Required – The API key for your app
|
75
|
-
* client_secret – Required – The shared secret for your app
|
76
|
-
* code – Required – The token you received in step 3
|
77
|
-
|
78
|
-
and you'll get your permanent access token back in the response.
|
79
|
-
|
80
|
-
There is a method to make the request and get the token for you. Pass
|
81
|
-
all the params received from the previous call and the method will verify
|
82
|
-
the params, extract the temp code and then request your token:
|
83
|
-
|
84
|
-
token = session.request_token(params)
|
85
|
-
|
86
|
-
This method will save the token to the session object and return it. For future sessions simply pass the token in when creating the session object:
|
87
|
-
|
88
|
-
session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)
|
89
|
-
|
90
|
-
5. The session must be activated before use:
|
91
|
-
|
92
|
-
ShopifyAPI::Base.activate_session(session)
|
93
|
-
|
94
|
-
6. Now you're ready to make authorized API requests to your shop! Data is returned as ActiveResource instances:
|
95
|
-
|
96
|
-
shop = ShopifyAPI::Shop.current
|
97
|
-
|
98
|
-
# Get a specific product
|
99
|
-
product = ShopifyAPI::Product.find(179761209)
|
100
|
-
|
101
|
-
# Create a new product
|
102
|
-
new_product = ShopifyAPI::Product.new
|
103
|
-
new_product.title = "Burton Custom Freestlye 151"
|
104
|
-
new_product.product_type = "Snowboard"
|
105
|
-
new_product.vendor = "Burton"
|
106
|
-
new_product.save
|
107
|
-
|
108
|
-
# Update a product
|
109
|
-
product.handle = "burton-snowboard"
|
110
|
-
product.save
|
111
|
-
|
112
|
-
Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
|
113
|
-
|
114
|
-
products = ShopifyAPI::Session.temp("SHOP_NAME.myshopify.com", token) { ShopifyAPI::Product.find(:all) }
|
115
|
-
|
116
|
-
8. If you want to work with another shop, you'll first need to clear the session:
|
117
|
-
|
118
|
-
ShopifyAPI::Base.clear_session
|
119
|
-
|
120
|
-
|
121
|
-
=== Console
|
122
|
-
|
123
|
-
This package also includes the +shopify+ executable to make it easy to open up an interactive console to use the API with a shop.
|
124
|
-
|
125
|
-
1. Obtain a private API key and password to use with your shop (step 2 in "Getting Started")
|
126
|
-
|
127
|
-
2. Use the +shopify+ script to save the credentials for the shop to quickly log in.
|
128
|
-
|
129
|
-
shopify add yourshopname
|
130
|
-
|
131
|
-
Follow the prompts for the shop domain, API key and password.
|
132
|
-
|
133
|
-
3. Start the console for the connection.
|
134
|
-
|
135
|
-
shopify console
|
136
|
-
|
137
|
-
4. To see the full list of commands, type:
|
138
|
-
|
139
|
-
shopify help
|
140
|
-
|
141
|
-
|
142
|
-
== Threadsafety
|
143
|
-
|
144
|
-
ActiveResource is inherently non-threadsafe, because class variables like ActiveResource::Base.site
|
145
|
-
and ActiveResource::Base.headers are shared between threads. This can cause conflicts when using
|
146
|
-
threaded libraries, like Sidekiq.
|
147
|
-
|
148
|
-
We have a forked version of ActiveResource that stores these class variables in threadlocal
|
149
|
-
variables. Using this forked version will allow ShopifyAPI to be used in a threaded environment.
|
150
|
-
|
151
|
-
To enable threadsafety with ShopifyAPI, add the following to your Gemfile:
|
152
|
-
|
153
|
-
gem 'activeresource', git: 'git://github.com/Shopify/activeresource', branch: 'threadsafe'
|
154
|
-
gem 'shopify_api', '>= 3.2.1'
|
155
|
-
|
156
|
-
== Using Development Version
|
157
|
-
|
158
|
-
Download the source code and run:
|
159
|
-
|
160
|
-
rake install
|
161
|
-
|
162
|
-
== Additional Resources
|
163
|
-
|
164
|
-
http://docs.shopify.com/api <= Read the tech docs!
|
165
|
-
|
166
|
-
http://ecommerce.shopify.com/c/shopify-apis-and-technology <= Ask questions on the forums!
|
167
|
-
|
168
|
-
== Copyright
|
169
|
-
|
170
|
-
Copyright (c) 2012 "Shopify inc.". See LICENSE for details.
|