disco_app 0.9.9 → 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 185b1ee723fc9de5c2e8b8c8eba75d263edad105ca9be9019edd521cfbe41e62
4
- data.tar.gz: 6b38ec8f3008ddb7108d35954cc0aef922ae275129df914306b4f158c045a4d2
3
+ metadata.gz: 6733d6f14a6a34189871a0255a65ca91527f864eb9e79ffff55e60aa4dc92a5d
4
+ data.tar.gz: a2988c764253a7e52a6ee67d1d27d57443296dc5c38534697d736fcb39c6575f
5
5
  SHA512:
6
- metadata.gz: e32221aa0b199edaf6a3f27410cda764f5afe016e24854cd78aee2e8772a7aa6d6c81a54f1a3c9001a36b30cd196c48fce4701d66df3458792bd8f586dab1c8e
7
- data.tar.gz: 0d178a9ac7c9fe9f07a727516e58a34dfa96a6f11e758d113356f23b66098666f9145061db969074a752b2174f71d0007e7f5ced8092f05ec9814ca1a400e476
6
+ metadata.gz: 4a2f55c26a2f1bdb55825784a3eb35015c3cb5dc4999a28ba73f8ed7d8a12d482259c5675f70650f1b3530a259da29dba4a221d7df282c15df721981a5a8bd7d
7
+ data.tar.gz: f7205e93ab775c08e91a692874e84bd5914f2db122e73123862c88b2854dec7b98f72cac97d7e2f9f6c65d05e67d3ec03e346a5e9d59e9aef07732b1e1819574
@@ -0,0 +1,42 @@
1
+ module DiscoApp::Concerns::CanBeLiquified
2
+ extend ActiveSupport::Concern
3
+
4
+ SPLIT_ARRAY_SEPARATOR = '@!@'
5
+
6
+ included do
7
+
8
+ # Return this model as an array of Liquid {% assign %} statements.
9
+ def as_liquid
10
+ as_json.map { |k, v| "{% assign #{liquid_model_name}_#{k} = #{as_liquid_value(v)} %}" }
11
+ end
12
+
13
+ # Render this model as a series of concatenated Liquid {% assign %} statements.
14
+ def to_liquid
15
+ as_liquid.join("\n")
16
+ end
17
+
18
+ # Method to allow override of the model name in Liquid. Useful for models
19
+ # residing in namespaces that would otherwise have very long prefixes.
20
+ def liquid_model_name
21
+ model_name.param_key
22
+ end
23
+
24
+ private
25
+
26
+ # Return given value as a string expression that will be evaluated in Liquid to result in the correct value type.
27
+ def as_liquid_value(value)
28
+ if value.is_a? Numeric or (!!value == value)
29
+ return value.to_s
30
+ end
31
+ if value.nil?
32
+ return "nil"
33
+ end
34
+ if value.is_a? Array
35
+ return "\"#{value.map { |e| (e.is_a? String) ? CGI::escapeHTML(e) : e }.join(SPLIT_ARRAY_SEPARATOR)}\" | split: \"#{SPLIT_ARRAY_SEPARATOR}\""
36
+ end
37
+ "\"#{CGI::escapeHTML(value.to_s)}\""
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,48 @@
1
+ module DiscoApp::Concerns::HasMetafields
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+
6
+ # Write multiple metafields for this object in a single call.
7
+ #
8
+ # Expects an argument in a nested hash structure with :namespace => :key =>
9
+ # :value, eg:
10
+ #
11
+ # Product.write_metafields(myapp: {
12
+ # key1: 'value1',
13
+ # key2: 'value2'
14
+ # })
15
+ #
16
+ # This method assumes that it is being called within a valid Shopify API
17
+ # session context, eg @shop.temp { ... }.
18
+ #
19
+ # It also assumes that the including class has defined the appropriate value
20
+ # for SHOPIFY_API_CLASS and that calling the `id` method on the instance
21
+ # will return the relevant object's Shopify ID.
22
+ #
23
+ # Returns true on success, false otherwise.
24
+ def write_metafields(metafields)
25
+ self.class::SHOPIFY_API_CLASS.new(
26
+ id: id,
27
+ metafields: build_metafields(metafields)
28
+ ).save
29
+ end
30
+
31
+ # Give a nested hash of metafields in the format described above, return
32
+ # an array of corresponding ShopifyAPI::Metafield instances.
33
+ def build_metafields(metafields)
34
+ metafields.map do |namespace, keys|
35
+ keys.map do |key, value|
36
+ ShopifyAPI::Metafield.new(
37
+ namespace: namespace,
38
+ key: key,
39
+ value: value,
40
+ value_type: value.is_a?(Integer) ? :integer : :string
41
+ )
42
+ end
43
+ end.flatten
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -1,3 +1,3 @@
1
1
  module DiscoApp
2
- VERSION = '0.9.9'
2
+ VERSION = '0.9.10'
3
3
  end
@@ -1,6 +1,9 @@
1
1
  class Product < ActiveRecord::Base
2
2
  include DiscoApp::Concerns::Synchronises
3
+ include DiscoApp::Concerns::HasMetafields
3
4
 
4
5
  belongs_to :shop, class_name: 'DiscoApp::Shop'
5
6
 
7
+ SHOPIFY_API_CLASS = ShopifyAPI::Product
8
+
6
9
  end
@@ -0,0 +1,31 @@
1
+ {
2
+ "product": {
3
+ "id": 632910393,
4
+ "metafields": [
5
+ {
6
+ "namespace": "namespace1",
7
+ "key": "n1key1",
8
+ "value": "value1",
9
+ "value_type": "string"
10
+ },
11
+ {
12
+ "namespace": "namespace1",
13
+ "key": "n1key2",
14
+ "value": 2,
15
+ "value_type": "integer"
16
+ },
17
+ {
18
+ "namespace": "namespace2",
19
+ "key": "n2key3",
20
+ "value": "value3",
21
+ "value_type": "string"
22
+ },
23
+ {
24
+ "namespace": "namespace2",
25
+ "key": "n2key4",
26
+ "value": 2,
27
+ "value_type": "integer"
28
+ }
29
+ ]
30
+ }
31
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "product": {
3
+ "id": 632910393,
4
+ "metafields": [
5
+ {
6
+ "namespace": "namespace1",
7
+ "key": "key1",
8
+ "value": "value1",
9
+ "value_type": "string"
10
+ },
11
+ {
12
+ "namespace": "namespace1",
13
+ "key": "key2",
14
+ "value": 2,
15
+ "value_type": "integer"
16
+ }
17
+ ]
18
+ }
19
+ }
@@ -0,0 +1,7 @@
1
+ {% assign model_numeric = 42 %}
2
+ {% assign model_boolean = true %}
3
+ {% assign model_empty = nil %}
4
+ {% assign model_string = "The cat&#39;s pyjamas are &quot;great&quot;." %}
5
+ {% assign model_array_of_numerics = "1@!@2@!@3" | split: "@!@" %}
6
+ {% assign model_array_of_strings = "A@!@B@!@C" | split: "@!@" %}
7
+ {% assign model_hash = "{}" %}
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class DiscoApp::CanBeLiquifiedTest < ActiveSupport::TestCase
4
+
5
+ class Model
6
+ include ActiveModel::Model
7
+ include DiscoApp::Concerns::CanBeLiquified
8
+
9
+ def initialize(attributes = {})
10
+ @attributes = attributes
11
+ end
12
+
13
+ def as_json
14
+ @attributes.as_json
15
+ end
16
+
17
+ def liquid_model_name
18
+ 'model'
19
+ end
20
+ end
21
+
22
+ def setup
23
+ @model = Model.new(
24
+ numeric: 42,
25
+ boolean: true,
26
+ empty: nil,
27
+ string: "The cat's pyjamas are \"great\".",
28
+ array_of_numerics: [1, 2, 3],
29
+ array_of_strings: ['A', 'B', 'C'],
30
+ hash: {}
31
+ )
32
+ end
33
+
34
+ def teardown
35
+ @model = nil
36
+ end
37
+
38
+ ##
39
+ # Test Liquid output.
40
+ ##
41
+
42
+ test 'correct liquid is output for model' do
43
+ assert_equal liquid_fixture('model.liquid'), @model.to_liquid
44
+ end
45
+
46
+ private
47
+
48
+ # Return an asset fixture as a string.
49
+ def liquid_fixture(path)
50
+ filename = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), 'fixtures', 'liquid', "#{path}")
51
+ File.read(filename)
52
+ end
53
+
54
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class DiscoApp::HasMetafieldsTest < ActiveSupport::TestCase
4
+ include DiscoApp::Test::ShopifyAPI
5
+
6
+ def setup
7
+ @product = products(:ipod)
8
+ @shop = @product.shop
9
+ end
10
+
11
+ def teardown
12
+ @product = nil
13
+ @shop = nil
14
+ end
15
+
16
+ test 'can write metafields with a single namespace' do
17
+ stub_api_request(:put, "#{@shop.admin_url}/products/#{@product.id}.json", 'widget_store/products/write_metafields_single_namespace')
18
+ assert @shop.temp { @product.write_metafields(
19
+ namespace1: {
20
+ key1: 'value1',
21
+ key2: 2
22
+ }
23
+ ) }
24
+ end
25
+
26
+ test 'can write metafields with multiple namespaces' do
27
+ stub_api_request(:put, "#{@shop.admin_url}/products/#{@product.id}.json", 'widget_store/products/write_metafields_multiple_namespaces')
28
+ assert @shop.temp { @product.write_metafields(
29
+ namespace1: {
30
+ n1key1: 'value1',
31
+ n1key2: 2
32
+ },
33
+ namespace2: {
34
+ n2key3: 'value3',
35
+ n2key4: 2
36
+ },
37
+ ) }
38
+ end
39
+
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disco_app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.9
4
+ version: 0.9.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Ballard
@@ -440,6 +440,8 @@ files:
440
440
  - app/models/disco_app/app_settings.rb
441
441
  - app/models/disco_app/application_charge.rb
442
442
  - app/models/disco_app/concerns/app_settings.rb
443
+ - app/models/disco_app/concerns/can_be_liquified.rb
444
+ - app/models/disco_app/concerns/has_metafields.rb
443
445
  - app/models/disco_app/concerns/plan.rb
444
446
  - app/models/disco_app/concerns/plan_code.rb
445
447
  - app/models/disco_app/concerns/renders_assets.rb
@@ -642,6 +644,10 @@ files:
642
644
  - test/fixtures/api/widget_store/charges/get_declined_recurring_application_charge_response.json
643
645
  - test/fixtures/api/widget_store/charges/get_pending_application_charge_response.json
644
646
  - test/fixtures/api/widget_store/charges/get_pending_recurring_application_charge_response.json
647
+ - test/fixtures/api/widget_store/products/write_metafields_multiple_namespaces_request.json
648
+ - test/fixtures/api/widget_store/products/write_metafields_multiple_namespaces_response.json
649
+ - test/fixtures/api/widget_store/products/write_metafields_single_namespace_request.json
650
+ - test/fixtures/api/widget_store/products/write_metafields_single_namespace_response.json
645
651
  - test/fixtures/api/widget_store/shop.json
646
652
  - test/fixtures/api/widget_store/webhooks.json
647
653
  - test/fixtures/assets/test.js
@@ -653,6 +659,7 @@ files:
653
659
  - test/fixtures/disco_app/shops.yml
654
660
  - test/fixtures/disco_app/subscriptions.yml
655
661
  - test/fixtures/js_configurations.yml
662
+ - test/fixtures/liquid/model.liquid
656
663
  - test/fixtures/products.yml
657
664
  - test/fixtures/webhooks/app_uninstalled.json
658
665
  - test/fixtures/webhooks/product_created.json
@@ -662,6 +669,8 @@ files:
662
669
  - test/integration/synchronises_test.rb
663
670
  - test/jobs/disco_app/app_installed_job_test.rb
664
671
  - test/jobs/disco_app/app_uninstalled_job_test.rb
672
+ - test/models/disco_app/can_be_liquified_test.rb
673
+ - test/models/disco_app/has_metafields_test.rb
665
674
  - test/models/disco_app/plan_test.rb
666
675
  - test/models/disco_app/renders_assets_test.rb
667
676
  - test/models/disco_app/session_test.rb
@@ -756,6 +765,7 @@ test_files:
756
765
  - test/dummy/config/database.yml
757
766
  - test/dummy/config/database.gitlab-ci.yml
758
767
  - test/dummy/config/application.rb
768
+ - test/fixtures/liquid/model.liquid
759
769
  - test/fixtures/api/widget_store/assets/create_test_js_response.json
760
770
  - test/fixtures/api/widget_store/assets/create_widget_js_response.json
761
771
  - test/fixtures/api/widget_store/assets/create_test_js_request.json
@@ -774,6 +784,10 @@ test_files:
774
784
  - test/fixtures/api/widget_store/assets/get_script_tags_empty_response.json
775
785
  - test/fixtures/api/widget_store/assets/get_script_tags_empty_request.json
776
786
  - test/fixtures/api/widget_store/assets/create_widget_js_request.json
787
+ - test/fixtures/api/widget_store/products/write_metafields_single_namespace_response.json
788
+ - test/fixtures/api/widget_store/products/write_metafields_multiple_namespaces_request.json
789
+ - test/fixtures/api/widget_store/products/write_metafields_multiple_namespaces_response.json
790
+ - test/fixtures/api/widget_store/products/write_metafields_single_namespace_request.json
777
791
  - test/fixtures/api/widget_store/webhooks.json
778
792
  - test/fixtures/api/widget_store/charges/create_application_charge_response.json
779
793
  - test/fixtures/api/widget_store/charges/create_second_recurring_application_charge_response.json
@@ -822,9 +836,11 @@ test_files:
822
836
  - test/controllers/home_controller_test.rb
823
837
  - test/integration/synchronises_test.rb
824
838
  - test/models/disco_app/plan_test.rb
839
+ - test/models/disco_app/has_metafields_test.rb
825
840
  - test/models/disco_app/shop_test.rb
826
841
  - test/models/disco_app/session_test.rb
827
842
  - test/models/disco_app/subscription_test.rb
843
+ - test/models/disco_app/can_be_liquified_test.rb
828
844
  - test/models/disco_app/renders_assets_test.rb
829
845
  - test/services/disco_app/charges_service_test.rb
830
846
  - test/services/disco_app/subscription_service_test.rb