lucid_shopify 0.5.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -9
- data/lib/lucid_shopify/activate_charge.rb +9 -9
- data/lib/lucid_shopify/assert_callback.rb +61 -0
- data/lib/lucid_shopify/assert_webhook.rb +28 -0
- data/lib/lucid_shopify/charge.rb +33 -28
- data/lib/lucid_shopify/client.rb +12 -17
- data/lib/lucid_shopify/config.rb +45 -0
- data/lib/lucid_shopify/container.rb +23 -0
- data/lib/lucid_shopify/create_all_webhooks.rb +40 -0
- data/lib/lucid_shopify/create_charge.rb +9 -9
- data/lib/lucid_shopify/create_webhook.rb +24 -0
- data/lib/lucid_shopify/delegate_webhooks.rb +1 -1
- data/lib/lucid_shopify/delete_all_webhooks.rb +30 -0
- data/lib/lucid_shopify/delete_request.rb +3 -1
- data/lib/lucid_shopify/delete_webhook.rb +22 -0
- data/lib/lucid_shopify/error.rb +11 -0
- data/lib/lucid_shopify/fetch_access_token.rb +12 -14
- data/lib/lucid_shopify/get_request.rb +3 -1
- data/lib/lucid_shopify/post_request.rb +3 -1
- data/lib/lucid_shopify/put_request.rb +3 -1
- data/lib/lucid_shopify/request.rb +5 -5
- data/lib/lucid_shopify/request_credentials.rb +1 -1
- data/lib/lucid_shopify/response.rb +4 -21
- data/lib/lucid_shopify/send_request.rb +16 -23
- data/lib/lucid_shopify/send_throttled_request.rb +2 -2
- data/lib/lucid_shopify/version.rb +1 -1
- data/lib/lucid_shopify/webhook.rb +2 -1
- data/lib/lucid_shopify.rb +31 -23
- metadata +53 -7
- data/lib/lucid_shopify/credentials.rb +0 -42
- data/lib/lucid_shopify/result.rb +0 -33
- data/lib/lucid_shopify/verify_callback.rb +0 -70
- data/lib/lucid_shopify/verify_webhook.rb +0 -40
- data/lib/lucid_shopify/webhooks.rb +0 -82
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a53c313f841a7f88609457eb4b2c4f1a7651eee3d687097be04f98d75f36f4a0
|
4
|
+
data.tar.gz: e3190ad427bd79f7bd0fe8f7ace431aad5544d9fe162b1098a5885d3a2959efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36cb4616edbe9d671ff6b34f3ee92d92e5a50bf20d7da05d2bfe783b307b8678d7c36d920174223ebf15739bd76577d001275db4c6f34805d09b8c485bd3aa14
|
7
|
+
data.tar.gz: '04293660bb3c7abb9d4999924fbd311ccbf29de0e9d405338321e5e95bb2257a4861ead6be9c4684e7ca7424a9d46c015a95ae0a3b44354c2a7225bd877ece18'
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ Usage
|
|
14
14
|
|
15
15
|
### Configure the default API client credentials
|
16
16
|
|
17
|
-
LucidShopify.
|
17
|
+
LucidShopify.config = LucidShopify::Config.new(
|
18
18
|
'...', # api_key
|
19
19
|
'...', # shared_secret
|
20
20
|
'...', # scope
|
@@ -66,28 +66,34 @@ to create a worker around something like this:
|
|
66
66
|
|
67
67
|
Create/delete all configured webhooks (see above):
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
webhooks.create_all(request_credentials)
|
72
|
-
webhooks.delete_all(request_credentials)
|
69
|
+
LucidShopify::CreateAllWebhooks.new.(request_credentials)
|
70
|
+
LucidShopify::DeleteAllWebhooks.new.(request_credentials)
|
73
71
|
|
74
72
|
Create/delete webhooks manually:
|
75
73
|
|
76
74
|
webhook = {topic: 'orders/create', fields: %w(id tags)}
|
77
75
|
|
78
|
-
|
79
|
-
|
76
|
+
LucidShopify::CreateWebhook.new.(request_credentials, webhook)
|
77
|
+
LucidShopify::DeleteWebhook.new.(request_credentials, webhook_id)
|
80
78
|
|
81
79
|
|
82
80
|
### Verification
|
83
81
|
|
84
82
|
Verify callback requests with the request params:
|
85
83
|
|
86
|
-
|
84
|
+
begin
|
85
|
+
LucidShopify::AssertCallback.new.(params)
|
86
|
+
rescue LucidShopify::Error => e
|
87
|
+
# ...
|
88
|
+
end
|
87
89
|
|
88
90
|
Verify webhook requests with the request data and the HMAC header:
|
89
91
|
|
90
|
-
|
92
|
+
begin
|
93
|
+
LucidShopify::AssertWebhook.new.(data, hmac)
|
94
|
+
rescue LucidShopify::Error => e
|
95
|
+
# ...
|
96
|
+
end
|
91
97
|
|
92
98
|
|
93
99
|
### Authorization
|
@@ -1,26 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require 'lucid_shopify/client'
|
3
|
+
require 'lucid_shopify/container'
|
6
4
|
|
7
5
|
module LucidShopify
|
8
6
|
class ActivateCharge
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
|
7
|
+
#
|
8
|
+
# @param client [#post_json]
|
9
|
+
#
|
10
|
+
def initialize(client: Container[:client])
|
11
|
+
@client = client
|
12
|
+
end
|
13
13
|
|
14
14
|
#
|
15
15
|
# Activate a recurring application charge.
|
16
16
|
#
|
17
17
|
# @param request_credentials [RequestCredentials]
|
18
|
-
# @param charge [
|
18
|
+
# @param charge [#to_h] an accepted charge received from Shopify via callback
|
19
19
|
#
|
20
20
|
# @return [Hash] the active charge
|
21
21
|
#
|
22
22
|
def call(request_credentials, charge)
|
23
|
-
data = client.post_json(request_credentials, "recurring_application_charges/#{
|
23
|
+
data = @client.post_json(request_credentials, "recurring_application_charges/#{charge.to_h['id']}/activate", charge.to_h)
|
24
24
|
|
25
25
|
data['recurring_application_charge']
|
26
26
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
require 'lucid_shopify'
|
6
|
+
|
7
|
+
module LucidShopify
|
8
|
+
class AssertCallback
|
9
|
+
Error = Class.new(Error)
|
10
|
+
|
11
|
+
#
|
12
|
+
# Assert that the callback request originated from Shopify.
|
13
|
+
#
|
14
|
+
# @param params [Hash] the request params
|
15
|
+
#
|
16
|
+
# @raise [Error] if signature is invalid
|
17
|
+
#
|
18
|
+
def call(params)
|
19
|
+
params = params.to_h
|
20
|
+
digest = OpenSSL::Digest::SHA256.new
|
21
|
+
digest = OpenSSL::HMAC.hexdigest(digest, LucidShopify.config.shared_secret, encoded_params(params))
|
22
|
+
|
23
|
+
raise Error, 'invalid signature' unless digest == params['hmac']
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# @param params [Hash]
|
28
|
+
#
|
29
|
+
# @return [String]
|
30
|
+
#
|
31
|
+
private def encoded_params(params)
|
32
|
+
params.reject do |k, _|
|
33
|
+
k == 'hmac'
|
34
|
+
end.map do |k, v|
|
35
|
+
[].tap do |param|
|
36
|
+
param << k.gsub(/./) { |c| encode_key(c) }
|
37
|
+
param << '='
|
38
|
+
param << v.gsub(/./) { |c| encode_val(c) }
|
39
|
+
end.join
|
40
|
+
end.join('&')
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# @param chr [String]
|
45
|
+
#
|
46
|
+
# @return [String]
|
47
|
+
#
|
48
|
+
private def encode_key(chr)
|
49
|
+
{'%' => '%25', '&' => '%26', '=' => '%3D'}[chr] || chr
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# @param chr [String]
|
54
|
+
#
|
55
|
+
# @return [String]
|
56
|
+
#
|
57
|
+
private def encode_val(chr)
|
58
|
+
{'%' => '%25', '&' => '%26'}[chr] || chr
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'base64'
|
4
|
+
require 'openssl'
|
5
|
+
|
6
|
+
require 'lucid_shopify'
|
7
|
+
|
8
|
+
module LucidShopify
|
9
|
+
class AssertWebhook
|
10
|
+
Error = Class.new(Error)
|
11
|
+
|
12
|
+
#
|
13
|
+
# Assert that the webhook request originated from Shopify.
|
14
|
+
#
|
15
|
+
# @param data [String] the signed request data
|
16
|
+
# @param hmac [String] the signature
|
17
|
+
#
|
18
|
+
# @return [Result]
|
19
|
+
#
|
20
|
+
def call(data, hmac)
|
21
|
+
digest = OpenSSL::Digest::SHA256.new
|
22
|
+
digest = OpenSSL::HMAC.digest(digest, LucidShopify.config.shared_secret, data)
|
23
|
+
digest = Base64.encode64(digest).strip
|
24
|
+
|
25
|
+
raise Error, 'invalid signature' unless digest == hmac
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/lucid_shopify/charge.rb
CHANGED
@@ -1,30 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require 'lucid_shopify/credentials'
|
3
|
+
require 'lucid_shopify'
|
6
4
|
|
7
5
|
module LucidShopify
|
8
6
|
#
|
9
|
-
#
|
7
|
+
# Convenient way to build the charge hash for {CreateCharge}.
|
10
8
|
#
|
11
9
|
class Charge
|
12
|
-
|
10
|
+
#
|
11
|
+
# @param plan_name [String]
|
12
|
+
# @param price [Integer]
|
13
|
+
# @param price_cap [Integer] requires price_terms
|
14
|
+
# @param price_terms [String] requires price_cap
|
15
|
+
# @param test [Boolean] is this a test_charge?
|
16
|
+
# @param trial_days [Integer]
|
17
|
+
#
|
18
|
+
def initialize(plan_name,
|
19
|
+
price,
|
20
|
+
price_cap: nil,
|
21
|
+
price_terms: nil,
|
22
|
+
test: false,
|
23
|
+
trial_days: 7)
|
24
|
+
@plan_name = plan_name
|
25
|
+
@price = price
|
26
|
+
@price_cap = price_cap
|
27
|
+
@price_terms = price_terms
|
28
|
+
@test = test
|
29
|
+
@trial_days = trial_days
|
13
30
|
|
14
|
-
|
15
|
-
|
16
|
-
# @return [Integer]
|
17
|
-
param :price
|
18
|
-
# @return [Integer] requires price_terms
|
19
|
-
option :price_cap, optional: true
|
20
|
-
# @return [String] requires price_cap
|
21
|
-
option :price_terms, optional: true
|
22
|
-
# @return [Boolean] is this a test charge?
|
23
|
-
option :test, default: proc { false }
|
24
|
-
# @return [Integer]
|
25
|
-
option :trial_days, default: proc { 7 }
|
26
|
-
# @return [Credentials]
|
27
|
-
option :credentials, default: proc { LucidShopify.credentials }
|
31
|
+
freeze
|
32
|
+
end
|
28
33
|
|
29
34
|
#
|
30
35
|
# Map to the Shopify API structure.
|
@@ -32,14 +37,14 @@ module LucidShopify
|
|
32
37
|
# @return [Hash]
|
33
38
|
#
|
34
39
|
def to_h
|
35
|
-
{}.tap do |
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
{}.tap do |h|
|
41
|
+
h[:name] = @plan_name
|
42
|
+
h[:price] = @price
|
43
|
+
h[:capped_amount] = @price_cap if usage_based_billing?
|
44
|
+
h[:terms] = @price_terms if usage_based_billing?
|
45
|
+
h[:return_url] = LucidShopify.config.billing_callback_uri
|
46
|
+
h[:test] = @test if @test
|
47
|
+
h[:trial_days] = @trial_days if @trial_days
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
@@ -47,7 +52,7 @@ module LucidShopify
|
|
47
52
|
# @return [Boolean]
|
48
53
|
#
|
49
54
|
private def usage_based_billing?
|
50
|
-
price_cap && price_terms
|
55
|
+
@price_cap && @price_terms
|
51
56
|
end
|
52
57
|
end
|
53
58
|
end
|
data/lib/lucid_shopify/client.rb
CHANGED
@@ -1,49 +1,44 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'lucid_shopify/
|
3
|
+
require 'lucid_shopify/container'
|
4
4
|
|
5
|
-
%w(delete get post put).each
|
6
|
-
require "lucid_shopify/#{method}_request"
|
7
|
-
end
|
5
|
+
%w(delete get post put).each { |m| require "lucid_shopify/#{m}_request" }
|
8
6
|
|
9
7
|
module LucidShopify
|
10
8
|
class Client
|
11
9
|
#
|
12
|
-
# @param send_request [
|
10
|
+
# @param send_request [#call]
|
13
11
|
#
|
14
|
-
def initialize(send_request:
|
12
|
+
def initialize(send_request: Container[:send_request])
|
15
13
|
@send_request = send_request
|
16
14
|
end
|
17
15
|
|
18
|
-
# @return [SendRequest]
|
19
|
-
attr_reader :send_request
|
20
|
-
|
21
16
|
#
|
22
|
-
# @see
|
17
|
+
# @see DeleteRequest#initialize
|
23
18
|
#
|
24
19
|
def delete(*args)
|
25
|
-
send_request.(DeleteRequest.new(*args))
|
20
|
+
@send_request.(DeleteRequest.new(*args))
|
26
21
|
end
|
27
22
|
|
28
23
|
#
|
29
|
-
# @see
|
24
|
+
# @see GetRequest#initialize
|
30
25
|
#
|
31
26
|
def get(*args)
|
32
|
-
send_request.(GetRequest.new(*args))
|
27
|
+
@send_request.(GetRequest.new(*args))
|
33
28
|
end
|
34
29
|
|
35
30
|
#
|
36
|
-
# @see
|
31
|
+
# @see PostRequest#initialize
|
37
32
|
#
|
38
33
|
def post_json(*args)
|
39
|
-
send_request.(PostRequest.new(*args))
|
34
|
+
@send_request.(PostRequest.new(*args))
|
40
35
|
end
|
41
36
|
|
42
37
|
#
|
43
|
-
# @see
|
38
|
+
# @see PutRequest#initialize
|
44
39
|
#
|
45
40
|
def put_json(*args)
|
46
|
-
send_request.(PutRequest.new(*args))
|
41
|
+
@send_request.(PutRequest.new(*args))
|
47
42
|
end
|
48
43
|
end
|
49
44
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require 'lucid_shopify'
|
6
|
+
|
7
|
+
module LucidShopify
|
8
|
+
NotConfiguredError = Class.new(Error)
|
9
|
+
|
10
|
+
class << self
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
# TODO: *Config.dry_initializer.attributes (version 2.0.0+)
|
14
|
+
def_delegators :config, :api_key, :shared_secret, :scope, :billing_callback_uri, :webhook_uri
|
15
|
+
|
16
|
+
# @param config [Config]
|
17
|
+
attr_writer :config
|
18
|
+
|
19
|
+
#
|
20
|
+
# @return [Config]
|
21
|
+
#
|
22
|
+
# @raise [NotConfiguredError] if credentials are unset
|
23
|
+
#
|
24
|
+
def config
|
25
|
+
raise NotConfiguredError unless @config
|
26
|
+
|
27
|
+
@config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Config
|
32
|
+
extend Dry::Initializer
|
33
|
+
|
34
|
+
# @return [String]
|
35
|
+
param :api_key
|
36
|
+
# @return [String]
|
37
|
+
param :shared_secret
|
38
|
+
# @return [String]
|
39
|
+
param :scope
|
40
|
+
# @return [String]
|
41
|
+
param :billing_callback_uri
|
42
|
+
# @return [String]
|
43
|
+
param :webhook_uri
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/container'
|
4
|
+
|
5
|
+
require 'lucid_shopify'
|
6
|
+
|
7
|
+
module LucidShopify
|
8
|
+
Container = Dry::Container.new
|
9
|
+
|
10
|
+
# Services only (dependencies); no value objects, entities.
|
11
|
+
Container.register(:activate_charge) { ActivateCharge.new }
|
12
|
+
Container.register(:assert_callback) { AssertCallback.new }
|
13
|
+
Container.register(:assert_webhook) { AssertWebhook.new }
|
14
|
+
Container.register(:client) { Client.new }
|
15
|
+
Container.register(:create_all_webhooks) { CreateAllWebhooks.new }
|
16
|
+
Container.register(:create_charge) { CreateCharge.new }
|
17
|
+
Container.register(:create_webhook) { CreateWebhook.new }
|
18
|
+
Container.register(:delete_all_webhooks) { DeleteAllWebhooks.new }
|
19
|
+
Container.register(:delete_webhook) { DeleteWebhook.new }
|
20
|
+
Container.register(:fetch_access_token) { FetchAccessToken.new }
|
21
|
+
Container.register(:send_request) { SendRequest.new }
|
22
|
+
Container.register(:send_throttled_request) { SendThrottledRequest.new }
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_shopify/container'
|
4
|
+
|
5
|
+
module LucidShopify
|
6
|
+
class CreateAllWebhooks
|
7
|
+
#
|
8
|
+
# @param create_webhook [#call]
|
9
|
+
#
|
10
|
+
def initialize(create_webhook: Container[:create_webhook])
|
11
|
+
@create_webhook = create_webhook
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Create all webhooks for the shop. Shopify ignores any webhooks which
|
16
|
+
# already exist remotely.
|
17
|
+
#
|
18
|
+
# @param request_credentials [RequestCredentials]
|
19
|
+
#
|
20
|
+
def call(request_credentials)
|
21
|
+
LucidShopify.webhooks.map do |webhook|
|
22
|
+
Thread.new { @create_webhook.(request_credentials, webhook) }
|
23
|
+
end.map(&:value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
#
|
29
|
+
# Webhooks created for each shop.
|
30
|
+
#
|
31
|
+
# @return [Array<Hash>]
|
32
|
+
#
|
33
|
+
# @example
|
34
|
+
# LucidShopify.webhooks << {topic: 'orders/create', fields: 'id,line_items'}
|
35
|
+
#
|
36
|
+
def webhooks
|
37
|
+
@webhooks ||= []
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,26 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require 'lucid_shopify/client'
|
3
|
+
require 'lucid_shopify/container'
|
6
4
|
|
7
5
|
module LucidShopify
|
8
6
|
class CreateCharge
|
9
|
-
|
10
|
-
|
11
|
-
#
|
12
|
-
|
7
|
+
#
|
8
|
+
# @param client [#post_json]
|
9
|
+
#
|
10
|
+
def initialize(client: Container[:client])
|
11
|
+
@client = client
|
12
|
+
end
|
13
13
|
|
14
14
|
#
|
15
15
|
# Create a new recurring application charge.
|
16
16
|
#
|
17
17
|
# @param request_credentials [RequestCredentials]
|
18
|
-
# @param charge [
|
18
|
+
# @param charge [#to_h]
|
19
19
|
#
|
20
20
|
# @return [Hash] the pending charge
|
21
21
|
#
|
22
22
|
def call(request_credentials, charge)
|
23
|
-
data = client.post_json(request_credentials, '
|
23
|
+
data = @client.post_json(request_credentials, 'recurring_application_charges', charge.to_h)
|
24
24
|
|
25
25
|
data['recurring_application_charge']
|
26
26
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_shopify/container'
|
4
|
+
|
5
|
+
module LucidShopify
|
6
|
+
class CreateWebhook
|
7
|
+
#
|
8
|
+
# @param client [#post_json]
|
9
|
+
#
|
10
|
+
def initialize(client: Container[:client])
|
11
|
+
@client = client
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# @param request_credentials [RequestCredentials]
|
16
|
+
# @param webhook [Hash]
|
17
|
+
#
|
18
|
+
def call(request_credentials, webhook)
|
19
|
+
data = {**webhook, address: LucidShopify.config.webhook_uri}
|
20
|
+
|
21
|
+
@client.post_json(request_credentials, 'webhooks', webhook: data)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -22,7 +22,7 @@ module LucidShopify
|
|
22
22
|
# Call each of the handlers registered for the given topic in turn. See
|
23
23
|
# {#register} below for more on webhook handlers.
|
24
24
|
#
|
25
|
-
# @param webhook [
|
25
|
+
# @param webhook [Webhook]
|
26
26
|
#
|
27
27
|
def call(webhook)
|
28
28
|
handlers[webhook.topic]&.each { |handler| handler.(webhook) }
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_shopify/container'
|
4
|
+
|
5
|
+
module LucidShopify
|
6
|
+
class DeleteAllWebhooks
|
7
|
+
#
|
8
|
+
# @param client [#get]
|
9
|
+
# @param delete_webhook [#call]
|
10
|
+
#
|
11
|
+
def initialize(client: Container[:client],
|
12
|
+
delete_webhook: Container[:delete_webhook])
|
13
|
+
@client = client
|
14
|
+
@delete_webhook = delete_webhook
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Delete any existing webhooks.
|
19
|
+
#
|
20
|
+
# @param request_credentials [RequestCredentials]
|
21
|
+
#
|
22
|
+
def call(request_credentials)
|
23
|
+
webhooks = @client.get('webhooks')['webhooks']
|
24
|
+
|
25
|
+
webhooks.map do |webhook|
|
26
|
+
Thread.new { @delete_webhook.(request_credentials, webhook['id']) }
|
27
|
+
end.map(&:value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'lucid_shopify
|
3
|
+
require 'lucid_shopify'
|
4
4
|
|
5
5
|
module LucidShopify
|
6
6
|
class DeleteRequest < Request
|
7
|
+
#
|
8
|
+
# @private
|
7
9
|
#
|
8
10
|
# @param credentials [RequestCredentials]
|
9
11
|
# @param path [String] the endpoint relative to the base URL
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'lucid_shopify/container'
|
4
|
+
|
5
|
+
module LucidShopify
|
6
|
+
class DeleteWebhook
|
7
|
+
#
|
8
|
+
# @param client [#delete]
|
9
|
+
#
|
10
|
+
def initialize(client: Container[:client])
|
11
|
+
@client = client
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# @param request_credentials [RequestCredentials]
|
16
|
+
# @param id [Integer]
|
17
|
+
#
|
18
|
+
def call(request_credentials, id)
|
19
|
+
@client.delete(request_credentials, "webhooks/#{id}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,19 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require 'lucid_shopify/client'
|
3
|
+
require 'lucid_shopify/container'
|
6
4
|
|
7
5
|
module LucidShopify
|
8
6
|
class FetchAccessToken
|
9
|
-
Error = Class.new(
|
10
|
-
|
11
|
-
extend Dry::Initializer
|
7
|
+
Error = Class.new(Error)
|
12
8
|
|
13
|
-
#
|
14
|
-
|
15
|
-
#
|
16
|
-
|
9
|
+
#
|
10
|
+
# @param client [#post_json]
|
11
|
+
#
|
12
|
+
def initialize(client: Container[:client])
|
13
|
+
@client = client
|
14
|
+
end
|
17
15
|
|
18
16
|
#
|
19
17
|
# Exchange an authorization code for a new Shopify access token.
|
@@ -26,10 +24,10 @@ module LucidShopify
|
|
26
24
|
# @raise [Error] if the response is invalid
|
27
25
|
#
|
28
26
|
def call(request_credentials, authorization_code)
|
29
|
-
data = client.post_json(request_credentials, 'oauth/access_token', post_data(authorization_code))
|
27
|
+
data = @client.post_json(request_credentials, 'oauth/access_token', post_data(authorization_code))
|
30
28
|
|
31
29
|
raise Error if data['access_token'].nil?
|
32
|
-
raise Error if data['scope'] !=
|
30
|
+
raise Error if data['scope'] != LucidShopify.config.scope
|
33
31
|
|
34
32
|
data['access_token']
|
35
33
|
end
|
@@ -41,8 +39,8 @@ module LucidShopify
|
|
41
39
|
#
|
42
40
|
private def post_data(authorization_code)
|
43
41
|
{
|
44
|
-
client_id:
|
45
|
-
client_secret:
|
42
|
+
client_id: LucidShopify.config.api_key,
|
43
|
+
client_secret: LucidShopify.config.shared_secret,
|
46
44
|
code: authorization_code,
|
47
45
|
}
|
48
46
|
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'lucid_shopify
|
3
|
+
require 'lucid_shopify'
|
4
4
|
|
5
5
|
module LucidShopify
|
6
6
|
class GetRequest < Request
|
7
|
+
#
|
8
|
+
# @private
|
7
9
|
#
|
8
10
|
# @param credentials [RequestCredentials]
|
9
11
|
# @param path [String] the endpoint relative to the base URL
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'lucid_shopify
|
3
|
+
require 'lucid_shopify'
|
4
4
|
|
5
5
|
module LucidShopify
|
6
6
|
class PostRequest < Request
|
7
|
+
#
|
8
|
+
# @private
|
7
9
|
#
|
8
10
|
# @param credentials [RequestCredentials]
|
9
11
|
# @param path [String] the endpoint relative to the base URL
|