Class: Sockudo::WebHook
- Inherits:
-
Object
- Object
- Sockudo::WebHook
- Defined in:
- lib/sockudo/webhook.rb
Overview
Used to parse and authenticate WebHooks
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
Instance Method Summary collapse
-
#data ⇒ Object
Access the parsed WebHook body.
-
#events ⇒ Object
Array of events (as Hashes) contained inside the webhook.
-
#initialize(request, client = Sockudo) ⇒ WebHook
constructor
Provide either a Rack::Request or a Hash containing :key, :signature, :body, and :content_type (optional).
-
#time ⇒ Time
The time at which the WebHook was initially triggered by Sockudo, i.e.
-
#valid?(extra_tokens = nil) ⇒ Boolean
Returns whether the WebHook is valid by checking that the signature matches the configured key & secret.
Constructor Details
#initialize(request, client = Sockudo) ⇒ WebHook
Provide either a Rack::Request or a Hash containing :key, :signature, :body, and :content_type (optional)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sockudo/webhook.rb', line 33 def initialize(request, client = Sockudo) @client = client # For Rack::Request and ActionDispatch::Request if request.respond_to?(:env) && request.respond_to?(:content_type) @key = request.env['HTTP_X_PUSHER_KEY'] @signature = request.env['HTTP_X_PUSHER_SIGNATURE'] @content_type = request.content_type request.body.rewind @body = request.body.read request.body.rewind else @key, @signature, @body = request.values_at(:key, :signature, :body) @content_type = request[:content_type] || 'application/json' end end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
28 29 30 |
# File 'lib/sockudo/webhook.rb', line 28 def key @key end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
28 29 30 |
# File 'lib/sockudo/webhook.rb', line 28 def signature @signature end |
Instance Method Details
#data ⇒ Object
Access the parsed WebHook body
88 89 90 91 92 93 94 95 |
# File 'lib/sockudo/webhook.rb', line 88 def data @data ||= case @content_type when 'application/json' MultiJson.decode(@body) else raise "Unknown Content-Type (#{@content_type})" end end |
#events ⇒ Object
Array of events (as Hashes) contained inside the webhook
73 74 75 |
# File 'lib/sockudo/webhook.rb', line 73 def events data['events'] end |
#time ⇒ Time
The time at which the WebHook was initially triggered by Sockudo, i.e. when the event occurred
82 83 84 |
# File 'lib/sockudo/webhook.rb', line 82 def time Time.at(data['time_ms'].to_f / 1000) end |
#valid?(extra_tokens = nil) ⇒ Boolean
Returns whether the WebHook is valid by checking that the signature matches the configured key & secret. In the case that the webhook is invalid, the reason is logged
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sockudo/webhook.rb', line 57 def valid?(extra_tokens = nil) extra_tokens = [extra_tokens] if extra_tokens.is_a?(Hash) if @key == @client.key return signature_valid?(@client.secret) elsif extra_tokens extra_tokens.each do |token| return signature_valid?(token[:secret]) if @key == token[:key] end end Sockudo.logger.warn "Received webhook with unknown key: #{key}" false end |