Class: ShopifyAPI::Webhooks::Registrations::Http

Inherits:
ShopifyAPI::Webhooks::Registration show all
Extended by:
T::Sig
Defined in:
lib/shopify_api/webhooks/registrations/http.rb

Constant Summary

Constants inherited from ShopifyAPI::Webhooks::Registration

ShopifyAPI::Webhooks::Registration::FIELDS_DELIMITER

Instance Attribute Summary

Attributes inherited from ShopifyAPI::Webhooks::Registration

#fields, #handler, #topic

Instance Method Summary collapse

Methods inherited from ShopifyAPI::Webhooks::Registration

#build_register_query, #initialize

Constructor Details

This class inherits a constructor from ShopifyAPI::Webhooks::Registration

Instance Method Details

#build_check_queryObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shopify_api/webhooks/registrations/http.rb', line 32

def build_check_query
  <<~QUERY
    {
      webhookSubscriptions(first: 1, topics: #{@topic}) {
        edges {
          node {
            id
            endpoint {
              __typename
              ... on WebhookHttpEndpoint {
                callbackUrl
              }
            }
          }
        }
      }
    }
  QUERY
end

#callback_addressObject



11
12
13
14
15
16
17
18
19
# File 'lib/shopify_api/webhooks/registrations/http.rb', line 11

def callback_address
  if @path.match?(%r{^https?://})
    @path
  elsif @path.match?(/^#{Context.host_name}/)
    "#{Context.host_scheme}://#{@path}"
  else
    "#{Context.host}/#{@path}"
  end
end

#mutation_name(webhook_id) ⇒ Object



27
28
29
# File 'lib/shopify_api/webhooks/registrations/http.rb', line 27

def mutation_name(webhook_id)
  webhook_id ? "webhookSubscriptionUpdate" : "webhookSubscriptionCreate"
end

#parse_check_result(body) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shopify_api/webhooks/registrations/http.rb', line 53

def parse_check_result(body)
  edges = body.dig("data", "webhookSubscriptions", "edges") || {}
  webhook_id = nil
  current_address = nil
  unless edges.empty?
    node = edges[0]["node"]
    webhook_id = node["id"].to_s
    current_address =
      if node.key?("endpoint")
        node["endpoint"]["callbackUrl"].to_s
      else
        node["callbackUrl"].to_s
      end
  end
  { webhook_id: webhook_id, current_address: current_address }
end

#subscription_argsObject



22
23
24
# File 'lib/shopify_api/webhooks/registrations/http.rb', line 22

def subscription_args
  { callbackUrl: callback_address, includeFields: fields }.compact
end