Sha256: 231f298b1b6ca75471b66465b19ad4a7021d7f3e6d3b3e932e128a8bdd9fbb70

Contents?: true

Size: 1.98 KB

Versions: 17

Compression:

Stored size: 1.98 KB

Contents

# typed: strict
# frozen_string_literal: true

module ShopifyAPI
  module Webhooks
    module Registrations
      class PubSub < Registration
        extend T::Sig

        sig { override.returns(String) }
        def callback_address
          @path
        end

        sig { override.returns(T::Hash[Symbol, String]) }
        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 }.compact
        end

        sig { override.params(webhook_id: T.nilable(String)).returns(String) }
        def mutation_name(webhook_id)
          webhook_id ? "pubSubWebhookSubscriptionUpdate" : "pubSubWebhookSubscriptionCreate"
        end

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

        sig { override.params(body: T::Hash[String, T.untyped]).returns(T::Hash[Symbol, String]) }
        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 = "pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
          end
          { webhook_id: webhook_id, current_address: current_address }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shopify_api-13.1.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-13.0.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.5.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.4.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.3.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.2.1 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.2.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.1.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-12.0.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-11.1.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-11.0.1 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-11.0.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-10.1.0 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-10.0.3 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-10.0.2 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-10.0.1 lib/shopify_api/webhooks/registrations/pub_sub.rb
shopify_api-10.0.0 lib/shopify_api/webhooks/registrations/pub_sub.rb