Class: ShopifyAPI::Webhooks::Registrations::PubSub

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

Constant Summary

Constants inherited from ShopifyAPI::Webhooks::Registration

ShopifyAPI::Webhooks::Registration::FIELDS_DELIMITER

Instance Attribute Summary

Attributes inherited from ShopifyAPI::Webhooks::Registration

#fields, #filter, #handler, #metafield_namespaces, #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



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

def build_check_query
  <<~QUERY
    {
      webhookSubscriptions(first: 1, topics: #{@topic}) {
        edges {
          node {
            id
            includeFields
            metafieldNamespaces
            filter
            endpoint {
              __typename
              ... on WebhookPubSubEndpoint {
                pubSubProject
                pubSubTopic
              }
            }
          }
        }
      }
    }
  QUERY
end

#callback_addressObject



11
12
13
# File 'lib/shopify_api/webhooks/registrations/pub_sub.rb', line 11

def callback_address
  @path
end

#mutation_name(webhook_id) ⇒ Object



25
26
27
# File 'lib/shopify_api/webhooks/registrations/pub_sub.rb', line 25

def mutation_name(webhook_id)
  webhook_id ? "pubSubWebhookSubscriptionUpdate" : "pubSubWebhookSubscriptionCreate"
end

#parse_check_result(body) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shopify_api/webhooks/registrations/pub_sub.rb', line 63

def parse_check_result(body)
  edges = body.dig("data", "webhookSubscriptions", "edges") || {}
  webhook_id = nil
  fields = []
  metafield_namespaces = []
  filter = nil
  current_address = nil
  unless edges.empty?
    node = edges[0]["node"]
    webhook_id = node["id"].to_s
    current_address =
      "pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
    fields = node["includeFields"] || []
    metafield_namespaces = node["metafieldNamespaces"] || []
    filter = node["filter"].to_s
  end
  { webhook_id: webhook_id, current_address: current_address, fields: fields,
    metafield_namespaces: metafield_namespaces, filter: filter, }
end

#subscription_argsObject



16
17
18
19
20
21
22
# File 'lib/shopify_api/webhooks/registrations/pub_sub.rb', line 16

def subscription_args
  project_topic_pair = callback_address.gsub(%r{^pubsub://}, "").split(":")
  project = project_topic_pair[0]
  topic = project_topic_pair[1]
  { pubSubProject: project, pubSubTopic: topic, includeFields: fields,
    metafieldNamespaces: metafield_namespaces, filter: filter, }.compact
end