Created
November 17, 2014 16:31
-
-
Save jdmorlan/6071261b656bd9289e76 to your computer and use it in GitHub Desktop.
A sample notification server example for jdmorlan.github.io blog post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'json' | |
require 'httparty' | |
subscriptions = [] | |
get '/subscriptions' do | |
subscriptions.to_json | |
end | |
post '/subscribe' do | |
json_body = JSON.parse(request.body.read) | |
subscriptions << json_body['url'] | |
data = {"event_payload" => "test-data", "event_type" => "test-event"}.to_json | |
trigger_event(subscriptions, data) | |
end | |
def trigger_event(subscriptions, data) | |
options = { | |
:body => data, | |
:headers => {'Content-Type' => 'application/json'} | |
} | |
subscriptions.each do |url| | |
HTTParty.post(url, options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment