lucid_shopify 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a53c313f841a7f88609457eb4b2c4f1a7651eee3d687097be04f98d75f36f4a0
4
- data.tar.gz: e3190ad427bd79f7bd0fe8f7ace431aad5544d9fe162b1098a5885d3a2959efc
3
+ metadata.gz: f12ecc227e47e723f176fabe2da11a246c3c3ab920f8428f6b78f63d383bce68
4
+ data.tar.gz: 3748d4c3d77de580e798fa2586d2369726fc125fc03d5092ad02d6ab25916bde
5
5
  SHA512:
6
- metadata.gz: 36cb4616edbe9d671ff6b34f3ee92d92e5a50bf20d7da05d2bfe783b307b8678d7c36d920174223ebf15739bd76577d001275db4c6f34805d09b8c485bd3aa14
7
- data.tar.gz: '04293660bb3c7abb9d4999924fbd311ccbf29de0e9d405338321e5e95bb2257a4861ead6be9c4684e7ca7424a9d46c015a95ae0a3b44354c2a7225bd877ece18'
6
+ metadata.gz: fc9ef0cb927c5c3657071fc498ac4059eed8b75336bc10b9fb3467484e95d75d0e1225f0fe57cfe53ad2868f8168f0faef36b4566f522f36b03f8586f42a7d2d
7
+ data.tar.gz: e014298a2e6f39dd17840b34c5444d60d93ebf44a20e2e045e9effce75d5d949211a7e8a42940f69baef37dba3fadc7e8002694a8ed99f5a20c3bc619357dbe0
data/README.md CHANGED
@@ -40,17 +40,14 @@ This is only useful during the OAuth2 process.
40
40
 
41
41
  Configure each webhook the app will create (if any):
42
42
 
43
- LucidShopify.webhooks << {topic: 'orders/create', fields: %w(id tags)}
44
- LucidShopify.webhooks << {topic: '...', fields: %w(...)}
43
+ LucidShopify.webhooks.register('orders/create', fields: 'id,tags'}
45
44
 
46
45
 
47
46
  ### Register webhook handlers
48
47
 
49
48
  For each webhook, register one or more handlers:
50
49
 
51
- delegate_webhooks = LucidShopify::DelegateWebhooks.default
52
-
53
- delegate_webhooks.register('orders/create', OrdersCreateWebhook.new)
50
+ LucidShopify.handlers.register('orders/create', OrdersCreateWebhook.new)
54
51
 
55
52
  See the inline method documentation for more detail.
56
53
 
@@ -59,7 +56,7 @@ to create a worker around something like this:
59
56
 
60
57
  webhook = LucidShopify::Webhook.new(myshopify_domain, topic, data)
61
58
 
62
- delegate_webhooks.(webhook)
59
+ LucidShopify.handlers.delegate(webhook)
63
60
 
64
61
 
65
62
  ### Create and delete webhooks
@@ -23,18 +23,4 @@ module LucidShopify
23
23
  end.map(&:value)
24
24
  end
25
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
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucidShopify
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LucidShopify
4
+ class WebhookHandlerList
5
+ def initialize
6
+ @handlers = {}
7
+ end
8
+
9
+ #
10
+ # Register a handler for a webhook topic. The callable handler should
11
+ # receive a single {Webhook} argument.
12
+ #
13
+ # @param topic [String]
14
+ # @param handler [#call]
15
+ #
16
+ def register(topic, handler = nil, &block)
17
+ raise ArgumentError unless nil ^ handler ^ block
18
+
19
+ handler = block if block
20
+
21
+ @handlers[topic] ||= []
22
+ @handlers[topic] << handler
23
+
24
+ nil
25
+ end
26
+
27
+ #
28
+ # @param topic [String]
29
+ #
30
+ def [](topic)
31
+ @handlers[topic] || []
32
+ end
33
+
34
+ #
35
+ # Call each of the handlers registered for the given topic in turn.
36
+ #
37
+ # @param webhook [Webhook]
38
+ #
39
+ def delegate(webhook)
40
+ self[webhook.topic].each { |handler| handler.(webhook) }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LucidShopify
4
+ class WebhookList
5
+ include Enumerable
6
+
7
+ def initialize
8
+ @webhooks = []
9
+ end
10
+
11
+ #
12
+ # @yield [Hash]
13
+ #
14
+ def each(&block)
15
+ @webhooks.each(&block)
16
+ end
17
+
18
+ #
19
+ # @param topic [String]
20
+ # @param fields [String] e.g. 'id,tags'
21
+ #
22
+ def register(topic, fields: nil)
23
+ @webhooks << {}.tap do |webhook|
24
+ webhook[:topic] = topic
25
+ webhook[:fields] = fields if fields
26
+ end
27
+
28
+ nil
29
+ end
30
+ end
31
+ end
data/lib/lucid_shopify.rb CHANGED
@@ -13,7 +13,6 @@ module LucidShopify
13
13
  autoload :CreateAllWebhooks, 'lucid_shopify/create_all_webhooks'
14
14
  autoload :CreateCharge, 'lucid_shopify/create_charge'
15
15
  autoload :CreateWebhook, 'lucid_shopify/create_webhook'
16
- autoload :DelegateWebhooks, 'lucid_shopify/delegate_webhooks'
17
16
  autoload :DeleteAllWebhooks, 'lucid_shopify/delete_all_webhooks'
18
17
  autoload :DeleteRequest, 'lucid_shopify/delete_request'
19
18
  autoload :DeleteWebhook, 'lucid_shopify/delete_webhook'
@@ -27,8 +26,40 @@ module LucidShopify
27
26
  autoload :Response, 'lucid_shopify/response'
28
27
  autoload :Result, 'lucid_shopify/result'
29
28
  autoload :SendRequest, 'lucid_shopify/send_request'
30
- autoload :ThrottledRequest, 'lucid_shopify/send_throttled_request'
29
+ autoload :SendThrottledRequest, 'lucid_shopify/send_throttled_request'
31
30
  autoload :VERSION, 'lucid_shopify/version'
32
31
  autoload :Webhook, 'lucid_shopify/webhook'
33
- autoload :Webhooks, 'lucid_shopify/webhooks'
32
+ autoload :WebhookHandlerList, 'lucid_shopify/webhook_handler_list'
33
+ autoload :WebhookList, 'lucid_shopify/webhook_list'
34
+
35
+ class << self
36
+ #
37
+ # Webhooks created for each shop.
38
+ #
39
+ # @return [WebhookList]
40
+ #
41
+ # @example
42
+ # LucidShopify.webhooks.register('orders/create', fields: 'id,tags')
43
+ #
44
+ def webhooks
45
+ @webhooks ||= WebhookList.new
46
+ end
47
+
48
+ #
49
+ # Handlers for webhook topics.
50
+ #
51
+ # @return [WebhookHandlerList]
52
+ #
53
+ # @example
54
+ # LucidShopify.handlers.register('orders/create', OrdersCreateWebhook.new)
55
+ #
56
+ # @example Call topic handlers
57
+ # webhook = Webhook.new(myshopify_domain, topic, data)
58
+ #
59
+ # LucidShopify.handlers.delegate(webhook)
60
+ #
61
+ def handlers
62
+ @handlers ||= WebhookHandlerList.new
63
+ end
64
+ end
34
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucid_shopify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelsey Judson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
11
+ date: 2018-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -126,7 +126,6 @@ files:
126
126
  - lib/lucid_shopify/create_all_webhooks.rb
127
127
  - lib/lucid_shopify/create_charge.rb
128
128
  - lib/lucid_shopify/create_webhook.rb
129
- - lib/lucid_shopify/delegate_webhooks.rb
130
129
  - lib/lucid_shopify/delete_all_webhooks.rb
131
130
  - lib/lucid_shopify/delete_request.rb
132
131
  - lib/lucid_shopify/delete_webhook.rb
@@ -142,6 +141,8 @@ files:
142
141
  - lib/lucid_shopify/send_throttled_request.rb
143
142
  - lib/lucid_shopify/version.rb
144
143
  - lib/lucid_shopify/webhook.rb
144
+ - lib/lucid_shopify/webhook_handler_list.rb
145
+ - lib/lucid_shopify/webhook_list.rb
145
146
  homepage: https://github.com/lucidnz/gem-lucid_shopify
146
147
  licenses:
147
148
  - ISC
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module LucidShopify
4
- class DelegateWebhooks
5
- class << self
6
- #
7
- # @return [DelegateWebhooks]
8
- #
9
- def default
10
- @default ||= new
11
- end
12
- end
13
-
14
- def initialize
15
- @handlers = {}
16
- end
17
-
18
- # @return [Hash<String, Array<#call>>]
19
- attr_reader :handlers
20
-
21
- #
22
- # Call each of the handlers registered for the given topic in turn. See
23
- # {#register} below for more on webhook handlers.
24
- #
25
- # @param webhook [Webhook]
26
- #
27
- def call(webhook)
28
- handlers[webhook.topic]&.each { |handler| handler.(webhook) }
29
- end
30
-
31
- #
32
- # Register a handler for a webhook topic. The callable handler will be
33
- # called with the argument passed to {#call}.
34
- #
35
- # @param topic [String] e.g. 'orders/create'
36
- # @param handler [#call]
37
- #
38
- def register(topic, handler)
39
- handlers[topic] ||= []
40
- handlers[topic] << handler
41
- end
42
- end
43
- end