Module: Webhookdb::Replicator::ShopifyV1Mixin

Included in:
ShopifyCustomerV1, ShopifyOrderV1
Defined in:
lib/webhookdb/replicator/shopify_v1_mixin.rb

Instance Method Summary collapse

Instance Method Details

#_fetch_backfill_page(pagination_token, **_kwargs) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 140

def _fetch_backfill_page(pagination_token, **_kwargs)
  url = if pagination_token.blank?
          self.service_integration.api_url + self._mixin_backfill_url
  else
    pagination_token
  end
  response = Webhookdb::Http.get(
    url,
    basic_auth: {username: self.service_integration.backfill_key,
                 password: self.service_integration.backfill_secret,},
    logger: self.logger,
    timeout: Webhookdb::Shopify.http_timeout,
  )
  data = response.parsed_response
  next_link = nil
  if response.headers.key?("link")
    links = Webhookdb::Shopify.parse_link_header(response.headers["link"])
    next_link = links[:next] if links.key?(:next)
  end
  return data[self._mixin_backfill_hashkey], next_link
end

#_mixin_backfill_hashkeyObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 10

def _mixin_backfill_hashkey
  raise NotImplementedError
end

#_mixin_backfill_urlObject

Raises:

  • (NotImplementedError)


6
7
8
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 6

def _mixin_backfill_url
  raise NotImplementedError
end

#_mixin_backfill_warningObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 14

def _mixin_backfill_warning
  raise NotImplementedError
end

#_resource_and_event(request) ⇒ Object

For Shopify endpoints the object and webhook have the same shape—the webhook is simply the updated object



23
24
25
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 23

def _resource_and_event(request)
  return request.body, nil
end

#_timestamp_column_nameObject



18
19
20
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 18

def _timestamp_column_name
  return :updated_at
end

#_verify_backfill_401_err_msgObject



131
132
133
134
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 131

def _verify_backfill_401_err_msg
  return "It looks like that API Key/Access Token combination is invalid. " \
         "Please reenter the API Key you just created:"
end

#_verify_backfill_403_err_msgObject



123
124
125
126
127
128
129
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 123

def _verify_backfill_403_err_msg
  return "It looks like that API Key does not have permission to access #{self.resource_name_singular} Records. " \
         "Please check the permissions by going to your private app page and " \
         "looking at the list of active permissions. " \
         "Once you've verified or corrected the permissions for this key, " \
         "please reenter the API Key you just created:"
end

#_verify_backfill_err_msgObject



136
137
138
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 136

def _verify_backfill_err_msg
  return "An error occurred. Please reenter the API Key you just created:"
end

#_webhook_response(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 27

def _webhook_response(request)
  # info for debugging
  shopify_auth = request.env["HTTP_X_SHOPIFY_HMAC_SHA256"]
  log_params = {shopify_auth:, shopify_body: request.params}
  self.logger.debug "webhook hit shopify endpoint", log_params

  return Webhookdb::WebhookResponse.error("missing hmac") if shopify_auth.nil?
  request.body.rewind
  request_data = request.body.read
  verified = Webhookdb::Shopify.verify_webhook(request_data, shopify_auth, self.service_integration.webhook_secret)
  return Webhookdb::WebhookResponse.ok if verified
  return Webhookdb::WebhookResponse.error("invalid hmac")
end

#calculate_backfill_state_machineObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 80

def calculate_backfill_state_machine
  step = Webhookdb::Replicator::StateMachineStep.new
  unless self.service_integration.backfill_key.present?
    step.output = \
      %(In order to backfill #{self.resource_name_plural}, we need an API key and password
(file an issue at #{Webhookdb.oss_repo_url} if you need token support).

- From your Shopify Dashboard, go to Apps and click the "Manage Private Apps" link at the bottom of the page.
- Then click "Create Private App" and fill out the necessary information.
- When you get to the "Admin API" section,
select "Read Access" for the #{self.resource_name_singular} API and leave the rest as is.
- Then hit "Save" and create the app.
- You'll be presented with a page that has info about your app's credentials.

We need both the API Key and Password.)
    return step.secret_prompt("API Key").backfill_key(self.service_integration)
  end

  unless self.service_integration.backfill_secret.present?
    return step.secret_prompt("Password").backfill_secret(self.service_integration)
  end

  unless self.service_integration.api_url.present?
    step.output = %(Nice! Now we need the name of your shop so that we can construct the api url.
This is the name that is used by Shopify for URL purposes.
It should be in the top left corner of your Admin Dashboard next to the Shopify logo.)
    step.post_to_url = self.service_integration.authed_api_path + "/transition/shop_name"
    return step.prompting("Shop Name")
  end

  # we check backfill credentials *after* entering the api_url because it is required to establish the auth connection
  unless (result = self.verify_backfill_credentials).verified
    self.service_integration.replicator.clear_backfill_information
    step.output = result.message
    return step.secret_prompt("API Key").backfill_key(self.service_integration)
  end

  step.output = %(Great! We are going to start backfilling your #{self.resource_name_plural}.
#{self._mixin_backfill_warning}
#{self._query_help_output})
  return step.completed
end

#calculate_webhook_state_machineObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 51

def calculate_webhook_state_machine
  step = Webhookdb::Replicator::StateMachineStep.new
  # if the service integration doesn't exist, create it with some standard values
  unless self.service_integration.webhook_secret.present?
    step.needs_input = true
    step.output = %(You are about to start replicating #{self.resource_name_plural} into WebhookDB.
We've made an endpoint available for #{self.resource_name_singular} webhooks:

#{self._webhook_endpoint}

From your Shopify admin dashboard, go to Settings -> Notifications.
Scroll down to the Webhook Section.
You will need to create a separate webhook for each #{self.resource_name_singular} event,
but you can use the URL above and select JSON as the desired format for all of them.

At the very bottom of the page, you should see a signing secret that will be used to verify all webhooks.
Copy that value.)
    return step.secret_prompt("secret").webhook_secret(self.service_integration)
  end

  step.output = %(Great! WebhookDB is now listening for #{self.resource_name_singular} webhooks.
#{self._query_help_output}
In order to backfill existing #{self.resource_name_plural}, run this from a shell:

#{self._backfill_command}
    )
  return step.completed
end

#process_state_change(field, value) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/webhookdb/replicator/shopify_v1_mixin.rb', line 41

def process_state_change(field, value)
  # special handling for converting a shop name into an api url
  if field == "shop_name"
    # revisionist history
    field = "api_url"
    value = "https://#{value}.myshopify.com"
  end
  return super(field, value)
end