From 32b93f82e2f771fcf87245b89cf60a73f64d91fb Mon Sep 17 00:00:00 2001 From: Kali Donovan Date: Thu, 31 Aug 2017 17:19:36 +0100 Subject: [PATCH] Allow specifying namespace for webhooks jobs --- CHANGELOG.md | 4 ++++ README.md | 19 ++++++++++++++----- .../shopify_app/webhooks_controller.rb | 10 +++++++++- lib/shopify_app/configuration.rb | 3 +++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0b7fd0c2..2c9d99a17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +Upcoming +----- +* Add `webhook_jobs_namespace` config option. [[#463]](https://github.com/Shopify/shopify_app/pull/463) + 8.1.0 ----- * Add support for per_user_authentication diff --git a/README.md b/README.md index 52e09a3c9..a7c5d8998 100644 --- a/README.md +++ b/README.md @@ -182,14 +182,15 @@ Mounting the Engine will provide the basic routes to authenticating a shop with |GET |'/logout' |Logout | |POST |'/webhooks/:type' |Webhook Callback | +### Nested Routes -If required the engine can be mounted at a nested route, eg: +The engine may also be mounted at a nested route, for example: ```ruby mount ShopifyApp::Engine, at: '/nested' ``` -This will create the Shopify engine routes under the specified subpath. You'll also need to make some updates to your `shopify_app.rb` and `omniauth.rb` initializers. First update the shopify_app initializer to include a custom `root_url` e.g: +This will create the Shopify engine routes under the specified subpath. You'll also need to make some updates to your `shopify_app.rb` and `omniauth.rb` initializers. First update the shopify_app initializer to include a custom `root_url` e.g.: ```ruby ShopifyApp.configure do |config| @@ -197,7 +198,7 @@ ShopifyApp.configure do |config| end ``` -then update the omniauth initializer to include a custom `callback_path` e.g: +then update the omniauth initializer to include a custom `callback_path` e.g.: ```ruby provider :shopify, @@ -242,7 +243,7 @@ end WebhooksManager --------------- -ShopifyApp can manage your app's webhooks for you by setting which webhooks you require in the initializer: +ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer: ```ruby ShopifyApp.configure do |config| @@ -254,7 +255,15 @@ end When the oauth callback is completed successfully ShopifyApp will queue a background job which will ensure all the specified webhooks exist for that shop. Because this runs on every oauth callback it means your app will always have the webhooks it needs even if the user uninstalls and re-installs the app. -ShopifyApp also provides a WebhooksController that receives webhooks and queues a job based on the webhook url. For example if you register the webhook from above then all you need to do is create a job called `CartsUpdateJob`. The job will be queued with 2 params `shop_domain` and `webhook` which is the webhook body. +ShopifyApp also provides a WebhooksController that receives webhooks and queues a job based on the received topic. For example if you register the webhook from above then all you need to do is create a job called `CartsUpdateJob`. The job will be queued with 2 params: `shop_domain` and `webhook` (which is the webhook body). + +If you would like to namespace your jobs you may set `webhook_jobs_namespace` in the config. For example if your app handles webhooks from other ecommerce applications as well, and you want Shopify cart update webhooks to be processed by a job living in `jobs/shopify/webhooks/carts_update_job.rb` rather than `jobs/carts_update_job.rb`): + +```ruby +ShopifyApp.configure do |config| + config.webhook_jobs_namespace = 'shopify/webhooks' +end +``` If you are only interested in particular fields, you can optionally filter the data sent by Shopify by specifying the `fields` parameter in `config/webhooks`. Note that you will still receive a webhook request from Shopify every time the resource is updated, but only the specified fields will be sent. diff --git a/app/controllers/shopify_app/webhooks_controller.rb b/app/controllers/shopify_app/webhooks_controller.rb index 265f7088f..7390f690b 100644 --- a/app/controllers/shopify_app/webhooks_controller.rb +++ b/app/controllers/shopify_app/webhooks_controller.rb @@ -18,11 +18,19 @@ def webhook_params end def webhook_job_klass - "#{webhook_type.classify}Job".safe_constantize or raise ShopifyApp::MissingWebhookJobError + webhook_job_klass_name.safe_constantize or raise ShopifyApp::MissingWebhookJobError + end + + def webhook_job_klass_name + [webhook_namespace, "#{webhook_type}_job"].compact.join('/').classify end def webhook_type params[:type] end + + def webhook_namespace + ShopifyApp.configuration.webhook_jobs_namespace + end end end diff --git a/lib/shopify_app/configuration.rb b/lib/shopify_app/configuration.rb index c05c2ae5a..da03c17bc 100644 --- a/lib/shopify_app/configuration.rb +++ b/lib/shopify_app/configuration.rb @@ -25,6 +25,9 @@ class Configuration # configure myshopify domain for local shopify development attr_accessor :myshopify_domain + # allow namespacing webhook jobs + attr_accessor :webhook_jobs_namespace + def initialize @root_url = '/' @myshopify_domain = 'myshopify.com'