squarespace_api 0.0.4 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc908a714e0fa1658267c8753adcbd9085e87bd2b0b46a108231e06b53a20f6b
4
- data.tar.gz: 18a4ba216bc82ab428b5ee8c7352f4f8a23d9ab98630f6a469961860242a8871
3
+ metadata.gz: 7605a851dda2e468747c0133620edb3763d8961c935ca15b005e3a4b406fefa7
4
+ data.tar.gz: 1fb57f3e5cd318a7f3d0167927ee58521d3b76ecdd24ee5ea25daa408d2bb8e0
5
5
  SHA512:
6
- metadata.gz: b2f62e190db5adc017b6d47933fc0e5fe28e10199098c1f148a1f4d2a10b7e5ec7efc95392f6afc22a2a53844d893733e1c815abba7d42135744332593640189
7
- data.tar.gz: 73283ff1f0f272f39b3cd46c8db5651ae8316539663276f6225b0608c2304a637c02e37fd9d529fb300b33e1436a3f874a871cd014db148ce4d339b0d377999a
6
+ metadata.gz: 334a4961dc1130524e539ae650ac7cb7697fadad88fa1509aa08b0b765e9d31683690b49ab7d5b9a03728e4b1832e5b46acdfaa66d1d9fc9129baaf77e578f5d
7
+ data.tar.gz: 3205784829d70dd252340dd7fa620066a380703a84f73be2f036b303d616b3b59886ce301bb8791ffc45afae6f101de4075672ff089e54a1c8da16573b7873d4
data/Gemfile.lock CHANGED
@@ -1,11 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- squarespace_api (0.0.3)
4
+ squarespace_api (0.0.7)
5
5
  faraday
6
6
  faraday_middleware
7
7
  json
8
- yaml
9
8
 
10
9
  GEM
11
10
  remote: https://rubygems.org/
@@ -16,29 +15,33 @@ GEM
16
15
  crack (0.4.5)
17
16
  rexml
18
17
  diff-lcs (1.4.4)
19
- faraday (1.7.0)
18
+ faraday (1.10.0)
20
19
  faraday-em_http (~> 1.0)
21
20
  faraday-em_synchrony (~> 1.0)
22
21
  faraday-excon (~> 1.1)
23
- faraday-httpclient (~> 1.0.1)
22
+ faraday-httpclient (~> 1.0)
23
+ faraday-multipart (~> 1.0)
24
24
  faraday-net_http (~> 1.0)
25
- faraday-net_http_persistent (~> 1.1)
25
+ faraday-net_http_persistent (~> 1.0)
26
26
  faraday-patron (~> 1.0)
27
27
  faraday-rack (~> 1.0)
28
- multipart-post (>= 1.2, < 3)
28
+ faraday-retry (~> 1.0)
29
29
  ruby2_keywords (>= 0.0.4)
30
30
  faraday-em_http (1.0.0)
31
31
  faraday-em_synchrony (1.0.0)
32
32
  faraday-excon (1.1.0)
33
33
  faraday-httpclient (1.0.1)
34
+ faraday-multipart (1.0.3)
35
+ multipart-post (>= 1.2, < 3)
34
36
  faraday-net_http (1.0.1)
35
37
  faraday-net_http_persistent (1.2.0)
36
38
  faraday-patron (1.0.0)
37
39
  faraday-rack (1.0.0)
38
- faraday_middleware (1.1.0)
40
+ faraday-retry (1.0.3)
41
+ faraday_middleware (1.2.0)
39
42
  faraday (~> 1.0)
40
43
  hashdiff (1.0.1)
41
- json (2.5.1)
44
+ json (2.6.1)
42
45
  method_source (1.0.0)
43
46
  multipart-post (2.1.1)
44
47
  pry (0.14.1)
@@ -64,7 +67,6 @@ GEM
64
67
  addressable (>= 2.8.0)
65
68
  crack (>= 0.3.2)
66
69
  hashdiff (>= 0.4.0, < 2.0.0)
67
- yaml (0.1.1)
68
70
 
69
71
  PLATFORMS
70
72
  ruby
data/README.md CHANGED
@@ -1,3 +1,231 @@
1
1
  # Squarespace Api
2
2
 
3
- Ruby interface to the Squarespace API
3
+ [![Gem Version](https://badge.fury.io/rb/squarespace_api.svg)](https://rubygems.org/gems/squarespace_api)
4
+
5
+
6
+ Ruby interface to the [Squarespace API](https://developers.squarespace.com/commerce-apis/overview).
7
+
8
+ ## Installation
9
+
10
+ Available in RubyGems.
11
+
12
+ ```bash
13
+ $ gem install 'squarespace_api'
14
+ ```
15
+
16
+ Add to your Gem file
17
+ ```ruby
18
+ gem 'squarespace_api'
19
+ ```
20
+
21
+
22
+
23
+ ## Quick Example
24
+
25
+ - Thread Safety Usage
26
+
27
+ ```ruby
28
+ require 'squarespace_api'
29
+
30
+ client = SquarespaceApi::Client.new(
31
+ SquarespaceApi::Config.new(
32
+ access_token: 'your access token'
33
+ )
34
+ )
35
+
36
+ # get one page of order
37
+ client.orders.all
38
+ => [<SquarespaceOrderHash>, ...]
39
+
40
+
41
+ # get every page of orders with filter
42
+ orders = []
43
+ orders = client.orders.all(fulfillmentStatus: 'PENDING') do |orders_per_page|
44
+ orders += orders_per_page
45
+ # or do something
46
+ end
47
+
48
+ # get website
49
+ client.website
50
+ ```
51
+
52
+
53
+ ## ResourceGroups
54
+
55
+ - [Orders](https://developers.squarespace.com/commerce-apis/orders-overview)
56
+
57
+ ```ruby
58
+ # get one page
59
+ client.orders.all(fulfillmentStatus: 'PENDING')
60
+
61
+ # get all pages
62
+ orders = []
63
+ orders = client.orders.all(fulfillmentStatus: 'PENDING') do |orders_per_page|
64
+ orders += orders_per_page
65
+ # or do something
66
+ end
67
+
68
+ # find an order
69
+ client.orders.find(id)
70
+
71
+ # create an order
72
+ client.orders.create(params)
73
+
74
+ # fulfill an order
75
+ client.orders.fulfil(id, params)
76
+ ```
77
+
78
+ - [Products](https://developers.squarespace.com/commerce-apis/products-overview)
79
+
80
+ ```ruby
81
+ client.products.all
82
+ client.products.create(params)
83
+ client.products.where(id: id)
84
+ client.products.find(id)
85
+ client.products.find_by_ids([id_1, id_2])
86
+ client.products.update(id, params)
87
+ client.products.delete(id)
88
+ ```
89
+
90
+ - ProductImages
91
+
92
+ ```ruby
93
+ # Noted that Path parameters should be passed in params.
94
+ client.product_images.upload(file_path, { product_id: product_id })
95
+ client.product_images.status(id, { product_id: product_id })
96
+ client.product_images.order(id, { product_id: product_id, afterImageId: 'otherImageId' })
97
+ client.product_images.update(id, { product_id: product_id, altText: 'my_picture' })
98
+ ```
99
+
100
+ - ProductVariants
101
+
102
+ ```ruby
103
+ # Noted that Path parameters should be passed in params.
104
+ client.product_variants.create({ product_id: product_id })
105
+ client.product_variants.update(id, { sku: 'P1234', product_id: product_id })
106
+ ```
107
+
108
+
109
+ - ProductVarantImages
110
+
111
+ ```ruby
112
+ # Noted that Path parameters should be passed in body params.
113
+ client.product_variant_images.create({ imageId: image_id, product_id: product_id, variant_id: variant_id })
114
+ ```
115
+
116
+ - Inventory
117
+
118
+ ```ruby
119
+ client.inventory.all
120
+ client.inventory.find(id)
121
+ client.inventory_adjustments.create(params)
122
+ ```
123
+
124
+ - Profiles
125
+
126
+ ```ruby
127
+ client.profiles.all
128
+ client.profiles.find(id)
129
+ client.profiles.create(params)
130
+ ```
131
+
132
+ - StorePages
133
+
134
+ ```ruby
135
+ client.store_pages.all
136
+ ```
137
+
138
+ - Transactions
139
+
140
+ ```ruby
141
+ client.transactions.all
142
+ client.transactions.find(id)
143
+ client.transactions.find_by_ids([id_1, id_2])
144
+ ```
145
+
146
+ - WebhookSubscriptions
147
+
148
+ ```ruby
149
+ client.webhook_subscriptions.all
150
+ client.webhook_subscriptions.find(id)
151
+ client.webhook_subscriptions.create(id)
152
+ client.webhook_subscriptions.update(id, params)
153
+ client.webhook_subscriptions.delete(id)
154
+ client.webhook_subscriptions.send_test_notification(id, params)
155
+ client.webhook_subscriptions.rotate_secret(id)
156
+ ```
157
+
158
+
159
+ ## Single Resource
160
+
161
+ - Website
162
+
163
+ ```ruby
164
+ client.website
165
+ ```
166
+
167
+ ## Oauth App
168
+
169
+ ### Configuration
170
+
171
+ configure your oauth cerdentials in ENV:
172
+
173
+ ```ruby
174
+ # Auto load ENV
175
+ SQUARESPACE_CLIENT_ID = 'your client id'
176
+ SQUARESPACE_CLIENT_SECRET = 'your client secret'
177
+ ```
178
+
179
+ Or configure in your initializer:
180
+
181
+ ```ruby
182
+ SquarespaceApi.configure do |c|
183
+ c.client_id = 'your client id'
184
+ c.client_secret = 'your client secret'
185
+ end
186
+ ```
187
+
188
+ If you have multiple apps, consider thread safe usage:
189
+
190
+ ```ruby
191
+ client = SquarespaceApi::Client.new(
192
+ SquarespaceApi::Config.new(
193
+ client_id: 'your client id',
194
+ client_secret: 'your client secret'
195
+ )
196
+ )
197
+ ```
198
+
199
+ ### Oauth Actions
200
+
201
+ - Construct Oauth Url
202
+
203
+ ```ruby
204
+ SquarespaceApi::Client.new.build_oauth_authorize_url(
205
+ redirect_url: 'https://api.test.com/squarespace',
206
+ scope: 'website.orders,website.orders.read',
207
+ state: '1234',
208
+ access_type: "offline"
209
+ )
210
+
211
+ => 'https://login.squarespace.com/api/1/login/oauth/provider/authorize?client_id=&redirect_url=https://api.test.com/squarespace&scope=website.orders,website.orders.read&state=1234&access_type=offline'
212
+ ```
213
+
214
+ - Request Access Token
215
+
216
+ ```ruby
217
+ # request access token
218
+ client.tokens.create(
219
+ code: 'code sent from Squaerspace',
220
+ redirect_uri: "your redirect uri"
221
+ )
222
+ ```
223
+
224
+ - Request Refresh Token
225
+
226
+ ```ruby
227
+ client.tokens.create(
228
+ refresh_token: 'some_refresh_token',
229
+ grant_type: "refresh_token"
230
+ )
231
+ ```
@@ -17,6 +17,7 @@ module SquarespaceApi
17
17
  @connection = Faraday.new do |connection|
18
18
  connection.headers = DEFAULT_HEADERS
19
19
  connection.response :json
20
+ connection.adapter(Faraday.default_adapter)
20
21
  end
21
22
 
22
23
  initialize_app_session if config.access_token
@@ -2,11 +2,11 @@ module SquarespaceApi
2
2
  module PaginatedFetch
3
3
  def self.build(params = {}, &_block)
4
4
  loop do
5
- response = yield(params)
5
+ pagination = yield(params)
6
6
 
7
- has_next_page = response.dig('pagination', 'hasNextPage').to_s == 'true'
8
- break unless has_next_page
9
- params = { cursor: response.dig('pagination', 'nextPageCursor') }
7
+ has_next_page = !!pagination&.dig('hasNextPage')
8
+ break if not has_next_page
9
+ params = { cursor: pagination['nextPageCursor'] }
10
10
  end
11
11
  end
12
12
  end
@@ -24,6 +24,10 @@ module SquarespaceApi
24
24
  raise 'This method should be implemented in a subclass.'
25
25
  end
26
26
 
27
+ def parse_pagination(response)
28
+ response.body['pagination']
29
+ end
30
+
27
31
  class << self
28
32
  def allowed_actions(*args)
29
33
  args.each do |action|
@@ -6,13 +6,14 @@ module SquarespaceApi
6
6
  module All
7
7
  def all(params = {}, &_block)
8
8
  PaginatedFetch.build(params) do |paginated_params|
9
- response = parse_collection(
10
- connection
9
+ query_result = connection
11
10
  .get(UriComponentBuidler.construct(resources_path, paginated_params), params: paginated_params)
12
- )
11
+ response = parse_collection(query_result)
12
+ pagination = parse_pagination(query_result)
13
+
13
14
  return response unless block_given?
14
15
  yield(response)
15
- response
16
+ pagination
16
17
  end
17
18
  end
18
19
 
@@ -1,3 +1,3 @@
1
1
  module SquarespaceApi
2
- VERSION = '0.0.4'.freeze
2
+ VERSION = '0.0.7'.freeze
3
3
  end
@@ -22,6 +22,33 @@ describe SquarespaceApi::ResourceGroupActions do
22
22
 
23
23
  it { expect(resource.all(status: 'pending')).to eq([]) }
24
24
  it { expect(resource.where(status: 'pending')).to eq([]) }
25
+
26
+ context "when pagination exists" do
27
+ before do
28
+ stub_request(:get, "https://api.squarespace.com/1.0/commerce/dummy?status=pending")
29
+ .to_return(status: 200, body: {
30
+ 'dummy' => [{ 'id' => 1 }],
31
+ 'pagination' => { "hasNextPage" => true, 'nextPageCursor' => "cursor"
32
+ }
33
+ }.to_json)
34
+
35
+ stub_request(:get, "https://api.squarespace.com/1.0/commerce/dummy?cursor=cursor")
36
+ .to_return(status: 200, body: {
37
+ 'dummy' => [{ 'id' => 2 }],
38
+ 'pagination' => { "hasNextPage" => false }
39
+ }.to_json)
40
+ end
41
+
42
+ it "should return all orders" do
43
+ orders = []
44
+
45
+ resource.all(status: 'pending') do |new_orders|
46
+ orders += new_orders
47
+ end
48
+
49
+ expect(orders).to eq([{ 'id' => 1 }, { 'id' => 2 }])
50
+ end
51
+ end
25
52
  end
26
53
 
27
54
  describe '#find' do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe SquarespaceApi do
4
4
  it 'has a version number' do
5
- expect(SquarespaceApi::VERSION).to eq('0.0.4')
5
+ expect(SquarespaceApi::VERSION).to eq('0.0.7')
6
6
  end
7
7
 
8
8
  context 'configure' do
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "squarespace_api"
3
- spec.version = "0.0.4"
3
+ spec.version = "0.0.7"
4
4
  spec.summary = "Ruby gem interacting with the Squarespace API."
5
5
  spec.description = "Ruby gem interacting with the Squarespace API."
6
6
  spec.authors = ["cdragon"]
@@ -18,5 +18,4 @@ Gem::Specification.new do |spec|
18
18
  spec.add_dependency "faraday"
19
19
  spec.add_dependency "faraday_middleware"
20
20
  spec.add_dependency "json"
21
- spec.add_dependency "yaml"
22
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squarespace_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - cdragon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-29 00:00:00.000000000 Z
11
+ date: 2022-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: yaml
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  description: Ruby gem interacting with the Squarespace API.
112
98
  email: cdragon1116@gmail.com
113
99
  executables: []
@@ -176,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
162
  - !ruby/object:Gem::Version
177
163
  version: '0'
178
164
  requirements: []
179
- rubygems_version: 3.0.6
165
+ rubygems_version: 3.0.9
180
166
  signing_key:
181
167
  specification_version: 4
182
168
  summary: Ruby gem interacting with the Squarespace API.