Class: EasyPost::Webhook
- Inherits:
-
Resource
- Object
- EasyPostObject
- Resource
- EasyPost::Webhook
- Defined in:
- lib/easypost/webhook.rb
Overview
Each Webhook contains the url which EasyPost will notify whenever an object in our system updates. Several types of objects are processed asynchronously in the EasyPost system, so whenever an object updates, an Event is sent via HTTP POST to each configured webhook URL.
Instance Attribute Summary
Attributes inherited from EasyPostObject
#api_key, #name, #parent, #unsaved_values
Class Method Summary collapse
-
.validate_webhook(event_body, headers, webhook_secret) ⇒ Object
Validate a webhook by comparing the HMAC signature header sent from EasyPost to your shared secret.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete a Webhook.
-
#update(params = {}) ⇒ Object
Update a Webhook.
Methods inherited from Resource
all, class_name, create, each, #refresh, retrieve, #save, url, #url
Methods inherited from EasyPostObject
#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values
Constructor Details
This class inherits a constructor from EasyPost::EasyPostObject
Class Method Details
.validate_webhook(event_body, headers, webhook_secret) ⇒ Object
Validate a webhook by comparing the HMAC signature header sent from EasyPost to your shared secret. If the signatures do not match, an error will be raised signifying the webhook either did not originate from EasyPost or the secrets do not match. If the signatures do match, the ‘event_body` will be returned as JSON.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/easypost/webhook.rb', line 40 def self.validate_webhook(event_body, headers, webhook_secret) easypost_hmac_signature = headers['X-Hmac-Signature'] if easypost_hmac_signature.nil? raise EasyPost::Error.new('Webhook received does not contain an HMAC signature.') end encoded_webhook_secret = webhook_secret.unicode_normalize(:nfkd).encode('utf-8') expected_signature = OpenSSL::HMAC.hexdigest('sha256', encoded_webhook_secret, event_body) digest = "hmac-sha256-hex=#{expected_signature}" unless digest == easypost_hmac_signature raise EasyPost::Error.new('Webhook received did not originate from EasyPost or had a webhook secret mismatch.') end JSON.parse(event_body) end |
Instance Method Details
#delete ⇒ Object
Delete a Webhook.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/easypost/webhook.rb', line 22 def delete # NOTE: This method is redefined here since the "url" method conflicts with the objects field unless id raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}") end instance_url = "#{self.class.url}/#{CGI.escape(id)}" response = EasyPost.make_request(:delete, instance_url, @api_key) refresh_from(response, api_key) self end |
#update(params = {}) ⇒ Object
Update a Webhook.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/easypost/webhook.rb', line 7 def update(params = {}) # NOTE: This method is redefined here since the "url" method conflicts with the objects field unless id raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}") end instance_url = "#{self.class.url}/#{CGI.escape(id)}" response = EasyPost.make_request(:patch, instance_url, @api_key, params) refresh_from(response, api_key) self end |