Class: Clearbit::Webhook

Inherits:
Mash
  • Object
show all
Defined in:
lib/clearbit/webhook.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mash

#custom_reader, #custom_writer, #deep_merge, #deep_update, #delete, #dup, #fetch, #id, #initializing_reader, #key?, #method_missing, new, #regular_dup, #replace, #respond_to?, #shallow_merge, #shallow_update, #type, #underbang_reader

Constructor Details

#initialize(env) ⇒ Webhook



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/clearbit/webhook.rb', line 22

def initialize(env)
  request = Rack::Request.new(env)

  request.body.rewind

  signature = request.env['HTTP_X_REQUEST_SIGNATURE']
  body      = request.body.read

  self.class.valid!(signature, body)

  merge!(JSON.parse(body))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Clearbit::Mash

Class Method Details

.clearbit_keyObject



7
8
9
# File 'lib/clearbit/webhook.rb', line 7

def self.clearbit_key
  Clearbit.key!
end

.valid!(signature, body) ⇒ Object



18
19
20
# File 'lib/clearbit/webhook.rb', line 18

def self.valid!(signature, body)
  valid?(signature, body) ? true : raise(Errors::InvalidWebhookSignature.new)
end

.valid?(request_signature, body) ⇒ Boolean



11
12
13
14
15
16
# File 'lib/clearbit/webhook.rb', line 11

def self.valid?(request_signature, body)
  return false unless request_signature && body

  signature = 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), clearbit_key, body)
  Rack::Utils.secure_compare(request_signature, signature)
end