spiffy_stores_api 4.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +10 -0
  4. data/.travis.yml +14 -0
  5. data/CHANGELOG +3 -0
  6. data/Gemfile +5 -0
  7. data/Gemfile_ar40 +5 -0
  8. data/Gemfile_ar41 +5 -0
  9. data/Gemfile_ar50 +5 -0
  10. data/Gemfile_ar_master +5 -0
  11. data/README.md +200 -0
  12. data/Rakefile +39 -0
  13. data/lib/active_resource/base_ext.rb +21 -0
  14. data/lib/active_resource/connection_ext.rb +10 -0
  15. data/lib/active_resource/detailed_log_subscriber.rb +19 -0
  16. data/lib/active_resource/disable_prefix_check.rb +36 -0
  17. data/lib/active_resource/json_errors.rb +31 -0
  18. data/lib/active_resource/to_query.rb +10 -0
  19. data/lib/spiffy_stores_api.rb +29 -0
  20. data/lib/spiffy_stores_api/connection.rb +33 -0
  21. data/lib/spiffy_stores_api/countable.rb +14 -0
  22. data/lib/spiffy_stores_api/json_format.rb +18 -0
  23. data/lib/spiffy_stores_api/limits.rb +86 -0
  24. data/lib/spiffy_stores_api/metafields.rb +19 -0
  25. data/lib/spiffy_stores_api/resources.rb +2 -0
  26. data/lib/spiffy_stores_api/resources/address.rb +4 -0
  27. data/lib/spiffy_stores_api/resources/article.rb +20 -0
  28. data/lib/spiffy_stores_api/resources/base.rb +86 -0
  29. data/lib/spiffy_stores_api/resources/blog.rb +9 -0
  30. data/lib/spiffy_stores_api/resources/collect.rb +5 -0
  31. data/lib/spiffy_stores_api/resources/country.rb +4 -0
  32. data/lib/spiffy_stores_api/resources/customer.rb +24 -0
  33. data/lib/spiffy_stores_api/resources/image.rb +16 -0
  34. data/lib/spiffy_stores_api/resources/metafield.rb +12 -0
  35. data/lib/spiffy_stores_api/resources/order.rb +23 -0
  36. data/lib/spiffy_stores_api/resources/page.rb +5 -0
  37. data/lib/spiffy_stores_api/resources/product.rb +32 -0
  38. data/lib/spiffy_stores_api/resources/province.rb +5 -0
  39. data/lib/spiffy_stores_api/resources/script_tag.rb +4 -0
  40. data/lib/spiffy_stores_api/resources/standard_collection.rb +18 -0
  41. data/lib/spiffy_stores_api/resources/store.rb +18 -0
  42. data/lib/spiffy_stores_api/resources/super_collection.rb +14 -0
  43. data/lib/spiffy_stores_api/resources/variant.rb +8 -0
  44. data/lib/spiffy_stores_api/resources/webhook.rb +4 -0
  45. data/lib/spiffy_stores_api/session.rb +145 -0
  46. data/lib/spiffy_stores_api/version.rb +3 -0
  47. data/spiffy_stores_api.gemspec +34 -0
  48. data/test/active_resource/json_errors_test.rb +19 -0
  49. data/test/article_test.rb +73 -0
  50. data/test/base_test.rb +112 -0
  51. data/test/blog_test.rb +8 -0
  52. data/test/collect_test.rb +9 -0
  53. data/test/countable_test.rb +13 -0
  54. data/test/customer_test.rb +50 -0
  55. data/test/fixtures/article.json +15 -0
  56. data/test/fixtures/articles.json +39 -0
  57. data/test/fixtures/asset.json +9 -0
  58. data/test/fixtures/assets.json +136 -0
  59. data/test/fixtures/authors.json +1 -0
  60. data/test/fixtures/blog.json +12 -0
  61. data/test/fixtures/blogs.json +13 -0
  62. data/test/fixtures/collect.json +10 -0
  63. data/test/fixtures/custom_collection.json +17 -0
  64. data/test/fixtures/customers.json +64 -0
  65. data/test/fixtures/customers_search.json +66 -0
  66. data/test/fixtures/image.json +10 -0
  67. data/test/fixtures/images.json +20 -0
  68. data/test/fixtures/metafield.json +13 -0
  69. data/test/fixtures/metafields.json +26 -0
  70. data/test/fixtures/o_auth_revoke.json +5 -0
  71. data/test/fixtures/order.json +297 -0
  72. data/test/fixtures/orders.json +299 -0
  73. data/test/fixtures/product.json +116 -0
  74. data/test/fixtures/redirect.json +7 -0
  75. data/test/fixtures/script_tag.json +10 -0
  76. data/test/fixtures/script_tags.json +18 -0
  77. data/test/fixtures/smart_collection.json +21 -0
  78. data/test/fixtures/store.json +35 -0
  79. data/test/fixtures/tags.json +1 -0
  80. data/test/fixtures/transaction.json +29 -0
  81. data/test/fixtures/variant.json +23 -0
  82. data/test/fixtures/variants.json +88 -0
  83. data/test/fixtures/webhook.json +10 -0
  84. data/test/fixtures/webhooks.json +18 -0
  85. data/test/metafield_test.rb +46 -0
  86. data/test/store_test.rb +58 -0
  87. data/test/test_helper.rb +90 -0
  88. metadata +219 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05ec96d33503fbe15376eeec20a213a9caf8022a78db5750a6e9fe19ecb9d71e
4
+ data.tar.gz: 4cf2334c9c1b0b361069904df1b763cdcef90069ca3dcc35ab18b757228d8ff0
5
+ SHA512:
6
+ metadata.gz: 4711b5524e2034510d526c01905970243fd8d386ce61be686de37bc214aa10a06fa6e857af592360de3f1856648b12dc11a43d9239ce59ee0f98f3869f045297
7
+ data.tar.gz: 163757b2e709e1220c134cad7ecec498d10c26da783b71d9535d42310733e1d8b3f1be19407ee55904ab78d119888845ab121b0a69a6ef51810152b2bcdb0b2f
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,10 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ doc
6
+ pkg
7
+ .ruby-version
8
+ *.lock
9
+ .idea
10
+ .dev/
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ sudo: false
3
+
4
+ rvm:
5
+ - 2.2.9
6
+ - 2.3.6
7
+ - 2.4.3
8
+ - 2.5.0
9
+
10
+ gemfile:
11
+ - Gemfile_ar40
12
+ - Gemfile_ar41
13
+ - Gemfile_ar50
14
+ - Gemfile_ar_master
@@ -0,0 +1,3 @@
1
+ == Version 4.11.0
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "activeresource", "~> 5.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "activeresource", "4.0.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "activeresource", "4.1.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "activeresource", "5.0.0"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'activeresource', git: 'git://github.com/rails/activeresource'
@@ -0,0 +1,200 @@
1
+ Spiffy Stores API
2
+ =================
3
+ [![Build Status](https://travis-ci.com/SpiffyStores/spiffy_stores_api.svg?branch=master)](https://travis-ci.com/SpiffyStores/spiffy_stores_api)
4
+
5
+
6
+ The SpiffyStores API gem allows Ruby developers to programmatically access the admin section of SpiffyStores stores.
7
+
8
+ 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.
9
+
10
+ ## Usage
11
+
12
+ ### Requirements
13
+
14
+ All API usage happens through SpiffyStores applications.
15
+
16
+ For more information and detailed documentation about the API visit https://www.spiffystores.com.au/kb/An_Introduction_to_the_Spiffy_Stores_API
17
+
18
+ #### Ruby version
19
+
20
+ This gem requires Ruby 2.4.3 as of version 4.11.0.
21
+
22
+ ### Installation
23
+
24
+ Add `spiffy_stores_api` to your `Gemfile`:
25
+
26
+ ```ruby
27
+ gem 'spiffy_stores_api'
28
+ ```
29
+
30
+ Or install via [gem](http://rubygems.org/)
31
+
32
+ ```bash
33
+ gem install spiffy_stores_api
34
+ ```
35
+
36
+ ### Getting Started
37
+
38
+ SpiffyStoresAPI 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:
39
+
40
+ 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.
41
+
42
+ If you're not sure how to create a new application in the partner/store admin and/or if you're not sure how to generate the required credentials, you can [read the related spiffy_stores docs](https://docs.spiffy_stores.com/api/guides/api-credentials) on the same.
43
+
44
+ 2. For a private App you just need to set the base site url as follows:
45
+
46
+ ```ruby
47
+ shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.spiffystores.com/api"
48
+ SpiffyStoresAPI::Base.site = shop_url
49
+ ```
50
+
51
+ That's it, you're done, skip to step 6 and start using the API!
52
+
53
+ For a partner app you will need to supply two parameters to the Session class before you instantiate it:
54
+
55
+ ```ruby
56
+ SpiffyStoresAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
57
+ ```
58
+
59
+ Spiffy Stores maintains [`omniauth-spiffy-oauth2`](https://github.com/SpiffyStores/omniauth-spiffy-oauth2) which securely wraps the OAuth flow and interactions with Spiffy Stores (steps 3 and 4 above). Using this gem is the recommended way to use OAuth authentication in your application.
60
+
61
+ 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:
62
+
63
+ ```
64
+ GET https://SHOP_NAME.spiffystores.com/api/oauth/authorize
65
+ ```
66
+
67
+ with the following parameters:
68
+
69
+ * ``client_id``– Required – The API key for your app
70
+ * ``scope`` – Required – The list of required scopes (explained here: http://docs.spiffy_stores.com/api/tutorials/oauth)
71
+ * ``redirect_uri`` – Required – The URL where you want to redirect the users after they authorize the client. The complete URL specified here must be identical to one of the Application Redirect URLs set in the App's section of the Partners dashboard. Note: in older applications, this parameter was optional, and redirected to the Application Callback URL when no other value was specified.
72
+ * ``state`` – Optional – A randomly selected value provided by your application, which is unique for each authorization request. During the OAuth callback phase, your application must check that this value matches the one you provided during authorization. [This mechanism is important for the security of your application](https://tools.ietf.org/html/rfc6819#section-3.6).
73
+
74
+ We've added the create_permission_url method to make this easier, first instantiate your session object:
75
+
76
+ ```ruby
77
+ session = SpiffyStoresAPI::Session.new("SHOP_NAME.spiffystores.com")
78
+ ```
79
+
80
+ Then call:
81
+
82
+ ```ruby
83
+ scope = ["write_products"]
84
+ permission_url = session.create_permission_url(scope)
85
+ ```
86
+
87
+ or if you want a custom redirect_uri:
88
+
89
+ ```ruby
90
+ permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")
91
+ ```
92
+
93
+ 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.
94
+
95
+ Before you proceed, make sure your application performs the following security checks. If any of the checks fails, your application must reject the request with an error, and must not proceed further.
96
+
97
+ * Ensure the provided ``state`` is the same one that your application provided to SpiffyStores during Step 3.
98
+ * Ensure the provided hmac is valid. The hmac is signed by SpiffyStores as explained below, in the Verification section.
99
+ * Ensure the provided hostname parameter is a valid hostname, ends with myspiffy_stores.com, and does not contain characters other than letters (a-z), numbers (0-9), dots, and hyphens.
100
+
101
+ If all security checks pass, the authorization code can be exchanged once for a permanent access token. The exchange is made with a request to the shop.
102
+
103
+ ```
104
+ POST https://SHOP_NAME.spiffystores.com/api/oauth/token
105
+ ```
106
+
107
+ with the following parameters:
108
+
109
+ * ``client_id`` – Required – The API key for your app
110
+ * ``client_secret`` – Required – The shared secret for your app
111
+ * ``code`` – Required – The token you received in step 3
112
+
113
+ and you'll get your permanent access token back in the response.
114
+
115
+ There is a method to make the request and get the token for you. Pass
116
+ all the params received from the previous call and the method will verify
117
+ the params, extract the temp code and then request your token:
118
+
119
+ ```ruby
120
+ token = session.request_token(params)
121
+ ```
122
+
123
+ 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:
124
+
125
+ ```ruby
126
+ session = SpiffyStoresAPI::Session.new("SHOP_NAME.spiffystores.com", token)
127
+ ```
128
+
129
+ 5. The session must be activated before use:
130
+
131
+ ```ruby
132
+ SpiffyStoresAPI::Base.activate_session(session)
133
+ ```
134
+
135
+ 6. Now you're ready to make authorized API requests to your shop! Data is returned as ActiveResource instances:
136
+
137
+ ```ruby
138
+ shop = SpiffyStoresAPI::Store.current
139
+
140
+ # Get a specific product
141
+ product = SpiffyStoresAPI::Product.find(123123)
142
+
143
+ # Create a new product
144
+ new_product = SpiffyStoresAPI::Product.new
145
+ new_product.title = "Spiffy Stores T-Shirt"
146
+ new_product.product_type = "Shirt"
147
+ new_product.vendor = "Spiffy Stores"
148
+ new_product.save
149
+
150
+ # Update a product
151
+ product.handle = "spiffy-stores-t-shirt"
152
+ product.save
153
+ ```
154
+
155
+ Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
156
+
157
+ ```ruby
158
+ products = SpiffyStoresAPI::Session.temp("SHOP_NAME.spiffystores.com", token) { SpiffyStoresAPI::Product.find(:all) }
159
+ ```
160
+
161
+ 7. If you want to work with another shop, you'll first need to clear the session:
162
+
163
+ ```ruby
164
+ SpiffyStoresAPI::Base.clear_session
165
+ ```
166
+
167
+ ## Using Development Version
168
+
169
+ Download the source code and run:
170
+
171
+ ```bash
172
+ rake install
173
+ ```
174
+
175
+ ## Additional Resources
176
+
177
+ API Reference: https://www.spiffystores.com.au/kb/API_Reference
178
+
179
+ ## Copyright
180
+
181
+ Copyright (c) 2018 Spiffy Stores
182
+
183
+ Permission is hereby granted, free of charge, to any person obtaining
184
+ a copy of this software and associated documentation files (the
185
+ "Software"), to deal in the Software without restriction, including
186
+ without limitation the rights to use, copy, modify, merge, publish,
187
+ distribute, sublicense, and/or sell copies of the Software, and to
188
+ permit persons to whom the Software is furnished to do so, subject to
189
+ the following conditions:
190
+
191
+ The above copyright notice and this permission notice shall be
192
+ included in all copies or substantial portions of the Software.
193
+
194
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
195
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
196
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
197
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
198
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
199
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
200
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ begin
12
+ require 'rcov/rcovtask'
13
+ Rcov::RcovTask.new do |test|
14
+ test.libs << 'test'
15
+ test.pattern = 'test/**/*_test.rb'
16
+ test.verbose = true
17
+ end
18
+ rescue LoadError
19
+ task :rcov do
20
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
21
+ end
22
+ end
23
+
24
+ task :default => :test
25
+
26
+ require 'rdoc/task'
27
+ Rake::RDocTask.new do |rdoc|
28
+ if File.exist?('VERSION.yml')
29
+ config = YAML.load(File.read('VERSION.yml'))
30
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
31
+ else
32
+ version = ""
33
+ end
34
+
35
+ rdoc.rdoc_dir = 'rdoc'
36
+ rdoc.title = "spiffy_stores_api #{version}"
37
+ rdoc.rdoc_files.include('README*')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveResource
2
+ class Base
3
+ if ActiveResource::VERSION::MAJOR < 4
4
+ # Backported from ActiveResource master branch
5
+ def self.headers
6
+ @headers ||= {}
7
+
8
+ if superclass != Object && superclass.headers
9
+ @headers = superclass.headers.merge(@headers)
10
+ else
11
+ @headers
12
+ end
13
+ end
14
+
15
+ # https://github.com/rails/activeresource/commit/dfef85ce8f653f75673631b2950fcdb0781c313c
16
+ def self.delete(id, options = {})
17
+ connection.delete(element_path(id, options), headers)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ require 'spiffy_stores_api/connection'
2
+
3
+ module ActiveResource
4
+ class Connection
5
+ attr_reader :response
6
+
7
+ prepend SpiffyStoresAPI::Connection::ResponseCapture
8
+ prepend SpiffyStoresAPI::Connection::RequestNotification
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveResource
2
+ class DetailedLogSubscriber < ActiveSupport::LogSubscriber
3
+ def request(event)
4
+ data = event.payload[:data]
5
+ headers = data.extract_options!
6
+ request_body = data.first
7
+
8
+ info "Request:\n#{request_body}" if request_body
9
+ info "Headers: #{headers.inspect}"
10
+ info "Response:\n#{event.payload[:response].body}"
11
+ end
12
+
13
+ def logger
14
+ ActiveResource::Base.logger
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
@@ -0,0 +1,36 @@
1
+ module DisablePrefixCheck
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def check_prefix_options(options)
6
+ end
7
+
8
+ # `flexible = true` is hack to allow multiple things through the same AR class
9
+ def conditional_prefix(resource, flexible = false)
10
+ resource_id = "#{resource}_id".to_sym
11
+ resource_type = flexible ? ":#{resource}" : resource.to_s.pluralize
12
+
13
+ init_prefix_explicit resource_type, resource_id
14
+
15
+ define_singleton_method :prefix do |options = {}|
16
+ resource_type = options[resource] if flexible
17
+
18
+ options[resource_id].nil? ? "/api/" : "/api/#{resource_type}/#{options[resource_id]}/"
19
+ end
20
+
21
+ define_singleton_method :instantiate_record do |record, prefix_options = {}|
22
+ new_record(record).tap do |resource|
23
+ resource.prefix_options = prefix_options unless prefix_options.blank?
24
+ end
25
+ end
26
+ end
27
+
28
+ def new_record(record)
29
+ if ActiveSupport::VERSION::MAJOR == 3 && ActiveSupport::VERSION::MINOR == 0
30
+ new(record)
31
+ else
32
+ new(record, true)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_resource/base'
2
+
3
+ module ActiveResource
4
+ class Errors < ActiveModel::Errors
5
+ def from_json(json, save_cache = false)
6
+ data = ActiveSupport::JSON.decode(json)['errors'] || {} rescue {}
7
+ case data
8
+ when String
9
+ from_string(data, save_cache)
10
+ else
11
+ from_hash(data, save_cache)
12
+ end
13
+ end
14
+
15
+ def from_hash(messages, save_cache = false)
16
+ clear unless save_cache
17
+
18
+ messages.each do |key,errors|
19
+ errors.each do |error|
20
+ add(key, error)
21
+ end
22
+ end
23
+ end
24
+
25
+ def from_string(error, save_cache = false)
26
+ clear unless save_cache
27
+
28
+ add(:base, error)
29
+ end
30
+ end
31
+ end