Class: ShopifyAPI::Webhooks::Request
- Inherits:
-
Object
- Object
- ShopifyAPI::Webhooks::Request
- Extended by:
- T::Sig
- Includes:
- Utils::VerifiableQuery
- Defined in:
- lib/shopify_api/webhooks/request.rb
Instance Method Summary collapse
- #api_version ⇒ Object
- #hmac ⇒ Object
-
#initialize(raw_body:, headers:) ⇒ Request
constructor
A new instance of Request.
- #parsed_body ⇒ Object
- #shop ⇒ Object
- #to_signable_string ⇒ Object
- #topic ⇒ Object
- #webhook_id ⇒ Object
Constructor Details
#initialize(raw_body:, headers:) ⇒ Request
Returns a new instance of Request.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/shopify_api/webhooks/request.rb', line 46 def initialize(raw_body:, headers:) # normalize the headers by forcing lowercase, removing any prepended "http"s, and changing underscores to dashes headers = headers.to_h { |k, v| [k.to_s.downcase.sub("http_", "").gsub("_", "-"), v] } missing_headers = [] [ "x-shopify-topic", "x-shopify-hmac-sha256", "x-shopify-shop-domain", ].each { |header| missing_headers << header unless headers.include?(header) } unless missing_headers.empty? raise Errors::InvalidWebhookError, "Missing one or more of the required HTTP headers to process webhooks: #{missing_headers}" end @headers = headers @raw_body = raw_body end |
Instance Method Details
#api_version ⇒ Object
26 27 28 |
# File 'lib/shopify_api/webhooks/request.rb', line 26 def api_version T.cast(@headers["x-shopify-api-version"], String) end |
#hmac ⇒ Object
11 12 13 |
# File 'lib/shopify_api/webhooks/request.rb', line 11 def hmac Digest.hexencode(Base64.decode64(T.cast(@headers["x-shopify-hmac-sha256"], String))) end |
#parsed_body ⇒ Object
41 42 43 |
# File 'lib/shopify_api/webhooks/request.rb', line 41 def parsed_body JSON.parse(@raw_body) end |
#shop ⇒ Object
21 22 23 |
# File 'lib/shopify_api/webhooks/request.rb', line 21 def shop T.cast(@headers["x-shopify-shop-domain"], String) end |
#to_signable_string ⇒ Object
36 37 38 |
# File 'lib/shopify_api/webhooks/request.rb', line 36 def to_signable_string @raw_body end |
#topic ⇒ Object
16 17 18 |
# File 'lib/shopify_api/webhooks/request.rb', line 16 def topic T.cast(@headers["x-shopify-topic"], String) end |
#webhook_id ⇒ Object
31 32 33 |
# File 'lib/shopify_api/webhooks/request.rb', line 31 def webhook_id T.cast(@headers["x-shopify-webhook-id"], String) end |