-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathrequest.rb
More file actions
73 lines (60 loc) · 2 KB
/
request.rb
File metadata and controls
73 lines (60 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# typed: strict
# frozen_string_literal: true
module ShopifyAPI
module Webhooks
class Request
extend T::Sig
include Utils::VerifiableQuery
sig { override.returns(String) }
def hmac
Digest.hexencode(Base64.decode64(T.cast(shopify_header("hmac-sha256"), String)))
end
sig { returns(String) }
def topic
T.cast(shopify_header("topic"), String)
end
sig { returns(String) }
def shop
T.cast(shopify_header("shop-domain"), String)
end
sig { returns(String) }
def api_version
T.cast(shopify_header("api-version"), String)
end
sig { returns(String) }
def webhook_id
T.cast(shopify_header("webhook-id"), String)
end
sig { override.returns(String) }
def to_signable_string
@raw_body
end
sig { returns(T::Hash[String, T.untyped]) }
def parsed_body
JSON.parse(@raw_body)
end
sig { params(raw_body: String, headers: T::Hash[String, T.untyped]).void }
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 = []
["topic", "hmac-sha256", "shop-domain"].each do |name|
unless headers.key?("shopify-#{name}") || headers.key?("x-shopify-#{name}")
missing_headers << "shopify-#{name} or x-shopify-#{name}"
end
end
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
private
sig { params(name: String).returns(T.untyped) }
def shopify_header(name)
@headers["shopify-#{name}"] || @headers["x-shopify-#{name}"]
end
end
end
end