metova-webhooks 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: 1e0762f8f8c8cfea0ee2b3421538e14e8cad9ee3
4
+ data.tar.gz: 6105ee50db418aec117bdc5757ee29115d228c33
5
+ SHA512:
6
+ metadata.gz: 2add541a95c4f5417a1b059d440fb0fcf501380fbc87260dec28990168d13bae65cd36d7ce1ba682ebde6237579ea95f18a2145ac6bb2a7064fff0739943f9d5
7
+ data.tar.gz: c4ce06cece8ac1c5a2077eda8e482ea8a99a1a728aa5331277a3d1f3274d2e0ac9bd908d5572a3a9f56dc7b28c1b9468a0a84d555b95dd4f4dec4bc5fbbb08b8
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jami Couch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # Metova::Webhooks
2
+
3
+ Easily add webhooks to your Rails app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'metova-webhooks'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install metova-webhooks
20
+
21
+ Then install the migrations:
22
+
23
+ $ rake metova-webhooks:install:migrations
24
+
25
+ And migrate:
26
+
27
+ $ rake db:migrate
28
+
29
+ ### Notes
30
+
31
+ Webhooks requires a 'User' model and ActiveJob, so make sure you have those things.
32
+
33
+ ## Usage
34
+
35
+ In your webhookable model, include `Metova::Webhookable`:
36
+
37
+ ```ruby
38
+ class MyModel
39
+ include Metova::Webhookable
40
+
41
+ # the following overrides are optional and can be used to customize your events
42
+
43
+ # override namespace to provide a namespace for your events (ex: a parent class and id)
44
+ def namespace
45
+ ''
46
+ end
47
+
48
+ # override hook_name to change the base event name
49
+ def hook_name
50
+ self.class.to_s.underscore
51
+ end
52
+
53
+ # override webhook_event to change how event names are constructed
54
+ def webhook_event(event)
55
+ [namespace, hook_name, event].reject(&:empty?).join(':')
56
+ end
57
+
58
+ # override webhook_serialize to change how the model is serialized for sending
59
+ def webhook_serialize
60
+ self.to_json
61
+ end
62
+
63
+ # ...
64
+ end
65
+ ```
66
+
67
+ Include `Metova::WebhooksBase` in a controller:
68
+
69
+ ```ruby
70
+ class MyWebhooksController < ActionController::Base
71
+ include Metova::WebhooksBase
72
+
73
+ # override create to change the create behavior
74
+ def create
75
+ # create takes a block, and the webhook will not save if there are any errors, so you can do custom validations
76
+ super do |webhook|
77
+ webhook.errors.add :user, "is not allowed to make webhooks" if some_condition
78
+ end
79
+ end
80
+
81
+ protected
82
+ # your controller needs to respond to current_user (for example, if you are using devise)
83
+ def current_user
84
+ nil
85
+ end
86
+ end
87
+ ```
88
+
89
+ Add routes to your `config/routes.rb`:
90
+
91
+ ```ruby
92
+ resources :webhooks, only: [:index, :create, :destroy]
93
+ ```
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/metova/metova-webhooks.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ module Metova
2
+ class WebhookJob < ActiveJob::Base
3
+ queue_as :default
4
+
5
+ def perform(event, payload)
6
+ Webhook.where(event: event).each do |webhook|
7
+ begin
8
+ url = URI.parse(webhook.url)
9
+ Net::HTTP.post_form url, JSON.parse(payload)
10
+ rescue Exception => e
11
+ Rails.logger.error e.message
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ module Metova
2
+ class Webhook < ActiveRecord::Base
3
+ belongs_to :user, required: true
4
+
5
+ validates_presence_of :event
6
+ validates_presence_of :url
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ class CreateMetovaWebhooks < ActiveRecord::Migration
2
+ def change
3
+ create_table :metova_webhooks do |t|
4
+ t.references :user, index: true
5
+ t.string :event
6
+ t.string :url
7
+
8
+ t.timestamps null: false
9
+ end
10
+ add_index :metova_webhooks, :event
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'metova/webhooks/engine'
2
+
3
+ require 'metova/webhooks/version'
4
+
5
+ require 'metova/webhooks/controllers/webhooks_base'
6
+ require 'metova/webhooks/models/webhookable'
7
+
8
+ module Metova
9
+ end
@@ -0,0 +1,27 @@
1
+ module Metova::WebhooksBase
2
+ extend ActiveSupport::Concern
3
+
4
+ def index
5
+ respond_with Metova::Webhook.where(user: current_user)
6
+ end
7
+
8
+ def create
9
+ @webhook = Metova::Webhook.new permitted_params.merge(user: current_user)
10
+ if block_given?
11
+ yield @webhook
12
+ end
13
+ @webhook.save if @webhook.errors.empty?
14
+ respond_with @webhook, location: nil
15
+ end
16
+
17
+ def destroy
18
+ @webhook = Metova::Webhook.where(user: current_user).find(params[:id])
19
+ @webhook.destroy
20
+ respond_with @webhook
21
+ end
22
+
23
+ protected
24
+ def permitted_params
25
+ params.require(:webhook).permit(:event, :url)
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ require 'responders'
2
+
3
+ module Metova::Webhooks
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Metova
6
+
7
+ # run engine migrations when running bin/rake db:migrate from the main app
8
+ initializer :append_migrations do |app|
9
+ unless app.root.to_s.match root.to_s
10
+ config.paths['db/migrate'].expanded.each do |expanded_path|
11
+ app.config.paths['db/migrate'] << expanded_path
12
+ end
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ module Metova::Webhookable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ after_create :create_hook
6
+ after_update :update_hook
7
+ after_destroy :destroy_hook
8
+ end
9
+
10
+ def namespace
11
+ ''
12
+ end
13
+
14
+ def hook_name
15
+ self.class.to_s.underscore
16
+ end
17
+
18
+ def webhook_event(event)
19
+ [namespace, hook_name, event].reject(&:empty?).join(':')
20
+ end
21
+
22
+ def create_hook
23
+ Metova::WebhookJob.perform_later webhook_event('create'), self.webhook_serialize
24
+ end
25
+
26
+ def update_hook
27
+ Metova::WebhookJob.perform_later webhook_event('update'), self.webhook_serialize
28
+ end
29
+
30
+ def destroy_hook
31
+ Metova::WebhookJob.perform_later webhook_event('destroy'), self.webhook_serialize
32
+ end
33
+
34
+ def webhook_serialize
35
+ self.to_json
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Metova
2
+ module Webhooks
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metova-webhooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jami Couch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: responders
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: capybara
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: combustion
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.5.3
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.5.3
139
+ - !ruby/object:Gem::Dependency
140
+ name: shoulda
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: shoulda-matchers
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 2.8.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 2.8.0
167
+ description: Webhook library for Rails
168
+ email:
169
+ - jami.couch@metova.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - LICENSE.txt
175
+ - README.md
176
+ - Rakefile
177
+ - app/job/metova/webhook_job.rb
178
+ - app/models/metova/webhook.rb
179
+ - db/migrate/20150812204248_create_metova_webhooks.rb
180
+ - lib/metova/webhooks.rb
181
+ - lib/metova/webhooks/controllers/webhooks_base.rb
182
+ - lib/metova/webhooks/engine.rb
183
+ - lib/metova/webhooks/models/webhookable.rb
184
+ - lib/metova/webhooks/version.rb
185
+ homepage: http://github.com/metova/metova-webhooks
186
+ licenses:
187
+ - MIT
188
+ metadata: {}
189
+ post_install_message:
190
+ rdoc_options: []
191
+ require_paths:
192
+ - lib
193
+ required_ruby_version: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: 2.0.0
198
+ required_rubygems_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ requirements: []
204
+ rubyforge_project:
205
+ rubygems_version: 2.4.5
206
+ signing_key:
207
+ specification_version: 4
208
+ summary: Webhook library for Rails
209
+ test_files: []