webhook_handler 0.3.1 → 0.4.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
  SHA1:
3
- metadata.gz: ac325d09df2af3cb4a38aac89d747a2d9044eeb7
4
- data.tar.gz: abfc52014ac55628ab27ade5318b9ee1efa79ea2
3
+ metadata.gz: acc1eb247b5f94b23259728fc1eee27ecfaf2f86
4
+ data.tar.gz: 82da8ac47464e6651a407dc106c7d13eb688a452
5
5
  SHA512:
6
- metadata.gz: 0149f03dcbdcce0428b46e77fe2e96dc9b5016ccc22754e167dd44dcac352c11a3c22ed8209ac0b76b962a37d98a1281b2bccee66141ec772278e9a2337fcbfc
7
- data.tar.gz: 13195bb1a675c25d2e199e9f05fac3546f3f16454f08967883de0171bf990c6041e159a2089a6463ba2d8c13e93e4ac3b58aec411f0f2fc2e3e0205fc282ba4a
6
+ metadata.gz: 25b9fb6b1dd76fc50fd1565bffbe3940de858a7c84716a2459869d2b93fb62f81712226b04f512558040b7c0b51e85207e9fba5dd42c82e7d72f9dd8758a7c35
7
+ data.tar.gz: 0dd20c18c026a181139ae072db4a69c2f6051020e66521a8b0af8cd4d574803d49400b8a8ef9debd330022f6d17bda670987ff52b77517cdcdabe1fecc36bfe3
@@ -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 [![Build Status](https://travis-ci.org/chrismytton/webhook_handler.svg?branch=master)](https://travis-ci.org/chrismytton/webhook_handler)
2
2
 
3
- Combines Sinatra and Sidekiq into a neat package that makes creating simple apps that respond to webhooks a pleasure.
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 Sinatra app.
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
- ### Sinatra compatibility
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 perform
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
- get '/status' do
60
- "I'm a regular Sinatra route!"
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
  ```
@@ -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 perform
10
- puts "Working hard!"
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
@@ -1,25 +1,40 @@
1
1
  require 'webhook_handler/version'
2
2
  require 'sidekiq'
3
- require 'sinatra'
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 self
10
+ klass.extend ClassMethods
8
11
  klass.send(:include, Sidekiq::Worker)
9
- klass.extend Sinatra::Delegator
10
- klass.instance_eval do
11
- get '/' do
12
- 'Send a POST request to this URL to trigger the webhook'
13
- end
12
+ end
13
+
14
+ module ClassMethods
15
+ def call(env)
16
+ new.call(env)
17
+ end
18
+ end
14
19
 
15
- post '/' do
16
- klass.perform_async
17
- 'ok'
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 call(*args)
23
- Sinatra::Application.call(*args)
37
+ def _handle_webhook
38
+ response.write(self.class.perform_async)
24
39
  end
25
40
  end
@@ -5,7 +5,7 @@ require 'webhook_handler/version'
5
5
  module WebhookHandler
6
6
  class CLI < Thor
7
7
  include Thor::Actions
8
- desc "new NAME", "create a new app called NAME"
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(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase }
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}'..."
@@ -1,3 +1,3 @@
1
1
  module WebhookHandler
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -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 'sinatra', ['>= 1', '< 2']
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.3.1
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: sinatra
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