Class: Rack::GithubWebhooks

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/github_webhooks.rb,
lib/rack/github_webhooks/version.rb

Defined Under Namespace

Classes: Signature

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ GithubWebhooks

Returns a new instance of GithubWebhooks.



24
25
26
27
# File 'lib/rack/github_webhooks.rb', line 24

def initialize(app, opts = {})
  @app = app
  @secret = opts[:secret]
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rack/github_webhooks.rb', line 29

def call(env)
  env['rack.input'].rewind
  signature = Signature.new(
    @secret,
    env['HTTP_X_HUB_SIGNATURE'],
    env['rack.input'].read
  )
  return [400, {}, ["Signatures didn't match!"]] unless signature.valid?

  begin
    env['rack.input'].rewind if env['rack.input'].respond_to?(:rewind)
  rescue Errno::ESPIPE
  end

  @app.call(env)
end