solidus_webhooks 0.0.1.v0.1.0.beta.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +35 -0
- data/.gem_release.yml +5 -0
- data/.github/stale.yml +17 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +7 -0
- data/Gemfile +33 -0
- data/LICENSE +26 -0
- data/README.md +176 -0
- data/Rakefile +6 -0
- data/app/assets/javascripts/spree/backend/solidus_webhooks.js +2 -0
- data/app/assets/javascripts/spree/frontend/solidus_webhooks.js +2 -0
- data/app/assets/stylesheets/spree/backend/solidus_webhooks.css +4 -0
- data/app/assets/stylesheets/spree/frontend/solidus_webhooks.css +4 -0
- data/app/controllers/spree/webhooks_controller.rb +14 -0
- data/app/models/spree/webhook.rb +19 -0
- data/bin/console +17 -0
- data/bin/r +13 -0
- data/bin/rake +7 -0
- data/bin/sandbox +84 -0
- data/bin/sandbox_rails +18 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +5 -0
- data/config/routes.rb +5 -0
- data/lib/generators/solidus_webhooks/install/install_generator.rb +32 -0
- data/lib/solidus_webhooks/configuration.rb +30 -0
- data/lib/solidus_webhooks/engine.rb +19 -0
- data/lib/solidus_webhooks/factories.rb +4 -0
- data/lib/solidus_webhooks/version.rb +5 -0
- data/lib/solidus_webhooks.rb +8 -0
- data/solidus_webhooks.gemspec +35 -0
- data/spec/features/can_register_a_handler_and_receive_webhooks_spec.rb +47 -0
- data/spec/spec_helper.rb +24 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ee15a8db0ac265823a8f226cabbd4a28881cf04191e27073abf252b4f554097a
|
4
|
+
data.tar.gz: b3888c9cfd6afa9bb6a513223274f60665013541e2c5a8e5d49970320cb0b258
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd1a0767f65439df1ff2273b36c1df0f04c492ccfd31ec04d676f9492eed61bfe2296890bec228fd3d28ad55b53f8032205bfbc07bb40cf966d3fc5031ccd37b
|
7
|
+
data.tar.gz: a47daaba62f5c9480c2330fd21c261844d35b55d63ff9d057e42fec129a002aee7d842e5e2c8a55eb214b827c2c30dc6c8fa7527c6942d70b74973ee626b2f30
|
@@ -0,0 +1,35 @@
|
|
1
|
+
version: 2.1
|
2
|
+
|
3
|
+
orbs:
|
4
|
+
# Always take the latest version of the orb, this allows us to
|
5
|
+
# run specs against Solidus supported versions only without the need
|
6
|
+
# to change this configuration every time a Solidus version is released
|
7
|
+
# or goes EOL.
|
8
|
+
solidusio_extensions: solidusio/extensions@volatile
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
run-specs-with-postgres:
|
12
|
+
executor: solidusio_extensions/postgres
|
13
|
+
steps:
|
14
|
+
- solidusio_extensions/run-tests
|
15
|
+
run-specs-with-mysql:
|
16
|
+
executor: solidusio_extensions/mysql
|
17
|
+
steps:
|
18
|
+
- solidusio_extensions/run-tests
|
19
|
+
|
20
|
+
workflows:
|
21
|
+
"Run specs on supported Solidus versions":
|
22
|
+
jobs:
|
23
|
+
- run-specs-with-postgres
|
24
|
+
- run-specs-with-mysql
|
25
|
+
"Weekly run specs against master":
|
26
|
+
triggers:
|
27
|
+
- schedule:
|
28
|
+
cron: "0 0 * * 4" # every Thursday
|
29
|
+
filters:
|
30
|
+
branches:
|
31
|
+
only:
|
32
|
+
- master
|
33
|
+
jobs:
|
34
|
+
- run-specs-with-postgres
|
35
|
+
- run-specs-with-mysql
|
data/.gem_release.yml
ADDED
data/.github/stale.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Number of days of inactivity before an issue becomes stale
|
2
|
+
daysUntilStale: 60
|
3
|
+
# Number of days of inactivity before a stale issue is closed
|
4
|
+
daysUntilClose: 7
|
5
|
+
# Issues with these labels will never be considered stale
|
6
|
+
exemptLabels:
|
7
|
+
- pinned
|
8
|
+
- security
|
9
|
+
# Label to use when marking an issue as stale
|
10
|
+
staleLabel: wontfix
|
11
|
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
12
|
+
markComment: >
|
13
|
+
This issue has been automatically marked as stale because it has not had
|
14
|
+
recent activity. It will be closed if no further activity occurs. Thank you
|
15
|
+
for your contributions.
|
16
|
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
17
|
+
closeComment: false
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
5
|
+
|
6
|
+
branch = ENV.fetch('SOLIDUS_BRANCH', 'master')
|
7
|
+
gem 'solidus', github: 'solidusio/solidus', branch: branch
|
8
|
+
|
9
|
+
# Needed to help Bundler figure out how to resolve dependencies,
|
10
|
+
# otherwise it takes forever to resolve them.
|
11
|
+
# See https://github.com/bundler/bundler/issues/6677
|
12
|
+
gem 'rails', '>0.a'
|
13
|
+
|
14
|
+
# Provides basic authentication functionality for testing parts of your engine
|
15
|
+
gem 'solidus_auth_devise'
|
16
|
+
|
17
|
+
case ENV['DB']
|
18
|
+
when 'mysql'
|
19
|
+
gem 'mysql2'
|
20
|
+
when 'postgresql'
|
21
|
+
gem 'pg'
|
22
|
+
else
|
23
|
+
gem 'sqlite3'
|
24
|
+
end
|
25
|
+
|
26
|
+
gemspec
|
27
|
+
|
28
|
+
# Use a local Gemfile to include development dependencies that might not be
|
29
|
+
# relevant for the project or for other contributors, e.g. pry-byebug.
|
30
|
+
#
|
31
|
+
# We use `send` instead of calling `eval_gemfile` to work around an issue with
|
32
|
+
# how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658.
|
33
|
+
send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local'
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2020 Nebulab srls
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name Solidus nor the names of its contributors may be used to
|
13
|
+
endorse or promote products derived from this software without specific
|
14
|
+
prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
SolidusWebhooks
|
2
|
+
===============
|
3
|
+
|
4
|
+
Provides comprehensive Webhook support for Solidus, with a simple and powerful way to register them and route payloads to appropriate actions, either synchronous or delayed.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-----
|
8
|
+
|
9
|
+
A Webhook receiver is just a callable and can be registered in the Solidus configuration as follows:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
SolidusWebhooks.config.register_webhook_handler :tracking_number, -> payload {
|
13
|
+
order = Spree::Order.find_by!(number: payload[:order])
|
14
|
+
shipment = order.shipments.find_by!(number: payload[:shipment])
|
15
|
+
shipment.update!(tracking: payload[:tracking])
|
16
|
+
}
|
17
|
+
```
|
18
|
+
|
19
|
+
This will enable sending `POST` requests to `/webhooks/tracking-number` with a JSON payload like this:
|
20
|
+
|
21
|
+
```json
|
22
|
+
{
|
23
|
+
"order": "R1234567890",
|
24
|
+
"shipment": "S1234567890",
|
25
|
+
"tracking": "T123-456-789"
|
26
|
+
}
|
27
|
+
```
|
28
|
+
|
29
|
+
### Handlers requirements
|
30
|
+
|
31
|
+
The only requirement on handlers is for them to respond to `#call` and accept a payload.
|
32
|
+
|
33
|
+
Example:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class TrackingNumberHandler
|
37
|
+
def call(payload)
|
38
|
+
order = Spree::Order.find_by!(number: payload[:order])
|
39
|
+
shipment = order.shipments.find_by!(number: payload[:shipment])
|
40
|
+
shipment.update!(tracking: payload[:tracking])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
SolidusWebhooks.config.register_webhook_handler :tracking_number, TrackingNumberHandler
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
### Making the handler asynchronous
|
49
|
+
|
50
|
+
To make a handler asynchronous just make its implementation internally call your preferred job handler (e.g. ActiveJob). In most cases you'll want to filter, prepare, and validate the payload for the job of your choice, to avoid ingesting and invalid input.
|
51
|
+
|
52
|
+
Example:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
SolidusWebhooks.config.register_webhook_handler :tracking_number, -> payload {
|
56
|
+
UpdateTrackingNumberJob.perform_later(
|
57
|
+
order: payload.fetch(:order)
|
58
|
+
shipment: payload.fetch(:shipment)
|
59
|
+
tracking: payload.fetch(:tracking)
|
60
|
+
)
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
|
65
|
+
### Payload routing
|
66
|
+
|
67
|
+
If your handler can receive different kind of payloads the most common technique is to route them to appropriate sub-handlers (that can be an ActiveJob class or a service class).
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
SolidusWebhooks.config.register_webhook_handler :tracking_number, -> payload {
|
71
|
+
case payload[:tracking]
|
72
|
+
when /^FOO(\d+-)+/
|
73
|
+
UpdateFooTrackingNumberJob.perform_later(
|
74
|
+
order: payload.fetch(:order)
|
75
|
+
shipment: payload.fetch(:shipment)
|
76
|
+
tracking: payload.fetch(:tracking)
|
77
|
+
)
|
78
|
+
when /^BAR(\d+-)+/
|
79
|
+
UpdateBarTrackingNumberJob.perform_later(
|
80
|
+
order: payload.fetch(:order)
|
81
|
+
shipment: payload.fetch(:shipment)
|
82
|
+
tracking: payload.fetch(:tracking)
|
83
|
+
)
|
84
|
+
else raise "unknown tracking service"
|
85
|
+
end
|
86
|
+
}
|
87
|
+
```
|
88
|
+
|
89
|
+
### Restricting Permissions
|
90
|
+
|
91
|
+
It's good practice not to use admin-user tokens for webhooks, instead you should define a permission set tied to the webhook handler you're providing. Use the standard Solidus permission-sets to do that.
|
92
|
+
|
93
|
+
Example:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
module ReceiveTrackingWebhookPermission < Spree::PermissionSets::Base
|
97
|
+
def activate!
|
98
|
+
can :receive, Spree::Webhook do |webhook|
|
99
|
+
webhook.id == :tracking_number
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
Spree::RoleConfiguration.configure do |config|
|
105
|
+
config.assign_permissions :foo_tracking_service, %w[
|
106
|
+
ReceiveTrackingWebhookPermission
|
107
|
+
]
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
Installation
|
112
|
+
------------
|
113
|
+
|
114
|
+
Add solidus_webhooks to your Gemfile:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
gem 'solidus_webhooks'
|
118
|
+
```
|
119
|
+
|
120
|
+
Bundle your dependencies and run the installation generator:
|
121
|
+
|
122
|
+
```shell
|
123
|
+
bundle
|
124
|
+
bundle exec rails g solidus_webhooks:install
|
125
|
+
```
|
126
|
+
|
127
|
+
Testing
|
128
|
+
-------
|
129
|
+
|
130
|
+
First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using `bin/rake extension:test_app`.
|
131
|
+
|
132
|
+
```shell
|
133
|
+
bundle
|
134
|
+
bin/rake
|
135
|
+
```
|
136
|
+
|
137
|
+
To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run
|
138
|
+
|
139
|
+
```shell
|
140
|
+
bundle exec rubocop
|
141
|
+
```
|
142
|
+
|
143
|
+
When testing your application's integration with this extension you may use its factories.
|
144
|
+
Simply add this require statement to your spec_helper:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
require 'solidus_webhooks/factories'
|
148
|
+
```
|
149
|
+
|
150
|
+
Sandbox app
|
151
|
+
-----------
|
152
|
+
|
153
|
+
To run this extension in a sandboxed Solidus application you can run `bin/sandbox`
|
154
|
+
The path for the sandbox app is `./sandbox` and `bin/rails` will forward any Rails command
|
155
|
+
to `sandbox/bin/rails`.
|
156
|
+
|
157
|
+
Example:
|
158
|
+
|
159
|
+
```shell
|
160
|
+
$ bin/rails server
|
161
|
+
=> Booting Puma
|
162
|
+
=> Rails 6.0.2.1 application starting in development
|
163
|
+
* Listening on tcp://127.0.0.1:3000
|
164
|
+
Use Ctrl-C to stop
|
165
|
+
```
|
166
|
+
|
167
|
+
Releasing
|
168
|
+
---------
|
169
|
+
|
170
|
+
Your new extension version can be released using `gem-release` like this:
|
171
|
+
|
172
|
+
```shell
|
173
|
+
bundle exec gem bump -v VERSION --tag --push --remote upstream && gem release
|
174
|
+
```
|
175
|
+
|
176
|
+
Copyright (c) 2020 Nebulab srls, released under the New BSD License
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Spree::WebhooksController < Spree::Api::BaseController
|
2
|
+
rescue_from(Spree::Webhook::WebhookNotFound) { head :not_found }
|
3
|
+
|
4
|
+
def receive
|
5
|
+
webhook = Spree::Webhook.find(params[:id])
|
6
|
+
payload = request.request_parameters["webhook"]
|
7
|
+
|
8
|
+
authorize! :receive, webhook
|
9
|
+
|
10
|
+
webhook.receive(payload)
|
11
|
+
|
12
|
+
head :ok
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Spree::Webhook
|
2
|
+
include ActiveModel::Model
|
3
|
+
attr_accessor :handler, :id
|
4
|
+
|
5
|
+
WebhookNotFound = Class.new(StandardError)
|
6
|
+
|
7
|
+
def receive(payload)
|
8
|
+
handler.call(payload)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find(id)
|
12
|
+
id = id.to_sym # normalize incoming ids
|
13
|
+
|
14
|
+
handler = SolidusWebhooks.config.find_webhook_handler(id) or
|
15
|
+
raise WebhookNotFound, "Cannot find a webhook handler for #{id.inspect}"
|
16
|
+
|
17
|
+
new(id: id, handler: handler)
|
18
|
+
end
|
19
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require "bundler/setup"
|
6
|
+
require "solidus_webhooks"
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
$LOAD_PATH.unshift(*Dir["#{__dir__}/../app/*"])
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
# require "pry"
|
14
|
+
# Pry.start
|
15
|
+
|
16
|
+
require "irb"
|
17
|
+
IRB.start(__FILE__)
|
data/bin/r
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
3
|
+
# installed from the root of your application.
|
4
|
+
|
5
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
6
|
+
ENGINE_PATH = File.expand_path('../lib/solidus_webhooks/engine', __dir__)
|
7
|
+
|
8
|
+
# Set up gems listed in the Gemfile.
|
9
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
10
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
11
|
+
|
12
|
+
require 'rails/all'
|
13
|
+
require 'rails/engine/commands'
|
data/bin/rake
ADDED
data/bin/sandbox
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -e
|
4
|
+
|
5
|
+
case "$DB" in
|
6
|
+
postgres|postgresql)
|
7
|
+
RAILSDB="postgresql"
|
8
|
+
;;
|
9
|
+
mysql)
|
10
|
+
RAILSDB="mysql"
|
11
|
+
;;
|
12
|
+
sqlite|'')
|
13
|
+
RAILSDB="sqlite3"
|
14
|
+
;;
|
15
|
+
*)
|
16
|
+
echo "Invalid DB specified: $DB"
|
17
|
+
exit 1
|
18
|
+
;;
|
19
|
+
esac
|
20
|
+
|
21
|
+
if [ ! -z $SOLIDUS_BRANCH ]
|
22
|
+
then
|
23
|
+
BRANCH=$SOLIDUS_BRANCH
|
24
|
+
else
|
25
|
+
BRANCH="master"
|
26
|
+
fi
|
27
|
+
|
28
|
+
extension_name="solidus_webhooks"
|
29
|
+
|
30
|
+
# Stay away from the bundler env of the containing extension.
|
31
|
+
function unbundled {
|
32
|
+
ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
|
33
|
+
}
|
34
|
+
|
35
|
+
rm -rf ./sandbox
|
36
|
+
unbundled bundle exec rails new sandbox --database="$RAILSDB" \
|
37
|
+
--skip-bundle \
|
38
|
+
--skip-git \
|
39
|
+
--skip-keeps \
|
40
|
+
--skip-rc \
|
41
|
+
--skip-spring \
|
42
|
+
--skip-test \
|
43
|
+
--skip-javascript
|
44
|
+
|
45
|
+
if [ ! -d "sandbox" ]; then
|
46
|
+
echo 'sandbox rails application failed'
|
47
|
+
exit 1
|
48
|
+
fi
|
49
|
+
|
50
|
+
cd ./sandbox
|
51
|
+
cat <<RUBY >> Gemfile
|
52
|
+
gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
|
53
|
+
gem 'solidus_auth_devise', '>= 2.1.0'
|
54
|
+
gem 'rails-i18n'
|
55
|
+
gem 'solidus_i18n'
|
56
|
+
|
57
|
+
gem '$extension_name', path: '..'
|
58
|
+
|
59
|
+
group :test, :development do
|
60
|
+
platforms :mri do
|
61
|
+
gem 'pry-byebug'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
RUBY
|
65
|
+
|
66
|
+
unbundled bundle install --gemfile Gemfile
|
67
|
+
|
68
|
+
unbundled bundle exec rake db:drop db:create
|
69
|
+
|
70
|
+
unbundled bundle exec rails generate spree:install \
|
71
|
+
--auto-accept \
|
72
|
+
--user_class=Spree::User \
|
73
|
+
--enforce_available_locales=true \
|
74
|
+
--with-authentication=false \
|
75
|
+
$@
|
76
|
+
|
77
|
+
unbundled bundle exec rails generate solidus:auth:install
|
78
|
+
|
79
|
+
echo
|
80
|
+
echo "🚀 Sandbox app successfully created for $extension_name!"
|
81
|
+
echo "🚀 Using $RAILSDB and Solidus $BRANCH"
|
82
|
+
echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
|
83
|
+
echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
|
84
|
+
echo "🚀 This app is intended for test purposes."
|
data/bin/sandbox_rails
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
app_root = 'sandbox'
|
6
|
+
|
7
|
+
unless File.exist? "#{app_root}/bin/rails"
|
8
|
+
warn 'Creating the sandbox app...'
|
9
|
+
Dir.chdir "#{__dir__}/.." do
|
10
|
+
system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
|
11
|
+
warn 'Automatic creation of the sandbox app failed'
|
12
|
+
exit 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Dir.chdir app_root
|
18
|
+
exec 'bin/rails', *ARGV
|
data/bin/setup
ADDED
data/config/routes.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusWebhooks
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
class_option :auto_run_migrations, type: :boolean, default: false
|
7
|
+
|
8
|
+
def add_javascripts
|
9
|
+
append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_webhooks\n" # rubocop:disable Metrics/LineLength
|
10
|
+
append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_webhooks\n" # rubocop:disable Metrics/LineLength
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_stylesheets
|
14
|
+
inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_webhooks\n", before: %r{\*/}, verbose: true # rubocop:disable Metrics/LineLength
|
15
|
+
inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_webhooks\n", before: %r{\*/}, verbose: true # rubocop:disable Metrics/LineLength
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_migrations
|
19
|
+
run 'bin/rails railties:install:migrations FROM=solidus_webhooks'
|
20
|
+
end
|
21
|
+
|
22
|
+
def run_migrations
|
23
|
+
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Metrics/LineLength
|
24
|
+
if run_migrations
|
25
|
+
run 'bin/rails db:migrate'
|
26
|
+
else
|
27
|
+
puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SolidusWebhooks
|
2
|
+
class Configuration
|
3
|
+
def initialize
|
4
|
+
@handlers = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def register_webhook_handler(id, handler)
|
8
|
+
unless handler.respond_to? :call
|
9
|
+
raise Spree::Webhook::InvalidHandler,
|
10
|
+
"Please provide a handler that responds to #call, got: #{handler.inspect}"
|
11
|
+
end
|
12
|
+
|
13
|
+
@handlers[id.to_sym] = handler
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_webhook_handler(id)
|
17
|
+
@handlers[id.to_sym]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config
|
22
|
+
@config
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.reset_config!
|
26
|
+
@config = Configuration.new
|
27
|
+
end
|
28
|
+
|
29
|
+
reset_config! # initialize the extension
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spree/core'
|
4
|
+
require 'solidus_webhooks'
|
5
|
+
|
6
|
+
module SolidusWebhooks
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
include SolidusSupport::EngineExtensions
|
9
|
+
|
10
|
+
isolate_namespace ::Spree
|
11
|
+
|
12
|
+
engine_name 'solidus_webhooks'
|
13
|
+
|
14
|
+
# use rspec for tests
|
15
|
+
config.generators do |g|
|
16
|
+
g.test_framework :rspec
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/solidus_webhooks/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'solidus_webhooks'
|
7
|
+
spec.version = SolidusWebhooks::VERSION
|
8
|
+
spec.authors = ['Elia Schito']
|
9
|
+
spec.email = 'contact@solidus.io'
|
10
|
+
|
11
|
+
spec.summary = 'Webhooks support for Solidus'
|
12
|
+
spec.homepage = 'https://github.com/solidusio-contrib/solidus_webhooks#readme'
|
13
|
+
spec.license = 'BSD-3-Clause'
|
14
|
+
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['source_code_uri'] = 'https://github.com/solidusio-contrib/solidus_webhooks'
|
17
|
+
spec.metadata['changelog_uri'] = 'https://github.com/solidusio-contrib/solidus_webhooks/releases'
|
18
|
+
|
19
|
+
spec.required_ruby_version = Gem::Requirement.new('~> 2.4')
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
|
24
|
+
|
25
|
+
spec.files = files.grep_v(%r{^(test|spec|features)/})
|
26
|
+
spec.test_files = files.grep(%r{^(test|spec|features)/})
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 3']
|
32
|
+
spec.add_dependency 'solidus_support', '~> 0.5.0'
|
33
|
+
|
34
|
+
spec.add_development_dependency 'solidus_dev_support'
|
35
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.feature "Can register a handler and receive Webhooks", type: :request do
|
4
|
+
background do
|
5
|
+
SolidusWebhooks.reset_config!
|
6
|
+
SolidusWebhooks.config.register_webhook_handler :foo, foo_handler
|
7
|
+
SolidusWebhooks.config.register_webhook_handler :bar, bar_handler
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:foo_payloads) { [] }
|
11
|
+
let(:bar_payloads) { [] }
|
12
|
+
|
13
|
+
let(:foo_handler) { ->(payload) { foo_payloads << payload } }
|
14
|
+
let(:bar_handler) { ->(payload) { bar_payloads << payload } }
|
15
|
+
|
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 }
|
18
|
+
|
19
|
+
scenario "calls the handler passing the payload" do
|
20
|
+
post "/webhooks/foo?token=#{token}", as: :json, params: {a: 123}
|
21
|
+
expect(response).to have_http_status(:ok)
|
22
|
+
|
23
|
+
post "/webhooks/foo?token=#{token}", as: :json, params: {b: 456}
|
24
|
+
expect(response).to have_http_status(:ok)
|
25
|
+
|
26
|
+
post "/webhooks/bar?token=#{token}", as: :json, params: {c: 789}
|
27
|
+
expect(response).to have_http_status(:ok)
|
28
|
+
|
29
|
+
expect(foo_payloads).to eq([{'a' => 123}, {'b' => 456}])
|
30
|
+
expect(bar_payloads).to eq([{'c' => 789}])
|
31
|
+
end
|
32
|
+
|
33
|
+
scenario "receives a bad handler id" do
|
34
|
+
post "/webhooks/baz?token=#{token}", as: :json, params: {a: 123}
|
35
|
+
expect(response).to have_http_status(:not_found)
|
36
|
+
end
|
37
|
+
|
38
|
+
scenario "refuses a bad token" do
|
39
|
+
post "/webhooks/baz?token=b4d-t0k3n", as: :json, params: {a: 123}
|
40
|
+
expect(response).to have_http_status(:unauthorized)
|
41
|
+
end
|
42
|
+
|
43
|
+
scenario "refuses a token without permissions" do
|
44
|
+
post "/webhooks/foo?token=#{token_without_permission}", as: :json, params: {a: 123}
|
45
|
+
expect(response).to have_http_status(:unauthorized)
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Configure Rails Environment
|
4
|
+
ENV['RAILS_ENV'] = 'test'
|
5
|
+
|
6
|
+
# Run Coverage report
|
7
|
+
require 'solidus_dev_support/rspec/coverage'
|
8
|
+
|
9
|
+
require File.expand_path('dummy/config/environment.rb', __dir__)
|
10
|
+
|
11
|
+
# Requires factories and other useful helpers defined in spree_core.
|
12
|
+
require 'solidus_dev_support/rspec/feature_helper'
|
13
|
+
|
14
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
15
|
+
# in spec/support/ and its subdirectories.
|
16
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
|
17
|
+
|
18
|
+
# Requires factories defined in lib/solidus_webhooks/factories.rb
|
19
|
+
require 'solidus_webhooks/factories'
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.infer_spec_type_from_file_location!
|
23
|
+
config.use_transactional_fixtures = false
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solidus_webhooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.v0.1.0.beta.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elia Schito
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: solidus_core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.0.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: solidus_support
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.5.0
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.5.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: solidus_dev_support
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
description:
|
62
|
+
email: contact@solidus.io
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- ".circleci/config.yml"
|
68
|
+
- ".gem_release.yml"
|
69
|
+
- ".github/stale.yml"
|
70
|
+
- ".gitignore"
|
71
|
+
- ".rspec"
|
72
|
+
- ".rubocop.yml"
|
73
|
+
- Gemfile
|
74
|
+
- LICENSE
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- app/assets/javascripts/spree/backend/solidus_webhooks.js
|
78
|
+
- app/assets/javascripts/spree/frontend/solidus_webhooks.js
|
79
|
+
- app/assets/stylesheets/spree/backend/solidus_webhooks.css
|
80
|
+
- app/assets/stylesheets/spree/frontend/solidus_webhooks.css
|
81
|
+
- app/controllers/spree/webhooks_controller.rb
|
82
|
+
- app/models/spree/webhook.rb
|
83
|
+
- bin/console
|
84
|
+
- bin/r
|
85
|
+
- bin/rake
|
86
|
+
- bin/sandbox
|
87
|
+
- bin/sandbox_rails
|
88
|
+
- bin/setup
|
89
|
+
- config/locales/en.yml
|
90
|
+
- config/routes.rb
|
91
|
+
- lib/generators/solidus_webhooks/install/install_generator.rb
|
92
|
+
- lib/solidus_webhooks.rb
|
93
|
+
- lib/solidus_webhooks/configuration.rb
|
94
|
+
- lib/solidus_webhooks/engine.rb
|
95
|
+
- lib/solidus_webhooks/factories.rb
|
96
|
+
- lib/solidus_webhooks/version.rb
|
97
|
+
- solidus_webhooks.gemspec
|
98
|
+
- spec/features/can_register_a_handler_and_receive_webhooks_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: https://github.com/solidusio-contrib/solidus_webhooks#readme
|
101
|
+
licenses:
|
102
|
+
- BSD-3-Clause
|
103
|
+
metadata:
|
104
|
+
homepage_uri: https://github.com/solidusio-contrib/solidus_webhooks#readme
|
105
|
+
source_code_uri: https://github.com/solidusio-contrib/solidus_webhooks
|
106
|
+
changelog_uri: https://github.com/solidusio-contrib/solidus_webhooks/releases
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '2.4'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">"
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 1.3.1
|
121
|
+
requirements: []
|
122
|
+
rubygems_version: 3.1.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Webhooks support for Solidus
|
126
|
+
test_files:
|
127
|
+
- spec/features/can_register_a_handler_and_receive_webhooks_spec.rb
|
128
|
+
- spec/spec_helper.rb
|