Sha256: 6d53cdffd46c1b472e8c111d3573ab7eb48acbda603d409f732be2009d927d2b
Contents?: true
Size: 1.37 KB
Versions: 5
Compression:
Stored size: 1.37 KB
Contents
require 'rack/auth/basic' require 'base64' require 'json' require 'wisper' module Routemaster class Receiver include Wisper::Publisher def initialize(app, options = {}) warn 'Routemaster::Receiver is deprecated, use Routemaster::Drain::Basic instead' warn "(at #{caller(2,1).first})" @app = app @path = options[:path] @uuid = options[:uuid] @handler = options[:handler] if options[:handler] end def call(env) catch :forward do throw :forward unless _intercept_endpoint?(env) return [401, {}, []] unless _has_auth?(env) return [403, {}, []] unless _valid_auth?(env) payload = _extract_payload(env) return [400, {}, []] unless payload @handler.on_events(payload) if @handler publish(:events_received, payload) return [204, {}, []] end @app.call(env) end private def _intercept_endpoint?(env) env['PATH_INFO'] == @path && env['REQUEST_METHOD'] == 'POST' end def _has_auth?(env) env.has_key?('HTTP_AUTHORIZATION') end def _valid_auth?(env) Base64. decode64(env['HTTP_AUTHORIZATION'].gsub(/^Basic /, '')). split(':').first == @uuid end def _extract_payload(env) return unless env['CONTENT_TYPE'] == 'application/json' JSON.parse(env['rack.input'].read) end end end
Version data entries
5 entries across 5 versions & 1 rubygems