webhook_handler 0.3.1 → 0.4.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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +11 -9
- data/example/app.rb +12 -2
- data/lib/webhook_handler.rb +27 -12
- data/lib/webhook_handler/cli.rb +2 -2
- data/lib/webhook_handler/version.rb +1 -1
- data/webhook_handler.gemspec +2 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acc1eb247b5f94b23259728fc1eee27ecfaf2f86
|
4
|
+
data.tar.gz: 82da8ac47464e6651a407dc106c7d13eb688a452
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25b9fb6b1dd76fc50fd1565bffbe3940de858a7c84716a2459869d2b93fb62f81712226b04f512558040b7c0b51e85207e9fba5dd42c82e7d72f9dd8758a7c35
|
7
|
+
data.tar.gz: 0dd20c18c026a181139ae072db4a69c2f6051020e66521a8b0af8cd4d574803d49400b8a8ef9debd330022f6d17bda670987ff52b77517cdcdabe1fecc36bfe3
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,12 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
## [0.4.0] - 2015-11-26
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
|
10
|
+
- Removed the dependency on Sinatra, it now operates as a simple Rack app.
|
11
|
+
|
6
12
|
## [0.3.1] - 2015-11-26
|
7
13
|
|
8
14
|
### Fixed
|
@@ -28,3 +34,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
28
34
|
[0.2.0]: https://github.com/chrismytton/webhook_handler/compare/v0.1.0...v0.2.0
|
29
35
|
[0.3.0]: https://github.com/chrismytton/webhook_handler/compare/v0.2.0...v0.3.0
|
30
36
|
[0.3.1]: https://github.com/chrismytton/webhook_handler/compare/v0.3.0...v0.3.1
|
37
|
+
[0.3.1]: https://github.com/chrismytton/webhook_handler/compare/v0.3.1...v0.4.0
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# WebhookHandler [](https://travis-ci.org/chrismytton/webhook_handler)
|
2
2
|
|
3
|
-
Combines
|
3
|
+
Combines Rack and Sidekiq into a neat package that makes creating simple apps that respond to webhooks a pleasure.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
Define a class and include the `WebhookHandler` module. This class then acts as a Sidekiq worker and a
|
23
|
+
Define a class and include the `WebhookHandler` module. This class then acts as a Sidekiq worker and a Rack app.
|
24
24
|
|
25
25
|
**app.rb**
|
26
26
|
|
@@ -44,20 +44,22 @@ require_relative './app'
|
|
44
44
|
run MyApp
|
45
45
|
```
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
You can also declare other routes in the class:
|
47
|
+
If you want to pass some arguments from the request into the background job you can define a `handle_webhook` method.
|
50
48
|
|
51
49
|
```ruby
|
52
50
|
class MyApp
|
53
51
|
include WebhookHandler
|
54
52
|
|
55
|
-
def
|
56
|
-
|
53
|
+
def handle_webhook
|
54
|
+
request.body.rewind
|
55
|
+
payload = JSON.parse(request.body.read)
|
56
|
+
self.class.perform_async(payload['message'])
|
57
57
|
end
|
58
58
|
|
59
|
-
|
60
|
-
"
|
59
|
+
def perform(message)
|
60
|
+
puts "Got a message: #{message}"
|
61
|
+
# Do some long running task...
|
62
|
+
sleep 2
|
61
63
|
end
|
62
64
|
end
|
63
65
|
```
|
data/example/app.rb
CHANGED
@@ -2,12 +2,22 @@ lib = File.expand_path('../../lib', __FILE__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
4
|
require 'webhook_handler'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# Trigger this with:
|
8
|
+
# curl -X POST http://localhost:5000 -d '{"message": "bar"}'
|
5
9
|
|
6
10
|
class MyApp
|
7
11
|
include WebhookHandler
|
8
12
|
|
9
|
-
def
|
10
|
-
|
13
|
+
def handle_webhook
|
14
|
+
request.body.rewind
|
15
|
+
payload = JSON.parse(request.body.read)
|
16
|
+
self.class.perform_async(payload['message'])
|
17
|
+
end
|
18
|
+
|
19
|
+
def perform(message)
|
20
|
+
puts "Working hard! Message: #{message}"
|
11
21
|
sleep 5
|
12
22
|
end
|
13
23
|
end
|
data/lib/webhook_handler.rb
CHANGED
@@ -1,25 +1,40 @@
|
|
1
1
|
require 'webhook_handler/version'
|
2
2
|
require 'sidekiq'
|
3
|
-
require '
|
3
|
+
require 'rack'
|
4
4
|
|
5
5
|
module WebhookHandler
|
6
|
+
attr_reader :request
|
7
|
+
attr_reader :response
|
8
|
+
|
6
9
|
def self.included(klass)
|
7
|
-
klass.extend
|
10
|
+
klass.extend ClassMethods
|
8
11
|
klass.send(:include, Sidekiq::Worker)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def call(env)
|
16
|
+
new.call(env)
|
17
|
+
end
|
18
|
+
end
|
14
19
|
|
15
|
-
|
16
|
-
|
17
|
-
|
20
|
+
def call(env)
|
21
|
+
@request = Rack::Request.new(env)
|
22
|
+
@response = Rack::Response.new
|
23
|
+
|
24
|
+
if request.get?
|
25
|
+
response.write('Send a POST request to this URL to trigger the webhook')
|
26
|
+
elsif request.post?
|
27
|
+
if respond_to?(:handle_webhook)
|
28
|
+
send(:handle_webhook)
|
29
|
+
else
|
30
|
+
_handle_webhook
|
18
31
|
end
|
19
32
|
end
|
33
|
+
|
34
|
+
response.finish
|
20
35
|
end
|
21
36
|
|
22
|
-
def
|
23
|
-
|
37
|
+
def _handle_webhook
|
38
|
+
response.write(self.class.perform_async)
|
24
39
|
end
|
25
40
|
end
|
data/lib/webhook_handler/cli.rb
CHANGED
@@ -5,7 +5,7 @@ require 'webhook_handler/version'
|
|
5
5
|
module WebhookHandler
|
6
6
|
class CLI < Thor
|
7
7
|
include Thor::Actions
|
8
|
-
desc
|
8
|
+
desc 'new NAME', 'create a new app called NAME'
|
9
9
|
|
10
10
|
def self.source_root
|
11
11
|
File.dirname(__FILE__)
|
@@ -13,7 +13,7 @@ module WebhookHandler
|
|
13
13
|
|
14
14
|
def new(name)
|
15
15
|
# @see http://git.io/vBqrp
|
16
|
-
@constant_name = name.tr('-', '_').gsub(/-[_-]*(?![_-]|$)/) {
|
16
|
+
@constant_name = name.tr('-', '_').gsub(/-[_-]*(?![_-]|$)/) { '::' }.gsub(/([_-]+|(::)|^)(.|$)/) { Regexp.last_match(2).to_s + Regexp.last_match(3).upcase }
|
17
17
|
|
18
18
|
FileUtils.mkdir_p(name)
|
19
19
|
puts "Creating app '#{name}'..."
|
data/webhook_handler.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
22
|
spec.add_dependency 'sidekiq', ['>= 3', '< 5']
|
23
|
-
spec.add_dependency '
|
23
|
+
spec.add_dependency 'rack', ['>= 1', '< 2']
|
24
24
|
spec.add_dependency 'thor'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'rack-test'
|
31
31
|
spec.add_development_dependency 'foreman'
|
32
32
|
spec.add_development_dependency 'puma'
|
33
|
+
spec.add_development_dependency 'simplecov'
|
33
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webhook_handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Mytton
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '5'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: rack
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
@@ -162,6 +162,20 @@ dependencies:
|
|
162
162
|
- - ">="
|
163
163
|
- !ruby/object:Gem::Version
|
164
164
|
version: '0'
|
165
|
+
- !ruby/object:Gem::Dependency
|
166
|
+
name: simplecov
|
167
|
+
requirement: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
type: :development
|
173
|
+
prerelease: false
|
174
|
+
version_requirements: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
165
179
|
description:
|
166
180
|
email:
|
167
181
|
- chrismytton@gmail.com
|