ruby_shopify_app 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. checksums.yaml +7 -0
  2. data/.babelrc +5 -0
  3. data/.github/CODEOWNERS +2 -0
  4. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  6. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  8. data/.github/probots.yml +2 -0
  9. data/.github/workflows/build.yml +40 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/rubocop.yml +22 -0
  12. data/.gitignore +14 -0
  13. data/.nvmrc +1 -0
  14. data/.rubocop.yml +18 -0
  15. data/.ruby-version +1 -0
  16. data/CHANGELOG-OLD.md +643 -0
  17. data/CHANGELOG.md +6 -0
  18. data/CONTRIBUTING.md +81 -0
  19. data/Gemfile +11 -0
  20. data/Gemfile.lock +280 -0
  21. data/LICENSE +19 -0
  22. data/README.md +132 -0
  23. data/Rakefile +7 -0
  24. data/SECURITY.md +59 -0
  25. data/app/assets/images/storage_access.svg +1 -0
  26. data/app/assets/javascripts/shopify_app/app_bridge_2.0.12.js +10 -0
  27. data/app/assets/javascripts/shopify_app/app_bridge_redirect.js +22 -0
  28. data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
  29. data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
  30. data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
  31. data/app/assets/javascripts/shopify_app/post_redirect.js +9 -0
  32. data/app/assets/javascripts/shopify_app/redirect.js +31 -0
  33. data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
  34. data/app/assets/javascripts/shopify_app/storage_access.js +148 -0
  35. data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
  36. data/app/assets/javascripts/shopify_app/top_level.js +2 -0
  37. data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
  38. data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
  39. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +39 -0
  40. data/app/controllers/concerns/shopify_app/require_known_shop.rb +40 -0
  41. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  42. data/app/controllers/shopify_app/authenticated_controller.rb +8 -0
  43. data/app/controllers/shopify_app/callback_controller.rb +195 -0
  44. data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
  45. data/app/controllers/shopify_app/sessions_controller.rb +202 -0
  46. data/app/controllers/shopify_app/webhooks_controller.rb +36 -0
  47. data/app/views/shopify_app/partials/_button_styles.html.erb +109 -0
  48. data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
  49. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +98 -0
  50. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  51. data/app/views/shopify_app/partials/_layout_styles.html.erb +182 -0
  52. data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
  53. data/app/views/shopify_app/sessions/enable_cookies.html.erb +70 -0
  54. data/app/views/shopify_app/sessions/new.html.erb +51 -0
  55. data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
  56. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +63 -0
  57. data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +13 -0
  58. data/app/views/shopify_app/shared/redirect.html.erb +23 -0
  59. data/config/locales/cs.yml +23 -0
  60. data/config/locales/da.yml +20 -0
  61. data/config/locales/de.yml +22 -0
  62. data/config/locales/en.yml +15 -0
  63. data/config/locales/es.yml +22 -0
  64. data/config/locales/fi.yml +20 -0
  65. data/config/locales/fr.yml +23 -0
  66. data/config/locales/it.yml +21 -0
  67. data/config/locales/ja.yml +17 -0
  68. data/config/locales/ko.yml +19 -0
  69. data/config/locales/nb.yml +21 -0
  70. data/config/locales/nl.yml +21 -0
  71. data/config/locales/pl.yml +21 -0
  72. data/config/locales/pt-BR.yml +21 -0
  73. data/config/locales/pt-PT.yml +22 -0
  74. data/config/locales/sv.yml +21 -0
  75. data/config/locales/th.yml +20 -0
  76. data/config/locales/tr.yml +22 -0
  77. data/config/locales/vi.yml +22 -0
  78. data/config/locales/zh-CN.yml +16 -0
  79. data/config/locales/zh-TW.yml +16 -0
  80. data/config/routes.rb +23 -0
  81. data/docs/Quickstart.md +31 -0
  82. data/docs/Releasing.md +21 -0
  83. data/docs/Troubleshooting.md +159 -0
  84. data/docs/Upgrading.md +132 -0
  85. data/docs/shopify_app/authentication.md +124 -0
  86. data/docs/shopify_app/engine.md +82 -0
  87. data/docs/shopify_app/generators.md +127 -0
  88. data/docs/shopify_app/handling-access-scopes-changes.md +24 -0
  89. data/docs/shopify_app/script-tags.md +28 -0
  90. data/docs/shopify_app/session-repository.md +88 -0
  91. data/docs/shopify_app/testing.md +38 -0
  92. data/docs/shopify_app/webhooks.md +72 -0
  93. data/images/app-proxy-screenshot.png +0 -0
  94. data/karma.conf.js +44 -0
  95. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
  96. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
  97. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
  98. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
  99. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +69 -0
  100. data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +13 -0
  101. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +26 -0
  102. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +8 -0
  103. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +11 -0
  104. data/lib/generators/shopify_app/app_proxy_controller/templates/index.html.erb +19 -0
  105. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
  106. data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
  107. data/lib/generators/shopify_app/controllers/controllers_generator.rb +30 -0
  108. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +53 -0
  109. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +18 -0
  110. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +75 -0
  111. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +12 -0
  112. data/lib/generators/shopify_app/install/install_generator.rb +121 -0
  113. data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +3 -0
  114. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +44 -0
  115. data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
  116. data/lib/generators/shopify_app/install/templates/omniauth.rb +4 -0
  117. data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
  118. data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
  119. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  120. data/lib/generators/shopify_app/install/templates/shopify_app_importmap.js +13 -0
  121. data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
  122. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  123. data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
  124. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  125. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  126. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
  127. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
  128. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -0
  129. data/lib/generators/shopify_app/routes/routes_generator.rb +32 -0
  130. data/lib/generators/shopify_app/routes/templates/routes.rb +12 -0
  131. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +70 -0
  132. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  133. data/lib/generators/shopify_app/shop_model/templates/db/migrate/create_shops.erb +15 -0
  134. data/lib/generators/shopify_app/shop_model/templates/shop.rb +8 -0
  135. data/lib/generators/shopify_app/shop_model/templates/shops.yml +3 -0
  136. data/lib/generators/shopify_app/shopify_app_generator.rb +18 -0
  137. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  138. data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
  139. data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
  140. data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
  141. data/lib/generators/shopify_app/user_model/user_model_generator.rb +70 -0
  142. data/lib/generators/shopify_app/views/views_generator.rb +30 -0
  143. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  144. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  145. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  146. data/lib/shopify_app/configuration.rb +119 -0
  147. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +38 -0
  148. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  149. data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
  150. data/lib/shopify_app/controller_concerns/itp.rb +45 -0
  151. data/lib/shopify_app/controller_concerns/localization.rb +23 -0
  152. data/lib/shopify_app/controller_concerns/login_protection.rb +259 -0
  153. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  154. data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
  155. data/lib/shopify_app/engine.rb +47 -0
  156. data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
  157. data/lib/shopify_app/jobs/webhooks_manager_job.rb +16 -0
  158. data/lib/shopify_app/managers/scripttags_manager.rb +78 -0
  159. data/lib/shopify_app/managers/webhooks_manager.rb +62 -0
  160. data/lib/shopify_app/middleware/jwt_middleware.rb +43 -0
  161. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
  162. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  163. data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
  164. data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
  165. data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
  166. data/lib/shopify_app/session/jwt.rb +67 -0
  167. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  168. data/lib/shopify_app/session/session_repository.rb +56 -0
  169. data/lib/shopify_app/session/session_storage.rb +20 -0
  170. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  171. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  172. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  173. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  174. data/lib/shopify_app/test_helpers/all.rb +2 -0
  175. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  176. data/lib/shopify_app/utils.rb +37 -0
  177. data/lib/shopify_app/version.rb +4 -0
  178. data/lib/shopify_app.rb +80 -0
  179. data/package.json +27 -0
  180. data/service.yml +4 -0
  181. data/shipit.rubygems.yml +4 -0
  182. data/shopify_app.gemspec +39 -0
  183. data/translation.yml +7 -0
  184. data/webpack.config.js +24 -0
  185. data/yarn.lock +5230 -0
  186. metadata +465 -0
@@ -0,0 +1,88 @@
1
+ # Session repository
2
+
3
+ #### Table of contents
4
+
5
+ [`ShopifyApp::SessionRepository`](#shopifyappsessionrepository)
6
+ * [Shop-based token storage](#shop-based-token-storage)
7
+ * [User-based token storage](#user-based-token-storage)
8
+
9
+ [Access scopes](#access-scopes)
10
+ * [`ShopifyApp::ShopSessionStorageWithScopes`](#shopifyappshopsessionstoragewithscopes)
11
+ * [``ShopifyApp::UserSessionStorageWithScopes``](#shopifyappusersessionstoragewithscopes)
12
+
13
+ [Migrating from shop-based to user-based token strategy](#migrating-from-shop-based-to-user-based-token-strategy)
14
+
15
+ ## ShopifyApp::SessionRepository
16
+
17
+ `ShopifyApp::SessionRepository` allows you as a developer to define how your sessions are stored and retrieved for shops. The `SessionRepository` is configured in the `config/initializers/shopify_app.rb` file and can be set to any object that implements `self.store(auth_session, *args)` which stores the session and returns a unique identifier and `self.retrieve(id)` which returns a `ShopifyAPI::Session` for the passed id. These methods are already implemented as part of the `ShopifyApp::SessionStorage` concern but can be overridden for custom implementation.
18
+
19
+ ### Shop-based token storage
20
+
21
+ Storing tokens on the store model means that any user login associated with the store will have equal access levels to whatever the original user granted the app.
22
+ ```sh
23
+ $ rails generate shopify_app:shop_model
24
+ ```
25
+ This will generate a shop model which will be the storage for the tokens necessary for authentication.
26
+
27
+ ### User-based token storage
28
+
29
+ A more granular control over the level of access per user on an app might be necessary, to which the shop-based token strategy is not sufficient. Shopify supports a user-based token storage strategy where a unique token to each user can be managed. Shop tokens must still be maintained if you are running background jobs so that you can make use of them when necessary.
30
+ ```sh
31
+ $ rails generate shopify_app:shop_model
32
+ $ rails generate shopify_app:user_model
33
+ ```
34
+ This will generate a shop model and user model, which will be the storage for the tokens necessary for authentication.
35
+
36
+ The current Shopify user will be stored in the rails session at `session[:shopify_user]`
37
+
38
+ Read more about Online vs. Offline access [here](https://help.shopify.com/api/getting-started/authentication/oauth).
39
+
40
+ ## Access scopes
41
+
42
+ If you want to customize how access scopes are stored for shops and users, you can implement the `access_scopes` getters and setters in the models that include `ShopifyApp::ShopSessionStorageWithScopes` and `ShopifyApp::UserSessionStorageWithScopes` as shown:
43
+
44
+ ### `ShopifyApp::ShopSessionStorageWithScopes`
45
+ ```ruby
46
+ class Shop < ActiveRecord::Base
47
+ include ShopifyApp::ShopSessionStorageWithScopes
48
+
49
+ def access_scopes=(scopes)
50
+ # Store access scopes
51
+ end
52
+ def access_scopes
53
+ # Find access scopes
54
+ end
55
+ end
56
+ ```
57
+
58
+ ### `ShopifyApp::UserSessionStorageWithScopes`
59
+ ```ruby
60
+ class User < ActiveRecord::Base
61
+ include ShopifyApp::UserSessionStorageWithScopes
62
+
63
+ def access_scopes=(scopes)
64
+ # Store access scopes
65
+ end
66
+ def access_scopes
67
+ # Find access scopes
68
+ end
69
+ end
70
+ ```
71
+ ## Migrating from shop-based to user-based token strategy
72
+
73
+ 1. Run the `user_model` generator as mentioned above.
74
+ 2. Ensure that both your `Shop` model and `User` model includes the necessary concerns `ShopifyApp::ShopSessionStorage` and `ShopifyApp::UserSessionStorage`.
75
+ 3. Make changes to 2 initializer files as shown below:
76
+ ```ruby
77
+ # In the `omniauth.rb` initializer:
78
+ provider :shopify,
79
+ ...
80
+ setup: lambda { |env|
81
+ configuration = ShopifyApp::OmniAuthConfiguration.new(env['omniauth.strategy'], Rack::Request.new(env))
82
+ configuration.build_options
83
+ }
84
+
85
+ # In the `shopify_app.rb` initializer:
86
+ config.shop_session_repository = {YOUR_SHOP_MODEL_CLASS}
87
+ config.user_session_repository = {YOUR_USER_MODEL_CLASS}
88
+ ```
@@ -0,0 +1,38 @@
1
+ # Testing
2
+
3
+ #### Table of contents
4
+
5
+ [Using test helpers inside your application](#using-test-helpers-inside-your-application)
6
+
7
+ [Testing an embedded app outside the Shopify admin](#testing-an-embedded-app-outside-the-shopify-admin)
8
+
9
+ ## Using test helpers inside your application
10
+
11
+ A test helper that will allow you to test `ShopifyApp::WebhookVerification` in the controller from your app, to use this test, you need to `require` it directly inside your app `test/controllers/webhook_verification_test.rb`.
12
+
13
+ ```ruby
14
+ require 'test_helper'
15
+ require 'action_controller'
16
+ require 'action_controller/base'
17
+ require 'shopify_app/test_helpers/webhook_verification_helper'
18
+ ```
19
+
20
+ Or you can require in your `test/test_helper.rb`.
21
+
22
+ ```ruby
23
+ ENV['RAILS_ENV'] ||= 'test'
24
+ require_relative '../config/environment'
25
+ require 'rails/test_help'
26
+ require 'byebug'
27
+ require 'shopify_app/test_helpers/all'
28
+ ```
29
+
30
+ With `lib/shopify_app/test_helpers/all'` more tests can be added and will only need to be required in once in your library.
31
+
32
+ ## Testing an embedded app outside the Shopify admin
33
+
34
+ By default, loading your embedded app will redirect to the Shopify admin, with the app view loaded in an `iframe`. If you need to load your app outside of the Shopify admin (e.g., for performance testing), you can change `forceRedirect: true` to `false` in `ShopifyApp.init` block in the `embedded_app` view. To keep the redirect on in production but off in your `development` and `test` environments, you can use:
35
+
36
+ ```javascript
37
+ forceRedirect: <%= Rails.env.development? || Rails.env.test? ? 'false' : 'true' %>
38
+ ```
@@ -0,0 +1,72 @@
1
+ # Webhooks
2
+
3
+ #### Table of contents
4
+
5
+ [Manage webhooks using `ShopifyApp::WebhooksManager`](#manage-webhooks-using-shopifyappwebhooksmanager)
6
+
7
+ ## Manage webhooks using `ShopifyApp::WebhooksManager`
8
+
9
+ See [`ShopifyApp::WebhooksManager`](/lib/shopify_app/managers/webhooks_manager.rb)
10
+ ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer:
11
+
12
+ ```ruby
13
+ ShopifyApp.configure do |config|
14
+ config.webhooks = [
15
+ {topic: 'carts/update', address: 'https://example.com/webhooks/carts_update'}
16
+ ]
17
+ end
18
+ ```
19
+
20
+ When the [OAuth callback](/docs/shopify_app/authentication.md#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.
21
+
22
+ ShopifyApp also provides a [WebhooksController](/app/controllers/shopify_app/webhooks_controller.rb) 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).
23
+
24
+ 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`):
25
+
26
+ ```ruby
27
+ ShopifyApp.configure do |config|
28
+ config.webhook_jobs_namespace = 'shopify/webhooks'
29
+ end
30
+ ```
31
+
32
+ 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.
33
+
34
+ ```ruby
35
+ ShopifyApp.configure do |config|
36
+ config.webhooks = [
37
+ {topic: 'products/update', address: 'https://example.com/webhooks/products_update', fields: ['title', 'vendor']}
38
+ ]
39
+ end
40
+ ```
41
+
42
+ If you'd rather implement your own controller then you'll want to use the [`ShopifyApp::WebhookVerification`](/lib/shopify_app/controller_concerns/webhook_verification.rb) module to verify your webhooks, example:
43
+
44
+ ```ruby
45
+ class CustomWebhooksController < ApplicationController
46
+ include ShopifyApp::WebhookVerification
47
+
48
+ def carts_update
49
+ params.permit!
50
+ SomeJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
51
+ head :no_content
52
+ end
53
+
54
+ private
55
+
56
+ def webhook_params
57
+ params.except(:controller, :action, :type)
58
+ end
59
+ end
60
+ ```
61
+
62
+ The module skips the `verify_authenticity_token` before_action and adds an action to verify that the webhook came from Shopify. You can now add a post route to your application, pointing to the controller and action to accept the webhook data from Shopify.
63
+
64
+ The WebhooksManager uses ActiveJob. If ActiveJob is not configured then by default Rails will run the jobs inline. However, it is highly recommended to configure a proper background processing queue like Sidekiq or Resque in production.
65
+
66
+ ShopifyApp can create webhooks for you using the `add_webhook` generator. This will add the new webhook to your config and create the required job class for you.
67
+
68
+ ```
69
+ rails g shopify_app:add_webhook -t carts/update -a https://example.com/webhooks/carts_update
70
+ ```
71
+
72
+ Where `-t` is the topic and `-a` is the address the webhook should be sent to.
Binary file
data/karma.conf.js ADDED
@@ -0,0 +1,44 @@
1
+ const karmaReporters = ['mocha-clean'];
2
+
3
+ function isDebug(argument) {
4
+ return argument === '--debug';
5
+ }
6
+
7
+ module.exports = function(config) {
8
+ config.set({
9
+ mode: 'development',
10
+ basePath: '',
11
+ frameworks: ['mocha', 'chai-sinon'],
12
+ files: [
13
+ 'app/assets/javascripts/**/*.js',
14
+ 'test/javascripts/**/*test.js',
15
+ ],
16
+ exclude: [
17
+ // Exclude JS files that create 'DOMContentLoaded' event listeners
18
+ 'app/assets/javascripts/**/redirect.js',
19
+ 'app/assets/javascripts/**/storage_access_redirect.js',
20
+ 'app/assets/javascripts/**/top_level_interaction.js',
21
+ 'app/assets/javascripts/**/partition_cookies.js',
22
+ ],
23
+ mochaReporter: {
24
+ output: 'autowatch',
25
+ },
26
+ preprocessors: {
27
+ 'test/javascripts/**/*test.js': ['webpack'],
28
+ },
29
+ webpack: {},
30
+ reporters: karmaReporters,
31
+ port: 9876,
32
+ colors: true,
33
+ logLevel: config.LOG_WARN,
34
+ autoWatch: false,
35
+ browsers: ['ChromeHeadless'],
36
+ singleRun: true,
37
+ client: {
38
+ mocha: {
39
+ ui: 'tdd',
40
+ grep: config.grep,
41
+ },
42
+ },
43
+ });
44
+ };
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class AddAfterAuthenticateJobGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ hook_for :test_framework, as: :job, in: :rails do |instance, generator|
10
+ instance.invoke(generator, [instance.send(:job_file_name)])
11
+ end
12
+
13
+ def init_after_authenticate_config
14
+ initializer = load_initializer
15
+
16
+ after_authenticate_job_config =
17
+ " config.after_authenticate_job = "\
18
+ "{ job: \"Shopify::AfterAuthenticateJob\", inline: false }\n"
19
+
20
+ inject_into_file(
21
+ 'config/initializers/shopify_app.rb',
22
+ after_authenticate_job_config,
23
+ before: 'end'
24
+ )
25
+
26
+ unless initializer.include?(after_authenticate_job_config)
27
+ shell.say("Error adding after_authenticate_job to config. Add this line manually: "\
28
+ "#{after_authenticate_job_config}", :red)
29
+ end
30
+ end
31
+
32
+ def add_after_authenticate_job
33
+ template('after_authenticate_job.rb', "app/jobs/#{job_file_name}_job.rb")
34
+ end
35
+
36
+ private
37
+
38
+ def load_initializer
39
+ File.read(File.join(destination_root, 'config/initializers/shopify_app.rb'))
40
+ end
41
+
42
+ def job_file_name
43
+ 'shopify/after_authenticate'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ module Shopify
3
+ class AfterAuthenticateJob < ActiveJob::Base
4
+ def perform(shop_domain:)
5
+ shop = Shop.find_by(shopify_domain: shop_domain)
6
+
7
+ shop.with_shopify_session do
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class AddMarketingActivityExtensionGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def generate_app_extension
10
+ template("marketing_activities_controller.rb", "app/controllers/marketing_activities_controller.rb")
11
+ generate_routes
12
+ end
13
+
14
+ private
15
+
16
+ def generate_routes
17
+ inject_into_file(
18
+ 'config/routes.rb',
19
+ optimize_indentation(routes, 2),
20
+ after: "root :to => 'home#index'\n"
21
+ )
22
+ end
23
+
24
+ def routes
25
+ <<~EOS
26
+
27
+ resource :marketing_activities, only: [:create, :update] do
28
+ patch :resume
29
+ patch :pause
30
+ patch :delete
31
+ post :republish
32
+ post :preload_form_data
33
+ post :preview
34
+ post :errors
35
+ end
36
+ EOS
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MarketingActivitiesController < ShopifyApp::ExtensionVerificationController
4
+ def preload_form_data
5
+ preload_data = {
6
+ "form_data": {},
7
+ }
8
+ render(json: preload_data, status: :ok)
9
+ end
10
+
11
+ def update
12
+ render(json: {}, status: :accepted)
13
+ end
14
+
15
+ def pause
16
+ render(json: {}, status: :accepted)
17
+ end
18
+
19
+ def resume
20
+ render(json: {}, status: :accepted)
21
+ end
22
+
23
+ def delete
24
+ render(json: {}, status: :accepted)
25
+ end
26
+
27
+ def preview
28
+ placeholder_img = "https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-image_small.png"
29
+ preview_response = {
30
+ "desktop": {
31
+ "preview_url": placeholder_img,
32
+ "content_type": "text/html",
33
+ "width": 360,
34
+ "height": 200,
35
+ },
36
+ "mobile": {
37
+ "preview_url": placeholder_img,
38
+ "content_type": "text/html",
39
+ "width": 360,
40
+ "height": 200,
41
+ },
42
+ }
43
+ render(json: preview_response, status: :ok)
44
+ end
45
+
46
+ def create
47
+ render(json: {}, status: :ok)
48
+ end
49
+
50
+ def republish
51
+ render(json: {}, status: :accepted)
52
+ end
53
+
54
+ def errors
55
+ request_id = params[:request_id]
56
+ message = params[:message]
57
+
58
+ Rails.logger.info("[Marketing Activity App Error Feedback] Request id: #{request_id}, message: #{message}")
59
+
60
+ render(json: {}, status: :ok)
61
+ end
62
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class AddWebhookGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+ class_option :topic, type: :string, aliases: "-t", required: true
9
+ class_option :address, type: :string, aliases: "-a", required: true
10
+
11
+ hook_for :test_framework, as: :job, in: :rails do |instance, generator|
12
+ instance.invoke(generator, [instance.send(:job_file_name)])
13
+ end
14
+
15
+ def init_webhook_config
16
+ initializer = load_initializer
17
+ return if initializer.include?("config.webhooks")
18
+
19
+ inject_into_file(
20
+ 'config/initializers/shopify_app.rb',
21
+ " config.webhooks = [\n ]\n",
22
+ before: 'end'
23
+ )
24
+ end
25
+
26
+ def inject_webhook_to_shopify_app_initializer
27
+ inject_into_file(
28
+ 'config/initializers/shopify_app.rb',
29
+ webhook_config,
30
+ after: "config.webhooks = ["
31
+ )
32
+
33
+ initializer = load_initializer
34
+
35
+ unless initializer.include?(webhook_config)
36
+ shell.say("Error adding webhook to config. Add this line manually: #{webhook_config}", :red)
37
+ end
38
+ end
39
+
40
+ def add_webhook_job
41
+ @job_file_name = job_file_name + '_job'
42
+ @job_class_name = @job_file_name.classify
43
+ template('webhook_job.rb', "app/jobs/#{@job_file_name}.rb")
44
+ end
45
+
46
+ private
47
+
48
+ def job_file_name
49
+ address.split('/').last
50
+ end
51
+
52
+ def load_initializer
53
+ File.read(File.join(destination_root, 'config/initializers/shopify_app.rb'))
54
+ end
55
+
56
+ def webhook_config
57
+ "\n {topic: '#{topic}', address: '#{address}', format: 'json'},"
58
+ end
59
+
60
+ def topic
61
+ options['topic']
62
+ end
63
+
64
+ def address
65
+ options['address']
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ class <%= @job_class_name %> < ActiveJob::Base
2
+ def perform(shop_domain:, webhook:)
3
+ shop = Shop.find_by(shopify_domain: shop_domain)
4
+
5
+ if shop.nil?
6
+ logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'")
7
+ return
8
+ end
9
+
10
+ shop.with_shopify_session do
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class AppProxyControllerGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def create_app_proxy_controller
10
+ template('app_proxy_controller.rb', 'app/controllers/app_proxy_controller.rb')
11
+ end
12
+
13
+ def create_app_proxy_index_view
14
+ copy_file('index.html.erb', 'app/views/app_proxy/index.html.erb')
15
+ end
16
+
17
+ def add_app_proxy_route
18
+ inject_into_file(
19
+ 'config/routes.rb',
20
+ File.read(File.expand_path(find_in_source_paths('app_proxy_route.rb'))),
21
+ after: "mount ShopifyApp::Engine, at: '/'\n"
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ class AppProxyController < ApplicationController
3
+ include ShopifyApp::AppProxyVerification
4
+
5
+ def index
6
+ render(layout: false, content_type: 'application/liquid')
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :app_proxy do
4
+ root action: 'index'
5
+ # simple routes without a specified controller will go to AppProxyController
6
+
7
+ # more complex routes will go to controllers in the AppProxy namespace
8
+ # resources :reviews
9
+ # GET /app_proxy/reviews will now be routed to
10
+ # AppProxy::ReviewsController#index, for example
11
+ end
@@ -0,0 +1,19 @@
1
+ <h2>App Proxy Page</h2>
2
+
3
+ <p>Congratulations! You have successfully configured App Proxy for your shopify application.</p>
4
+ <p>Any valid liquid code included in this page will be parsed and rendered by Shopify.</p>
5
+
6
+ <br>
7
+
8
+ <pre>
9
+ Your Shop Details:
10
+ <hr>
11
+ Shop Name : {{ shop.name }}
12
+ myshopify name : {{ shop.permanent_domain }}
13
+ Primary Domain of Shop : {{ shop.domain }}
14
+ Shop URL : {{ shop.url }}
15
+ Shop Currency : {{ shop.currency }}
16
+ No. of products in this shop : {{ shop.products_count }}
17
+ <br>
18
+ <hr>
19
+ </pre>
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module ShopifyApp
6
+ module Generators
7
+ class AuthenticatedControllerGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_authenticated_controller
11
+ template('authenticated_controller.rb', 'app/controllers/authenticated_controller.rb')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AuthenticatedController < ApplicationController
4
+ include ShopifyApp::Authenticated
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class ControllersGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../../../../..", __FILE__)
8
+
9
+ def create_controllers
10
+ controllers.each do |controller|
11
+ copy_file(controller)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def controllers
18
+ files_within_root('.', 'app/controllers/shopify_app/*.*')
19
+ end
20
+
21
+ def files_within_root(prefix, glob)
22
+ root = "#{self.class.source_root}/#{prefix}"
23
+
24
+ Dir["#{root}/#{glob}"].sort.map do |full_path|
25
+ full_path.sub(root, '.').gsub('/./', '/')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class HomeControllerGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ class_option :with_cookie_authentication, type: :boolean, default: false
10
+ class_option :embedded, type: :string, default: 'true'
11
+
12
+ def create_home_controller
13
+ template(home_controller_template, 'app/controllers/home_controller.rb')
14
+ end
15
+
16
+ def create_products_controller
17
+ generate("shopify_app:products_controller") unless with_cookie_authentication?
18
+ end
19
+
20
+ def create_home_index_view
21
+ template('index.html.erb', 'app/views/home/index.html.erb')
22
+ end
23
+
24
+ def add_home_index_route
25
+ route("root :to => 'home#index'")
26
+ end
27
+
28
+ private
29
+
30
+ def embedded?
31
+ options['embedded'] == 'true'
32
+ end
33
+
34
+ def embedded_app?
35
+ ShopifyApp.configuration.embedded_app?
36
+ end
37
+
38
+ def with_cookie_authentication?
39
+ options['with_cookie_authentication']
40
+ end
41
+
42
+ def home_controller_template
43
+ return 'unauthenticated_home_controller.rb' unless authenticated_home_controller_required?
44
+
45
+ 'home_controller.rb'
46
+ end
47
+
48
+ def authenticated_home_controller_required?
49
+ with_cookie_authentication? || !embedded? || !embedded_app?
50
+ end
51
+ end
52
+ end
53
+ end