square_event 1.0.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +35 -0
- data/Appraisals +23 -0
- data/CHANGELOG.md +4 -0
- data/CONTRIBUTING.md +43 -0
- data/Gemfile +3 -0
- data/LICENSE.md +23 -0
- data/README.md +146 -0
- data/Rakefile +15 -0
- data/app/controllers/square_event/webhook_controller.rb +47 -0
- data/config/routes.rb +3 -0
- data/gemfiles/rails5.1.gemfile +7 -0
- data/gemfiles/rails5.2.gemfile +7 -0
- data/gemfiles/rails6.0.gemfile +7 -0
- data/gemfiles/rails6.1.gemfile +7 -0
- data/gemfiles/rails_master.gemfile +7 -0
- data/lib/square_event.rb +65 -0
- data/lib/square_event/engine.rb +5 -0
- data/lib/square_event/errors.rb +16 -0
- data/lib/square_event/event.rb +34 -0
- data/lib/square_event/version.rb +3 -0
- data/lib/square_event/webhook.rb +36 -0
- data/spec/controllers/webhook_controller_spec.rb +130 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +65 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +40 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +4 -0
- data/spec/dummy/config/initializers/secret_token.rb +16 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/lib/stripe_event_spec.rb +154 -0
- data/spec/rails_helper.rb +3 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/fixtures/evt_payment_created.json +67 -0
- data/square_event.gemspec +28 -0
- metadata +242 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ecf0821b8d28696caefd11c54078e6b888e97f95a307936a6e6c66039af135ac
|
|
4
|
+
data.tar.gz: ff07a0237ec567e9a691c23946b78384d3d441ecfc06c61f8bf1d24f959d5282
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66e0ddd01436b3546cb7f3f45b2489156ed89728228c2f25df2a72d5100c432b687a425644fee368bf2d6e3cdb98199d52320a269bfec967e2211d956571afa1
|
|
7
|
+
data.tar.gz: ab0c50a053176902c6271b2e84f4be2e9b3a3c424225ad63571df2cf17d96df28f4729b48437f05e7b3977d4b0087912372ef4c2f9efc55cb219733b23717558
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
language: ruby
|
|
2
|
+
cache: bundler
|
|
3
|
+
|
|
4
|
+
rvm:
|
|
5
|
+
- 2.5
|
|
6
|
+
- 2.6
|
|
7
|
+
- 2.7
|
|
8
|
+
- 3.0
|
|
9
|
+
|
|
10
|
+
gemfile:
|
|
11
|
+
- gemfiles/rails5.1.gemfile
|
|
12
|
+
- gemfiles/rails5.2.gemfile
|
|
13
|
+
- gemfiles/rails6.0.gemfile
|
|
14
|
+
- gemfiles/rails6.1.gemfile
|
|
15
|
+
- gemfiles/rails_master.gemfile
|
|
16
|
+
|
|
17
|
+
matrix:
|
|
18
|
+
include:
|
|
19
|
+
- rvm: 2.3
|
|
20
|
+
gemfile: gemfiles/rails3.2.gemfile
|
|
21
|
+
- rvm: 2.3
|
|
22
|
+
gemfile: gemfiles/rails4.2.gemfile
|
|
23
|
+
- rvm: 2.4
|
|
24
|
+
gemfile: gemfiles/rails4.2.gemfile
|
|
25
|
+
- rvm: 2.7
|
|
26
|
+
gemfile: gemfiles/rails_master.gemfile
|
|
27
|
+
- rvm: 3.0
|
|
28
|
+
gemfile: gemfiles/rails_master.gemfile
|
|
29
|
+
allow_failures:
|
|
30
|
+
- gemfile: gemfiles/rails_master.gemfile
|
|
31
|
+
fast_finish: true
|
|
32
|
+
|
|
33
|
+
notifications:
|
|
34
|
+
email:
|
|
35
|
+
- andy@andycallaghan.com
|
data/Appraisals
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# appraise "rails4.2" do
|
|
2
|
+
# gem "rails", "~> 4.2.0"
|
|
3
|
+
# end
|
|
4
|
+
|
|
5
|
+
appraise "rails5.1" do
|
|
6
|
+
gem "rails", "~> 5.1.0"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
appraise "rails5.2" do
|
|
10
|
+
gem "rails", "~> 5.2.0"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
appraise "rails6.0" do
|
|
14
|
+
gem "rails", "~> 6.0.0"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
appraise "rails6.1" do
|
|
18
|
+
gem "rails", "~> 6.1.0"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
appraise "rails_master" do
|
|
22
|
+
gem "rails", github: "rails"
|
|
23
|
+
end
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
## Want to Contribute?
|
|
2
|
+
|
|
3
|
+
Awesome. We love help, but before getting started, please read:
|
|
4
|
+
|
|
5
|
+
**[Don't "Push" Your Pull Requests](http://www.igvita.com/2011/12/19/dont-push-your-pull-requests/)**
|
|
6
|
+
|
|
7
|
+
## Ready for a Pull-Request?
|
|
8
|
+
|
|
9
|
+
1. Fork the repo.
|
|
10
|
+
|
|
11
|
+
2. Run the tests. We only take pull requests with passing tests, and it's great
|
|
12
|
+
to know that you have a clean slate: `bundle && appraisal && bundle exec rake`
|
|
13
|
+
|
|
14
|
+
3. Add a test for your change. Only refactoring and documentation changes
|
|
15
|
+
require no new tests. If you are adding functionality or fixing a bug, we need
|
|
16
|
+
a test!
|
|
17
|
+
|
|
18
|
+
4. Make the test pass.
|
|
19
|
+
|
|
20
|
+
5. Push to your fork and submit a pull request.
|
|
21
|
+
|
|
22
|
+
At this point you're waiting on us. We like to at least comment on, if not
|
|
23
|
+
accept, pull requests within three business days (and, typically, one business
|
|
24
|
+
day). We may suggest some changes or improvements or alternatives.
|
|
25
|
+
|
|
26
|
+
Some things that will increase the chance that your pull request is accepted,
|
|
27
|
+
taken straight from the Ruby on Rails guide:
|
|
28
|
+
|
|
29
|
+
## Conventions
|
|
30
|
+
|
|
31
|
+
* Use Rails idioms and helpers.
|
|
32
|
+
* Include tests that fail without your code, and pass with your code.
|
|
33
|
+
* Update the documentation, the surrounding one, examples elsewhere, guides,
|
|
34
|
+
whatever is affected by your contribution
|
|
35
|
+
|
|
36
|
+
Syntax:
|
|
37
|
+
|
|
38
|
+
* Two spaces, no tabs.
|
|
39
|
+
* No trailing whitespace. Blank lines should not have any space.
|
|
40
|
+
* Prefer `&&`/`||` over `and`/`or`.
|
|
41
|
+
* `MyClass.my_method(my_arg)` not `my_method( my_arg )` or `my_method my_arg`.
|
|
42
|
+
* `a = b` not `a=b`.
|
|
43
|
+
* Follow the conventions you see used in the source already.
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2020-2021 Andy Callaghan - Stripe modifications
|
|
4
|
+
Copyright 2012-2020 Integrallis Software - original work
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
a copy of this software and associated documentation files (the
|
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# SquareEvent
|
|
2
|
+
|
|
3
|
+
<!-- [](http://travis-ci.org/integrallis/stripe_event)
|
|
4
|
+
[](http://badge.fury.io/rb/stripe_event)
|
|
5
|
+
[](https://codeclimate.com/github/integrallis/stripe_event)
|
|
6
|
+
[](https://coveralls.io/r/integrallis/stripe_event)
|
|
7
|
+
[](https://rubygems.org/gems/stripe_event) -->
|
|
8
|
+
|
|
9
|
+
SquareEvent is built on the [ActiveSupport::Notifications API](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html). Incoming webhook requests are [authenticated with the webhook signature](#authenticating-webhooks-with-signatures). Define subscribers to handle specific event types. Subscribers can be a block or an object that responds to `#call`.
|
|
10
|
+
|
|
11
|
+
The gem is based on the excellent [Stripe Event](https://github.com/integrallis/stripe_event) work from Integrallis Software, it has been adapted and re-written to work with Square webhooks.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
# Gemfile
|
|
17
|
+
gem 'square_event'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
# config/routes.rb
|
|
22
|
+
mount SquareEvent::Engine, at: '/my-chosen-path' # provide a custom path
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# config/initializers/stripe.rb
|
|
29
|
+
SquareEvent.signing_secret = Rails.application.credentials.square[Rails.env][:webhook_secret]
|
|
30
|
+
SquareEvent.notification_url = Rails.application.credentials.square[Rails.env][:webhook_url]
|
|
31
|
+
|
|
32
|
+
SquareEvent.configure do |events|
|
|
33
|
+
events.subscribe 'payment.created' do |event|
|
|
34
|
+
# Define subscriber behavior based on the event object
|
|
35
|
+
event.class #=> SquareEvent::Event
|
|
36
|
+
event.type #=> "payment.created"
|
|
37
|
+
event.data #=> data": { "type": "payment", "id": "KkAkhdMs...
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
events.all do |event|
|
|
41
|
+
# Handle all event types - logging, etc.
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Subscriber or interactor objects that respond to #call
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
class CustomerCreated
|
|
50
|
+
def call(event)
|
|
51
|
+
# Event handling
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class BillingEventLogger
|
|
56
|
+
def initialize(logger)
|
|
57
|
+
@logger = logger
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def call(event)
|
|
61
|
+
@logger.info "BILLING:#{event.type}:#{event.id}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
SquareEvent.configure do |events|
|
|
68
|
+
events.all BillingEventLogger.new(Rails.logger)
|
|
69
|
+
events.subscribe 'customer.created', CustomerCreated.new
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Subscribing to a namespace of event types
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
SquareEvent.subscribe 'customer.' do |event|
|
|
77
|
+
# Will be triggered for any customer.* events
|
|
78
|
+
end
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Securing your webhook endpoint
|
|
82
|
+
|
|
83
|
+
### Authenticating webhooks with signatures
|
|
84
|
+
|
|
85
|
+
Square will cryptographically sign webhook payloads with a signature that is included in a special header sent with the request. Verifying this signature lets your application properly authenticate the request originated from Square. SquareEvent mandates that this is used for every request. Please set the `signing_secret` and `notification_url` configuration values:
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
SquareEvent.signing_secret = Rails.application.credentials.square[Rails.env][:webhook_secret]
|
|
89
|
+
SquareEvent.notification_url = Rails.application.credentials.square[Rails.env][:notification_url]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Please refer to Square's documentation for more details: https://developer.squareup.com/docs/webhooks-api/validate-notifications
|
|
93
|
+
|
|
94
|
+
### Sandbox and live mode
|
|
95
|
+
|
|
96
|
+
If you'd like to ignore particular webhook events (perhaps to ignore test webhooks in production, you can do so by returning `nil` in your custom `event_filter`. For example:
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
SquareEvent.event_filter = lambda do |event|
|
|
100
|
+
return nil if Rails.env.production? && !event.sandbox?
|
|
101
|
+
event
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Without Rails
|
|
106
|
+
|
|
107
|
+
SquareEvent can be used outside of Rails applications as well. Here is a basic Sinatra implementation:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
require 'json'
|
|
111
|
+
require 'sinatra'
|
|
112
|
+
require 'stripe_event'
|
|
113
|
+
|
|
114
|
+
SquareEvent.signing_secret = ENV['SQUARE_SIGNING_SECRET']
|
|
115
|
+
SquareEvent.notification_url = ENV['SQUARE_NOTIFICATION_URL']
|
|
116
|
+
|
|
117
|
+
SquareEvent.subscribe 'payment.created' do |event|
|
|
118
|
+
# Look ma, no Rails!
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
post '/_billing_events' do
|
|
122
|
+
data = JSON.parse(request.body.read, symbolize_names: true)
|
|
123
|
+
SquareEvent.instrument(data)
|
|
124
|
+
200
|
|
125
|
+
end
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Testing
|
|
129
|
+
|
|
130
|
+
Handling webhooks is a critical piece of modern billing systems. Verifying the behavior of SquareEvent subscribers can be done fairly easily by stubbing out the HTTP signature header used to authenticate the webhook request. Tools like [Webmock](https://github.com/bblimke/webmock) and [VCR](https://github.com/vcr/vcr) work well. [RequestBin](https://requestbin.com/) is great for collecting the payloads. For exploratory phases of development, [UltraHook](http://www.ultrahook.com/) and other tools can forward webhook requests directly to localhost.
|
|
131
|
+
|
|
132
|
+
The Square ruby library does not currently offer an `Event` object to use to create or refer to webhook with, so their testing in Ruby is harder than with Stripe.
|
|
133
|
+
|
|
134
|
+
### Maintainers
|
|
135
|
+
|
|
136
|
+
* [Andy Callaghan](https://github.com/acallaghan)
|
|
137
|
+
|
|
138
|
+
Special thanks to all the [contributors](https://github.com/jammed-org/square_event/graphs/contributors).
|
|
139
|
+
|
|
140
|
+
### Versioning
|
|
141
|
+
|
|
142
|
+
Semantic Versioning 2.0 as defined at <http://semver.org>.
|
|
143
|
+
|
|
144
|
+
### License
|
|
145
|
+
|
|
146
|
+
[MIT License](https://github.com/jammed-org/square_event/blob/master/LICENSE.md). Copyright 2020-2021 Andy Callaghan, Square work. Copyright 2012-2015 Integrallis Software original Stripe work.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler/setup'
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
if ENV['CI']
|
|
9
|
+
task default: :spec
|
|
10
|
+
else
|
|
11
|
+
require 'appraisal'
|
|
12
|
+
task :default do
|
|
13
|
+
system('bundle exec rake appraisal spec')
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'square_event/webhook'
|
|
2
|
+
require 'square_event/errors'
|
|
3
|
+
|
|
4
|
+
module SquareEvent
|
|
5
|
+
class WebhookController < ActionController::Base
|
|
6
|
+
SECRET_ERROR = 'There was no webhook signing secret provided for this webhook'.freeze
|
|
7
|
+
NOTIFICATION_ERROR = 'There was no webhook notification URL provided for this webhook. Make sure it exactly matches and starts with https://'.freeze
|
|
8
|
+
|
|
9
|
+
if Rails.application.config.action_controller.default_protect_from_forgery
|
|
10
|
+
skip_before_action :verify_authenticity_token
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def event
|
|
14
|
+
SquareEvent.instrument(verified_event)
|
|
15
|
+
head :ok
|
|
16
|
+
rescue SquareEvent::SignatureVerificationError => e
|
|
17
|
+
log_error(e)
|
|
18
|
+
head :bad_request
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def verified_event
|
|
24
|
+
payload = request.raw_post
|
|
25
|
+
signature = request.headers['X-Square-Signature']
|
|
26
|
+
environment = request.headers['square-environment']
|
|
27
|
+
timestamp = request.headers['square-initial-delivery-timestamp']
|
|
28
|
+
secret = SquareEvent.signing_secret
|
|
29
|
+
notification_url = SquareEvent.notification_url
|
|
30
|
+
|
|
31
|
+
if secret.nil?
|
|
32
|
+
raise SignatureVerificationError.new(SECRET_ERROR)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
if notification_url.nil?
|
|
36
|
+
raise SignatureVerificationError.new(NOTIFICATION_ERROR)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
SquareEvent::Webhook.construct_event(payload, signature, secret, notification_url, environment, timestamp)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def log_error(e)
|
|
43
|
+
logger.error e.message
|
|
44
|
+
e.backtrace.each { |line| logger.error " #{line}" }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/config/routes.rb
ADDED
data/lib/square_event.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require "active_support/notifications"
|
|
2
|
+
require "square_event/engine" if defined?(Rails)
|
|
3
|
+
require "square_event/errors"
|
|
4
|
+
require "square_event/event"
|
|
5
|
+
|
|
6
|
+
module SquareEvent
|
|
7
|
+
class << self
|
|
8
|
+
attr_accessor :adapter, :backend, :namespace, :event_filter, :notification_url, :signing_secret
|
|
9
|
+
|
|
10
|
+
def configure(&block)
|
|
11
|
+
raise ArgumentError, "must provide a block" unless block_given?
|
|
12
|
+
block.arity.zero? ? instance_eval(&block) : yield(self)
|
|
13
|
+
end
|
|
14
|
+
alias :setup :configure
|
|
15
|
+
|
|
16
|
+
def instrument(event)
|
|
17
|
+
event = event_filter.call(event)
|
|
18
|
+
backend.instrument namespace.call(event.type), event if event
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def subscribe(name, callable = nil, &block)
|
|
22
|
+
callable ||= block
|
|
23
|
+
backend.subscribe namespace.to_regexp(name), adapter.call(callable)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def all(callable = nil, &block)
|
|
27
|
+
callable ||= block
|
|
28
|
+
subscribe nil, callable
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def listening?(name)
|
|
32
|
+
namespaced_name = namespace.call(name)
|
|
33
|
+
backend.notifier.listening?(namespaced_name)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Namespace < Struct.new(:value, :delimiter)
|
|
38
|
+
def call(name = nil)
|
|
39
|
+
"#{value}#{delimiter}#{name}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def to_regexp(name = nil)
|
|
43
|
+
%r{^#{Regexp.escape call(name)}}
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class NotificationAdapter < Struct.new(:subscriber)
|
|
48
|
+
def self.call(callable)
|
|
49
|
+
new(callable)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def call(*args)
|
|
53
|
+
payload = args.last
|
|
54
|
+
subscriber.call(payload)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
class Error < StandardError; end
|
|
59
|
+
class UnauthorizedError < Error; end
|
|
60
|
+
|
|
61
|
+
self.adapter = NotificationAdapter
|
|
62
|
+
self.backend = ActiveSupport::Notifications
|
|
63
|
+
self.namespace = Namespace.new("square_event", ".")
|
|
64
|
+
self.event_filter = lambda { |event| event }
|
|
65
|
+
end
|