Permalink
Browse files

Add an extra event attribute to the JSON sent to Ducksboard.

Now that Ducksboard service hook supports many events, a simpler
mechanism to detect which kind of event has been received on the
service's side is needed. Adding an attribute to the JSON body
identifying the event type is convenient.
  • Loading branch information...
1 parent 78a73e2 commit 5434245a6a993fae7b8aee4ec0294cd0fc3e478b @aitorciki aitorciki committed Feb 16, 2012
Showing with 24 additions and 19 deletions.
  1. +5 −6 services/ducksboard.rb
  2. +19 −13 test/ducksboard_test.rb
View
@@ -7,14 +7,13 @@ class Service::Ducksboard < Service
DUCKS_MAX_KEYS = 5
def receive_push
- # As simple as it can get: just build a ducksboard webhooks url from
- # the webhook_key config param and send the whole payload, ducksboard
- # will know what to do with it.
- # Why not using a POST-receive url then? Because we are interested in
- # more events than just pushes!
+ # Build Ducksboard webhook urls from the webhook_key config param,
+ # then send a JSON containing the event type and payload to such
+ # url.
http.headers['content-type'] = 'application/x-www-form-urlencoded'
- body = http.params.merge(:payload => JSON.generate(payload))
+ body = http.params.merge(
+ :content => JSON.generate(:event => event, :payload => payload))
parse_webhook_key(data).each do |key|
url = "https://webhooks.ducksboard.com/#{key}"
View
@@ -3,22 +3,28 @@ def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end
- def test_receive
+ def receive_helper(event)
# Our service is pretty simple, and so is the test: just check that
- # the original payload is received on our side, where the parsing
- # will happen.
- svc = service({
- 'webhook_key' => '1234abcd'
- }, payload)
+ # the original payload and event are received on our side,
+ # where the parsing will happen.
+ svc = service(event, {'webhook_key' => '1234abcd'}, payload)
@stubs.post '/1234abcd' do |env|
body = Rack::Utils.parse_nested_query(env[:body])
- recv = JSON.parse(body['payload'])
- assert_equal payload, recv
+ recv = JSON.parse(body['content'])
+ assert_equal recv['payload'], payload
+ assert_equal recv['event'], event.to_s
[200, {}, '']
end
- svc.receive
+ event_method = "receive_#{event}"
+ svc.send(event_method)
+ end
+
+ def test_receive
+ [:push, :issues, :fork, :watch].each do |event|
+ receive_helper event
+ end
end
def test_webhook_key_through_url
@@ -28,8 +34,8 @@ def test_webhook_key_through_url
@stubs.post '/abcd1234' do |env|
body = Rack::Utils.parse_nested_query(env[:body])
- recv = JSON.parse(body['payload'])
- assert_equal payload, recv
+ recv = JSON.parse(body['content'])
+ assert_equal recv['payload'], payload
[200, {}, '']
end
@@ -48,8 +54,8 @@ def test_many_webhook_keys
@stubs.post "/#{endpoint}" do |env|
posted << endpoint
body = Rack::Utils.parse_nested_query(env[:body])
- recv = JSON.parse(body['payload'])
- assert_equal payload, recv
+ recv = JSON.parse(body['content'])
+ assert_equal recv['payload'], payload
[200, {}, '']
end
end

0 comments on commit 5434245

Please sign in to comment.