solidus_webhooks 0.1.0 → 0.2.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: 33eeb1647a31a48efa2e66d1cb4747938f4dafd467f5ac9c1dd1713ef88e0292
4
- data.tar.gz: 77f21a95ead4dbf098a7a92527354bced8078c9475edf522eee577ca8283e5e7
3
+ metadata.gz: d71609325f0e59694111c1efee4a131a283af6dfac87d9102a9e405dfca918ea
4
+ data.tar.gz: b9f80ecd1f6652d83915b2a49c083285df3e7e17c63b01414c273857daeadd44
5
5
  SHA512:
6
- metadata.gz: 3b55c38a880b83ff3d95d0622dc425973590a6b1051fe8f16a9f364713b552e7cf20755f413f9c25c89b2a893cd2ebb4892230837e3000d469cbe1fcfb39d33f
7
- data.tar.gz: f45d9569f09fd41009ae627bdfe10855d74bffd66a5c54daba52a2442b62be8307090e7946a0deb369f8792cf3eb82b939944457ff177e78ed1cee32fe178d7b
6
+ metadata.gz: 826c8abe905a3a69d7ea642b72a047534282db7bb60c3cdced93f897c2d62f887d72807aff7cb57cc5ec1082966b9602908c97d7ba2d866ad3ffb8aeb0f09ce1
7
+ data.tar.gz: 8fab3c87b593ca5743818ed8551296b85709813341fa5bba3ddd2561992138fc92ba984525b3080a5c30fd9d2b64f35f99d610cdcd885115f1732eadccbe4780
data/README.md CHANGED
@@ -108,6 +108,20 @@ Spree::RoleConfiguration.configure do |config|
108
108
  end
109
109
  ```
110
110
 
111
+ ### Accessing the API User
112
+
113
+ If you need to access the API user that is making the call to the webhook, you can just accept an additional argument in the callable handler.
114
+
115
+ Example:
116
+
117
+ ```ruby
118
+ SolidusWebhooks.config.register_webhook_handler :tracking_number, -> payload, user {
119
+ Rails.logger.info "Received payload from user #{user.email}: #{payload.to_json}"
120
+ # …
121
+ }
122
+ ```
123
+
124
+
111
125
  Installation
112
126
  ------------
113
127
 
@@ -4,10 +4,11 @@ class Spree::WebhooksController < Spree::Api::BaseController
4
4
  def receive
5
5
  webhook = Spree::Webhook.find(params[:id])
6
6
  payload = request.request_parameters["webhook"]
7
+ user = current_api_user
7
8
 
8
9
  authorize! :receive, webhook
9
10
 
10
- webhook.receive(payload)
11
+ webhook.receive(payload, user)
11
12
 
12
13
  head :ok
13
14
  end
@@ -4,8 +4,12 @@ class Spree::Webhook
4
4
 
5
5
  WebhookNotFound = Class.new(StandardError)
6
6
 
7
- def receive(payload)
8
- handler.call(payload)
7
+ def receive(payload, user)
8
+ if handler_arity == 1
9
+ handler.call(payload)
10
+ else
11
+ handler.call(payload, user)
12
+ end
9
13
  end
10
14
 
11
15
  def self.find(id)
@@ -16,4 +20,14 @@ class Spree::Webhook
16
20
 
17
21
  new(id: id, handler: handler)
18
22
  end
23
+
24
+ private
25
+
26
+ def handler_arity
27
+ if handler.respond_to?(:arity)
28
+ handler.arity
29
+ else
30
+ handler.method(:call).arity
31
+ end
32
+ end
19
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusWebhooks
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -3,45 +3,73 @@ require 'spec_helper'
3
3
  RSpec.feature "Can register a handler and receive Webhooks", type: :request do
4
4
  background do
5
5
  SolidusWebhooks.reset_config!
6
- SolidusWebhooks.config.register_webhook_handler :foo, foo_handler
7
- SolidusWebhooks.config.register_webhook_handler :bar, bar_handler
6
+ SolidusWebhooks.config.register_webhook_handler :proc, proc_handler
7
+ SolidusWebhooks.config.register_webhook_handler :method, method_handler
8
+ SolidusWebhooks.config.register_webhook_handler :user, user_handler
9
+ SolidusWebhooks.config.register_webhook_handler :splat, splat_handler
10
+ SolidusWebhooks.config.register_webhook_handler :method_and_user, method_and_user_handler
8
11
  end
9
12
 
10
- let(:foo_payloads) { [] }
11
- let(:bar_payloads) { [] }
13
+ let(:proc_payloads) { [] }
14
+ let(:method_payloads) { [] }
15
+ let(:user_payloads) { [] }
16
+ let(:splat_payloads) { [] }
17
+ let(:method_and_user_payloads) { [] }
12
18
 
13
- let(:foo_handler) { ->(payload) { foo_payloads << payload } }
14
- let(:bar_handler) { ->(payload) { bar_payloads << payload } }
19
+ let(:proc_handler) { ->(payload) { proc_payloads << payload } }
20
+ let(:method_handler) { Struct.new(:payloads) do
21
+ def call(payload) payloads << payload end
22
+ end.new(method_payloads) }
23
+ let(:user_handler) { ->(payload, user) { user_payloads << [payload, user] } }
24
+ let(:splat_handler) { ->(*args) { splat_payloads << args } }
25
+ let(:method_and_user_handler) { Struct.new(:payloads) do
26
+ def call(payload, user) payloads << payload end
27
+ end.new(method_and_user_payloads) }
15
28
 
16
- let(:token) { create(:admin_user, spree_api_key: "123").spree_api_key }
17
- let(:token_without_permission) { create(:user, spree_api_key: "456").spree_api_key }
29
+ let(:authorized_user) { create(:admin_user, spree_api_key: "123") }
30
+ let(:authorized_token) { authorized_user.spree_api_key }
31
+
32
+ let(:unauthorized_user) { create(:user, spree_api_key: "456") }
33
+ let(:unauthorized_token) { unauthorized_user.spree_api_key }
18
34
 
19
35
  scenario "calls the handler passing the payload" do
20
- post "/webhooks/foo?token=#{token}", as: :json, params: {a: 123}
36
+ post "/webhooks/proc?token=#{authorized_token}", as: :json, params: {a: 123}
37
+ expect(response).to have_http_status(:ok)
38
+
39
+ post "/webhooks/proc?token=#{authorized_token}", as: :json, params: {b: 456}
40
+ expect(response).to have_http_status(:ok)
41
+
42
+ post "/webhooks/method?token=#{authorized_token}", as: :json, params: {c: 789}
43
+ expect(response).to have_http_status(:ok)
44
+
45
+ post "/webhooks/user?token=#{authorized_token}", as: :json, params: {d: 012}
21
46
  expect(response).to have_http_status(:ok)
22
47
 
23
- post "/webhooks/foo?token=#{token}", as: :json, params: {b: 456}
48
+ post "/webhooks/splat?token=#{authorized_token}", as: :json, params: {e: 345}
24
49
  expect(response).to have_http_status(:ok)
25
50
 
26
- post "/webhooks/bar?token=#{token}", as: :json, params: {c: 789}
51
+ post "/webhooks/method_and_user?token=#{authorized_token}", as: :json, params: {f: 678}
27
52
  expect(response).to have_http_status(:ok)
28
53
 
29
- expect(foo_payloads).to eq([{'a' => 123}, {'b' => 456}])
30
- expect(bar_payloads).to eq([{'c' => 789}])
54
+ expect(proc_payloads).to eq([{'a' => 123}, {'b' => 456}])
55
+ expect(method_payloads).to eq([{'c' => 789}])
56
+ expect(user_payloads).to eq([[{'d' => 012}, authorized_user]])
57
+ expect(splat_payloads).to eq([[{'e' => 345}, authorized_user]])
58
+ expect(method_and_user_payloads).to eq([{'f' => 678}])
31
59
  end
32
60
 
33
61
  scenario "receives a bad handler id" do
34
- post "/webhooks/baz?token=#{token}", as: :json, params: {a: 123}
62
+ post "/webhooks/abc?token=#{authorized_token}", as: :json, params: {a: 123}
35
63
  expect(response).to have_http_status(:not_found)
36
64
  end
37
65
 
38
66
  scenario "refuses a bad token" do
39
- post "/webhooks/baz?token=b4d-t0k3n", as: :json, params: {a: 123}
67
+ post "/webhooks/user?token=b4d-t0k3n", as: :json, params: {a: 123}
40
68
  expect(response).to have_http_status(:unauthorized)
41
69
  end
42
70
 
43
71
  scenario "refuses a token without permissions" do
44
- post "/webhooks/foo?token=#{token_without_permission}", as: :json, params: {a: 123}
72
+ post "/webhooks/proc?token=#{unauthorized_token}", as: :json, params: {a: 123}
45
73
  expect(response).to have_http_status(:unauthorized)
46
74
  end
47
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_webhooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elia Schito
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core