lib/activehook/app/middleware.rb in activehook-0.1.3 vs lib/activehook/app/middleware.rb in activehook-0.1.4

- old
+ new

@@ -1,27 +1,19 @@ module ActiveHook module App class Middleware - class << self - attr_accessor :valid, :invalid, :not_created, :created - end - - @invalid = ->(_env) { [400, { "Content-Type" => "application/json" }, [{ valid: false }.to_json]] } - @valid = ->(_env) { [200, { "Content-Type" => "application/json" }, [{ valid: true }.to_json]] } - @not_created = ->(_env) { [400, { "Content-Type" => "application/json" }, [{ status: false }.to_json]] } - @created = ->(_env) { [200, { "Content-Type" => "application/json" }, [{ status: true }.to_json]] } - def initialize(app) @app = app end def call(env) @env = env @req = Rack::Request.new(env) - if validation_request? then valid? - elsif creation_request? then create? + if validation_request? then response(Validation) + #Not enabling webhook creation yet. + #elsif creation_request? then response(Creation) else @app.call(@env) end end def validation_request? @@ -30,38 +22,30 @@ def creation_request? @req.path == ActiveHook.config.creation_path && @req.post? end - def valid? - if Validation.new(@req.params).start - self.class.valid.call(@env) - else - self.class.invalid.call(@env) - end + def response(klass) + response = + if klass.new(@req).start then { code: 200, status: true } + else { code: 400, status: false } + end + [response[:code], { "Content-Type" => "application/json" }, [{ status: response[:status] }.to_json]] end - - def create? - if Creation.new(@req.params).start - self.class.created.call(@env) - else - self.class.not_created.call(@env) - end - end end - Validation = Struct.new(:params) do + Validation = Struct.new(:req) do def start - hook = { id: params['id'].to_i, key: params['key'] } + hook = { id: req.params['id'].to_i, key: req.params['key'] } ActiveHook::Validate.new(hook).perform rescue false end end - Creation = Struct.new(:params) do + Creation = Struct.new(:req) do def start - hook = { uri: params['uri'], payload: JSON.parse(params['payload']) } + hook = JSON.parse(req.body.read) ActiveHook::Hook.new(hook).perform rescue false end end