Skip to content

Instantly share code, notes, and snippets.

@jdmorlan
Created November 17, 2014 16:31
Show Gist options
  • Save jdmorlan/6071261b656bd9289e76 to your computer and use it in GitHub Desktop.
Save jdmorlan/6071261b656bd9289e76 to your computer and use it in GitHub Desktop.
A sample notification server example for jdmorlan.github.io blog post.
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