Skip to content

Instantly share code, notes, and snippets.

@f-mer
Last active December 7, 2018 22:53
Show Gist options
  • Save f-mer/ab12cbd21603b3d29b5a5e84e34c1a99 to your computer and use it in GitHub Desktop.
Save f-mer/ab12cbd21603b3d29b5a5e84e34c1a99 to your computer and use it in GitHub Desktop.
Cloud Events
require 'sinatra'
set :protection, :except => :frame_options
set :bind, '0.0.0.0'
module CloudEvents
class Event
attr_accessor :data, :version, :id, :time, :type, :source, :schemaurl
HTTP_HEADERS = {
'HTTP_CE_CLOUDEVENTVERSION' => :version,
'HTTP_CE_EVENTID' => :id,
'HTTP_CE_EVENTTIME' => :time,
'HTTP_CE_EVENTTYPE' => :type,
'HTTP_CE_SOURCE' => :source,
'HTTP_CE_SCHEMAURL' => :schemaurl,
}
def initialize(data:, version:, id:, time:, type:, source:, schemaurl:)
@data = data
@version = version
@id = id
@time = time
@type = type
@source = source
@schemaurl = schemaurl
end
def self.from_req(req)
new(
data: req.body.read,
version: req.fetch_header('HTTP_CE_CLOUDEVENTVERSION'),
id: req.fetch_header('HTTP_CE_EVENTID'),
time: Time.parse(req.fetch_header('HTTP_CE_EVENTTIME')),
type: req.fetch_header('HTTP_CE_EVENTTYPE'),
source: req.fetch_header('HTTP_CE_SOURCE'),
schemaurl: req.fetch_header('HTTP_CE_SCHEMAURL'),
)
end
end
end
get '/' do
req = Rack::Request.new(env)
event = CloudEvents::Event.from_req(req)
puts event.inspect
"Hello!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment