shopify-default_variant 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26a82ea5223f1dffa187a6fae8aea1e66e31cdda5561e1a7e4555dbc4f74c604
4
+ data.tar.gz: 4769647f463ecfd9e140f20cd00ce1522d377caa1b560d60061639b4fa4b88c1
5
+ SHA512:
6
+ metadata.gz: b4d4908effdc353b787b2819fd37398f0c2f3f17a99400c693d869ea79fd2f446cd59f82e3180a7813fd261c405bd08733c1000ee9f6f8a2af5d3080d5aed881
7
+ data.tar.gz: ba4efd402ba8e51adcd1af39c61e553f0be1839b56365298a9cc069ed109d01e0498ac4bc3654e5b88d9dc5be2072106f7a55e8066d2bbb3844f40a1c18df975
data/.env.template ADDED
@@ -0,0 +1,8 @@
1
+ # To run tests:
2
+ #
3
+ # 1. Copy this file to .env
4
+ # 2. Fill-in the below variables with valid values.
5
+ # These values will be used by the tests. Data will be written to SHOPIFY_DOMAIN
6
+ #
7
+ SHOPIFY_DOMAIN=
8
+ SHOPIFY_TOKEN=
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in shopify-default_variant.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 sshaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Shopify::DefaultVariant
2
+
3
+ [![CI](https://github.com/ScreenStaring/shopify-default_variant/actions/workflows/ci.yml/badge.svg)](https://github.com/ScreenStaring/shopify-default_variant/actions/workflows/ci.yml)
4
+
5
+ Determine if the given Shopify product only has a default variant, or if the given variant is the default variant.
6
+
7
+ Works with a variety of objects:
8
+
9
+ - ShopifyAPI gem response objects
10
+ - GraphQL response `Hash` with `String` or `Symbol` keys
11
+ - Plain Ol' Ruby Object (PORO)
12
+ - Plain `Hash` with `String` or `Symbol` keys
13
+
14
+ ## Usage
15
+
16
+ ```rb
17
+ Shopify::DefaultVariant.match?(object)
18
+ ```
19
+
20
+ Where `object` can be a product or a variant in any of the aforementioned forms.
21
+
22
+ More examples:
23
+
24
+ ```rb
25
+ require "shopify_api" # Just an example, *not* a recommendation 😉
26
+
27
+ product = ShopifyAPI::Product.find(id: id)
28
+ Shopify::DefaultVariant.match?(product)
29
+ Shopify::DefaultVariant.match?(product.variants)
30
+ Shopify::DefaultVariant.match?(product.variants.sample)
31
+
32
+ require "shopify_api-graphql-tiny"
33
+
34
+ gql = ShopifyAPI::GraphQL::Tiny.new("shop", "token")
35
+
36
+ data = gql.execute(YOUR_QUERY)
37
+ # Be sure to remove the data key and query's name key (whatever it may be)
38
+ Shopify::DefaultVariant.match?(data.dig("data", "queryName"))
39
+
40
+ # Or use a Object or Hash that resembles one of the well-known forms
41
+ Shopify::DefaultVariant.match?(my_custom_object)
42
+ ```
43
+
44
+ ## Testing
45
+
46
+ The GraphQL tests require a Shopify store and access token. You can set this by
47
+ copying `.env.template` to `.env.test` and adding the appropriate values.
48
+ VCR is used to record these requests.
49
+
50
+ ---
51
+
52
+ Made by [ScreenStaring](http://screenstaring.com)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "shopify/default_variant"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shopify
4
+ module DefaultVariant
5
+ VERSION = "0.0.1"
6
+ TITLE = "Default Title"
7
+
8
+ class Object
9
+ def initialize(object)
10
+ @object = object
11
+ end
12
+
13
+ def match?
14
+ # TODO: arity???
15
+ if @object.respond_to?(:variants)
16
+ variants = @object.variants
17
+ return variants.size == 1 && default_variant?(variants[0])
18
+ end
19
+
20
+ default_variant?(@object)
21
+ end
22
+
23
+ private
24
+
25
+ def default_variant?(object)
26
+ return false unless object.respond_to?(:title)
27
+ match = object.title == DefaultVariant::TITLE
28
+
29
+ return match unless object.respond_to?(:option1)
30
+
31
+ match &&= (object.option1.nil? || object.option1 == DefaultVariant::TITLE)
32
+ return match unless object.respond_to?(:option2)
33
+
34
+ match && (object.option2.nil? || object.option2 == DefaultVariant::TITLE)
35
+ end
36
+ end
37
+
38
+ class Hash
39
+ def initialize(hash)
40
+ @hash = hash
41
+ end
42
+
43
+ def match?
44
+ return false unless @hash.is_a?(::Hash)
45
+
46
+ plain_hash_match? || graphql_hash_match?
47
+ end
48
+
49
+ private
50
+
51
+ def plain_hash_match?
52
+ v = variants
53
+ return default_variant?(@hash) unless v
54
+
55
+ v.is_a?(Array) && v.size == 1 && v[0].is_a?(::Hash) && default_variant?(v[0])
56
+ end
57
+
58
+ def graphql_hash_match?
59
+ if @hash.include?(:hasOnlyDefaultVariant) || @hash.include?("hasOnlyDefaultVariant")
60
+ return @hash[:hasOnlyDefaultVariant] || @hash["hasOnlyDefaultVariant"]
61
+ end
62
+
63
+ v = variants
64
+ match = v.is_a?(::Hash) ? graphql_default_variant?(v) : false
65
+ return match unless match && (@hash.include?(:totalVariants) || @hash.include?("totalVariants"))
66
+
67
+ match && (@hash[:totalVariants] || @hash["totalVariants"]) == 1
68
+ end
69
+
70
+ def variants
71
+ @hash[:variants] || @hash["variants"]
72
+ end
73
+
74
+ def default_variant?(hash)
75
+ match = default_title?(hash)
76
+ # FIXME: should do like we do for GraphQL and not assume we have a title
77
+ return match unless match && (hash.include?(:option1) || hash.include?("option1"))
78
+
79
+ match &&= (hash[:option1] || hash["option1"]) == DefaultVariant::TITLE
80
+ return match unless match && (hash.include?(:option2) || hash.include?("option2"))
81
+
82
+ match && (hash[:option2] || hash["option2"]).nil?
83
+
84
+ # option3 too!
85
+ end
86
+
87
+ def graphql_default_variant?(variants)
88
+ edges = variants[:edges] || variants["edges"]
89
+ return false unless edges.is_a?(Array)
90
+
91
+ nodes = edges.map { |e| e[:node] || e["node"] }
92
+ return false unless nodes.size == 1 && nodes[0].is_a?(::Hash)
93
+
94
+ match = nil
95
+ if nodes[0].include?(:title) || nodes[0].include?("title")
96
+ match = default_title?(nodes[0])
97
+ end
98
+
99
+ return false if match == false
100
+
101
+ if nodes[0].include?(:selectedOptions) || nodes[0].include?("selectedOptions")
102
+ match = graphql_default_option?(nodes[0][:selectedOptions] || nodes[0]["selectedOptions"])
103
+ end
104
+
105
+ # Could still be nil so we force a boolean
106
+ !!match
107
+ end
108
+
109
+ def graphql_default_option?(options)
110
+ return false unless options.is_a?(Array) && options.size == 1
111
+
112
+ name = options[0][:name] || options[0]["name"]
113
+ value = options[0][:value] || options[0]["value"]
114
+
115
+ name == "Title" && value == DefaultVariant::TITLE
116
+ end
117
+
118
+ def default_title?(hash)
119
+ hash[:title] == DefaultVariant::TITLE || hash["title"] == DefaultVariant::TITLE
120
+ end
121
+ end
122
+
123
+ class << self
124
+ def match?(object)
125
+ handler(object).match?
126
+ end
127
+
128
+ private
129
+
130
+ def handler(object)
131
+ if object.is_a?(::Hash)
132
+ DefaultVariant::Hash.new(object)
133
+ else
134
+ DefaultVariant::Object.new(object)
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/shopify/default_variant"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "shopify-default_variant"
7
+ spec.version = Shopify::DefaultVariant::VERSION
8
+ spec.authors = ["sshaw"]
9
+ spec.email = ["skye.shaw@gmail.com"]
10
+
11
+ spec.summary = "Determine if the given Shopify product only has a default variant, or if the given variant is the default variant"
12
+ spec.description = "Determine if the given Shopify product only has a default variant, or if the given variant is the default variant. Works with ShopifyAPI, a GraphQL response hash with Symbol or String keys, POROs or plain Hashes"
13
+ spec.homepage = "https://github.com/ScreenStaring/shopify-default_variant"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler"
33
+ spec.add_development_dependency "rake", "~> 13.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "shopify_api-graphql-tiny", "~> 0.1.1"
36
+ spec.add_development_dependency "vcr"
37
+ spec.add_development_dependency "dotenv"
38
+ spec.add_development_dependency "webmock"
39
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopify-default_variant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sshaw
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shopify_api-graphql-tiny
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Determine if the given Shopify product only has a default variant, or
112
+ if the given variant is the default variant. Works with ShopifyAPI, a GraphQL response
113
+ hash with Symbol or String keys, POROs or plain Hashes
114
+ email:
115
+ - skye.shaw@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".env.template"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/shopify/default_variant.rb
128
+ - shopify-default_variant.gemspec
129
+ homepage: https://github.com/ScreenStaring/shopify-default_variant
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ homepage_uri: https://github.com/ScreenStaring/shopify-default_variant
134
+ source_code_uri: https://github.com/ScreenStaring/shopify-default_variant
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '2.4'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubygems_version: 3.3.26
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Determine if the given Shopify product only has a default variant, or if
154
+ the given variant is the default variant
155
+ test_files: []