shopify_webhook 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f058937197537958a1fc7224da3c205604702a81
4
+ data.tar.gz: 7695ae69d359173194c2e942d201e6bb0a5781cd
5
+ SHA512:
6
+ metadata.gz: 3dc092f3a9933c5a46af2ee1f793be26a063baeee74394c1004a7dafa961b057947830f3690b3c01ed7de80c39940e08c9049c158fffba50eb65f72b4e7cb67f
7
+ data.tar.gz: 818ae4010e7d273a62f3e2e8358121f5053a1aac7974aa766cca31513b398fb873eea5e2ffc823dc8632134296f3547323a4e310556a2819fb6da9ca886c7e3b
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Inspire9
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Shopify Webhook
2
+
3
+ A Rack endpoint for handling Shopify webhooks, and fires an ActiveSupport notification for each succesful request.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shopify_webhook', '0.1.0'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Mount an instance of `ShopifyWebhook::Endpoint` to your preferred route. In a Rails app, that'd look something like this:
16
+
17
+ ```ruby
18
+ post '/shopify/webhook', to: ShopifyWebhook::Endpoint.new(SHARED_SECRET)
19
+ ```
20
+
21
+ Then, handle the notifications using something like the following (which would probably go in an initialiser for a Rails app):
22
+
23
+ ```ruby
24
+ ActiveSupport::Notifications.subscribe(
25
+ 'notification.shopify.webhook'
26
+ ) do |*args|
27
+ event = ActiveSupport::Notifications::Event.new *args
28
+ # use event.payload[:json] however you like.
29
+ end
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/inspire9/shopify_webhook/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
39
+
40
+ ## Licence
41
+
42
+ Copyright (c) 2014, Shopify Webhook is developed and maintained by [Inspire9](http://development.inspire9.com), and is released under the open MIT Licence.
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -0,0 +1,12 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'active_support/notifications'
4
+ require 'active_support/core_ext/module/delegation'
5
+ require 'multi_json'
6
+
7
+ module ShopifyWebhook
8
+ #
9
+ end
10
+
11
+ require 'shopify_webhook/endpoint'
12
+ require 'shopify_webhook/verifier'
@@ -0,0 +1,27 @@
1
+ class ShopifyWebhook::Endpoint
2
+ delegate :instrument, to: ActiveSupport::Notifications
3
+
4
+ def initialize(secret)
5
+ @secret = secret
6
+ end
7
+
8
+ def call(env)
9
+ request = Rack::Request.new env
10
+
11
+ if ShopifyWebhook::Verifier.new(request, secret).call
12
+ instrument 'notification.shopify.webhook', json: json(request)
13
+ [200, {}, ['']]
14
+ else
15
+ [400, {}, ['']]
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :secret
22
+
23
+ def json(request)
24
+ request.body.rewind
25
+ MultiJson.load request.body.read
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ class ShopifyWebhook::Verifier
2
+ def initialize(request, secret)
3
+ @request, @secret = request, secret
4
+ end
5
+
6
+ def call
7
+ hmac == header
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :request, :secret
13
+
14
+ def data
15
+ request.body.rewind
16
+ request.body.read
17
+ end
18
+
19
+ def header
20
+ request.env['HTTP_X_SHOPIFY_HMAC_SHA256'] ||
21
+ request.env['X-Shopify-Hmac-SHA256']
22
+ end
23
+
24
+ def hmac
25
+ Base64.encode64(
26
+ OpenSSL::HMAC.digest(digest, secret, data)
27
+ ).strip
28
+ end
29
+
30
+ def digest
31
+ OpenSSL::Digest.new 'sha256'
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'shopify_webhook'
4
+ spec.version = '0.1.0'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = %q{A Rack endpoint for handling Shopify webhooks.}
8
+ spec.description = %q{A Rack endpoint for handling Shopify webhooks, and fires an ActiveSupport notification for each succesful request.}
9
+ spec.homepage = 'http://github.com/inspire9/shopify_webhook'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_runtime_dependency 'activesupport', '>= 3.1.0'
18
+ spec.add_runtime_dependency 'multi_json', '>= 1.3.0'
19
+ spec.add_runtime_dependency 'rack'
20
+
21
+ spec.add_development_dependency 'rack-test', '~> 0.6.2'
22
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
23
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Shopify Notifications' do
4
+ include Rack::Test::Methods
5
+
6
+ let(:app) { ShopifyWebhook::Endpoint.new 'SUPERSECRET' }
7
+ let(:subscriptions) { [] }
8
+
9
+ def subscribe(&block)
10
+ subscriptions << ActiveSupport::Notifications.subscribe(
11
+ 'notification.shopify.webhook', &block
12
+ )
13
+ end
14
+
15
+ def hmac_for(body)
16
+ Base64.encode64(
17
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), 'SUPERSECRET', body)
18
+ ).strip
19
+ end
20
+
21
+ def post_with_hmac(path, params, headers = {})
22
+ post path, params, headers.merge(
23
+ 'HTTP_X_SHOPIFY_HMAC_SHA256' => hmac_for(params)
24
+ )
25
+ end
26
+
27
+ after :each do
28
+ subscriptions.each do |subscription|
29
+ ActiveSupport::Notifications.unsubscribe(subscription)
30
+ end
31
+ end
32
+
33
+ it 'returns a 200' do
34
+ post_with_hmac '/', '[]'
35
+
36
+ expect(last_response.status).to eq(200)
37
+ end
38
+
39
+ it 'fires an event' do
40
+ notification = false
41
+ subscribe { |*args| notification = true }
42
+
43
+ post_with_hmac '/', '[]'
44
+
45
+ expect(notification).to eq(true)
46
+ end
47
+
48
+ it 'includes the JSON body' do
49
+ subscribe { |*args|
50
+ event = ActiveSupport::Notifications::Event.new *args
51
+ expect(event.payload[:json]).to eq([{'foo' => 'bar'}])
52
+ }
53
+
54
+ post_with_hmac '/', '[{"foo":"bar"}]'
55
+ end
56
+
57
+ it 'accepts a dashed HMAC header' do
58
+ post '/', '[]', {'X-Shopify-Hmac-SHA256' => hmac_for('[]')}
59
+
60
+ expect(last_response.status).to eq(200)
61
+ end
62
+
63
+ context 'with invalid HMAC' do
64
+ it 'returns a 400' do
65
+ post '/', '[]'
66
+
67
+ expect(last_response.status).to eq(400)
68
+ end
69
+
70
+ it 'does not fire an event' do
71
+ notification = false
72
+ subscribe { |*args| notification = true }
73
+
74
+ post '/', '[]'
75
+
76
+ expect(notification).to eq(false)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopify_webhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ description: A Rack endpoint for handling Shopify webhooks, and fires an ActiveSupport
84
+ notification for each succesful request.
85
+ email:
86
+ - pat@freelancing-gods.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/shopify_webhook.rb
97
+ - lib/shopify_webhook/endpoint.rb
98
+ - lib/shopify_webhook/verifier.rb
99
+ - shopify_webhook.gemspec
100
+ - spec/acceptance/notification_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/inspire9/shopify_webhook
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Rack endpoint for handling Shopify webhooks.
126
+ test_files:
127
+ - spec/acceptance/notification_spec.rb
128
+ - spec/spec_helper.rb