-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks.rb
176 lines (145 loc) · 5.39 KB
/
webhooks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
$:.<< File.dirname(__FILE__)
#curl -XPOST -d "name=xyz" -d "[email protected]" -d "public=true" http://localhost:9292/v1/channels
#curl -XPOST -d "name=xyz" -d "webhook_url=http://localhost:9292/webhook" http://localhost:9292/v1/channels/1/webhooks
#curl -XPOST -d "event_name=request.created" -d "data='{name:[email protected]}'" http://localhost:9292/v1/channels/1/events
require File.join(File.dirname(__FILE__), 'config', 'boot')
module Webhooks
class Application < Sinatra::Base
use Rack::Throttle::Daily, :max => 1000 # requests
use Rack::Throttle::Hourly, :max => 100 # requests
#use Rack::Throttle::Interval, :min => 1 # seconds
mime_type :json, "application/json"
configure { set :public_folder, File.join(File.dirname(__FILE__), 'public') }
before { content_type :json }
helpers do
def throw_unauthenticated_response
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, json_response(:error => "unauthorized")])
end
def authenticate_publisher!
throw_unauthenticated_response unless authenticated_publisher?
end
def authenticate_publisher_or_subscriber!
throw_unauthenticated_response unless authenticated_publisher?(public_check=false) ||
authenticated_subscriber?
end
def authenticated_publisher?(public_check=true)
@channel = Channel.get_by_guid_or_id(params[:id])
return false unless @channel
return true if public_check && @channel.public
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? && @auth.basic? && @auth.credentials
@channel.authorized?(@auth.username)
else
false
end
end
def authenticated_subscriber?
@webhook = Webhook.get_by_channel_id_and_webhook_id(params[:id], params[:webhook_id])
return false unless @webhook
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? && @auth.basic? && @auth.credentials
@webhook.authorized?(@auth.username)
else
false
end
end
end
not_found { throw :halt, [404, json_response({:error => 'not_found'})] }
def json_response(object)
if object.is_a?(Array)
object.map { |element| element.attrs }.to_json
else
object.to_json
end
end
def valid_record(record, options={})
options = {:status => 200, :response => record}.merge(options)
status(options[:status])
json_response(options[:response])
end
def invalid_record(record)
throw :halt, [422, json_response(:errors => record.errors.full_messages)]
end
def manage_resource(resource, options={})
raise Sinatra::NotFound unless resource
yield(resource) if block_given?
valid_record(resource, options)
rescue ActiveRecord::RecordInvalid => e
invalid_record(e.record)
end
get '/' do
content_type :html
erb :about
end
post '/v1/channels' do
manage_resource Channel.new(params.slice(*Channel.accessible_keys)),
:status => 201 do |channel|
channel.save!
end
end
get '/v1/channels/:id' do
authenticate_publisher!
manage_resource @channel
end
put '/v1/channels/:id' do
authenticate_publisher!
manage_resource @channel do |channel|
attributes = params.slice(*Channel.accessible_keys).reject {|k,v| k == "guid"}
channel.update_attributes!(attributes)
end
end
delete '/v1/channels/:id' do
authenticate_publisher!
manage_resource @channel, :response => {"response" => "ok"} do |channel|
channel.mark_as_deleted
end
end
post '/v1/channels/:id/webhooks' do
authenticate_publisher!
manage_resource @channel.webhooks.build(params.slice(*Webhook.accessible_keys)),
:status => 201 do |webhook|
webhook.save!
end
end
get '/v1/channels/:id/webhooks/:webhook_id' do
authenticate_publisher_or_subscriber!
manage_resource Webhook.by_channel_id_and_webhook_id(
params['id'], params['webhook_id'])
end
put '/v1/channels/:id/webhooks/:webhook_id' do
authenticate_publisher_or_subscriber!
manage_resource Webhook.by_channel_id_and_webhook_id(
params['id'], params['webhook_id']) do |webhook|
webhook.update_attributes!(params.slice(*Webhook.accessible_keys))
end
end
delete '/v1/channels/:id/webhooks/:webhook_id' do
authenticate_publisher_or_subscriber!
manage_resource Webhook.by_channel_id_and_webhook_id(
params['id'], params['webhook_id']),
:response => {"response" => "ok"} do |webhook|
webhook.mark_as_deleted
end
end
get '/v1/channels/:id/webhooks' do
authenticate_publisher!
manage_resource @channel.webhooks
end
post '/v1/channels/:id/events' do
authenticate_publisher!
manage_resource @channel.events.build(params.slice(*Event.accessible_keys)),
:status => 201 do |event|
event.save!
end
end
get '/v1/channels/:id/events/:event_id' do
authenticate_publisher!
manage_resource @channel.events.find_by_id(params[:event_id])
end
get '/v1/channels/:id/events/:event_id/log' do
authenticate_publisher!
manage_resource @channel.events.find_by_id(params[:event_id]).try(:notifications)
end
end
end