-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b9c13d6
Showing
26 changed files
with
7,897 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# To run tests: | ||
# | ||
# 1. Copy this file to .env | ||
# 2. Fill-in the below variables with valid values. | ||
# These values will be used by the tests. Data will be written to SHOPIFY_DOMAIN | ||
# | ||
SHOPIFY_DOMAIN= | ||
SHOPIFY_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: CI | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
ruby: ['3.2', '3.1', '3.0', '2.7', '2.6', '2.5', '2.4' ] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
bundler-cache: true | ||
|
||
- run: bundle exec rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/ruby | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=ruby | ||
|
||
### Ruby ### | ||
*.gem | ||
*.rbc | ||
/.config | ||
/coverage/ | ||
/InstalledFiles | ||
/pkg/ | ||
/spec/reports/ | ||
/spec/examples.txt | ||
/test/tmp/ | ||
/test/version_tmp/ | ||
/tmp/ | ||
|
||
# Used by dotenv library to load environment variables. | ||
.env* | ||
!.env.template | ||
|
||
# Ignore Byebug command history file. | ||
.byebug_history | ||
|
||
## Specific to RubyMotion: | ||
.dat* | ||
.repl_history | ||
build/ | ||
*.bridgesupport | ||
build-iPhoneOS/ | ||
build-iPhoneSimulator/ | ||
|
||
## Specific to RubyMotion (use of CocoaPods): | ||
# | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# vendor/Pods/ | ||
|
||
## Documentation cache and generated files: | ||
/.yardoc/ | ||
/_yardoc/ | ||
/doc/ | ||
/rdoc/ | ||
|
||
## Environment normalization: | ||
/.bundle/ | ||
/vendor/bundle | ||
/lib/bundler/man/ | ||
|
||
# for a library or gem, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# Gemfile.lock | ||
# .ruby-version | ||
# .ruby-gemset | ||
|
||
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: | ||
.rvmrc | ||
|
||
# Used by RuboCop. Remote config files pulled in from inherit_from directive. | ||
# .rubocop-https?--* | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/ruby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in shopify-default_variant.gemspec | ||
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
|
||
gem "rspec", "~> 3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 sshaw | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Shopify::DefaultVariant | ||
|
||
Determine if the given Shopify product only has a default variant, or if the given variant is the default variant. | ||
|
||
Works with a variety of objects: | ||
|
||
- ShopifyAPI gem response objects | ||
- GraphQL response `Hash` with `String` or `Symbol` keys | ||
- Plain Ol' Ruby Object (PORO) | ||
- Plain `Hash` with `String` or `Symbol` keys | ||
|
||
## Usage | ||
|
||
```rb | ||
Shopify::DefaultVariant.match?(object) | ||
``` | ||
|
||
Where `object` can be a product or a variant in any of the aforementioned forms. | ||
|
||
More examples: | ||
|
||
```rb | ||
require "shopify_api" | ||
|
||
product = ShopifyAPI::Product.find(id: id) | ||
Shopify::DefaultVariant.match?(product) | ||
Shopify::DefaultVariant.match?(product.variants) | ||
Shopify::DefaultVariant.match?(product.variants.sample) | ||
|
||
require "shopify_api-graphql-tiny" | ||
|
||
gql = ShopifyAPI::GraphQL::Tiny.new("shop", "token") | ||
|
||
data = gql.execute(YOUR_QUERY) | ||
# Be sure to remove the data key and query's name key (whatever it may be) | ||
Shopify::DefaultVariant.match?(data.dig("data", "queryName")) | ||
|
||
# Or use a Object or Hash that resembles one of the well-known forms | ||
Shopify::DefaultVariant.match?(my_custom_object) | ||
``` | ||
|
||
## Testing | ||
|
||
The GraphQL tests require a Shopify store and access token. You can set this by | ||
copying `.env.template` to `.env.test` and adding the appropriate values. | ||
VCR is used to record these requests. | ||
|
||
--- | ||
|
||
Made by [ScreenStaring](http://screenstaring.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "shopify/default_variant" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
# (If you use this, don't forget to add pry to your Gemfile!) | ||
# require "pry" | ||
# Pry.start | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shopify | ||
module DefaultVariant | ||
VERSION = "0.0.1" | ||
TITLE = "Default Title" | ||
|
||
class Object | ||
def initialize(object) | ||
@object = object | ||
end | ||
|
||
def match? | ||
# TODO: arity??? | ||
if @object.respond_to?(:variants) | ||
variants = @object.variants | ||
return variants.size == 1 && default_variant?(variants[0]) | ||
end | ||
|
||
default_variant?(@object) | ||
end | ||
|
||
private | ||
|
||
def default_variant?(object) | ||
return false unless object.respond_to?(:title) | ||
match = object.title == DefaultVariant::TITLE | ||
|
||
return match unless object.respond_to?(:option1) | ||
|
||
match &&= (object.option1.nil? || object.option1 == DefaultVariant::TITLE) | ||
return match unless object.respond_to?(:option2) | ||
|
||
match && (object.option2.nil? || object.option2 == DefaultVariant::TITLE) | ||
end | ||
end | ||
|
||
class Hash | ||
def initialize(hash) | ||
@hash = hash | ||
end | ||
|
||
def match? | ||
return false unless @hash.is_a?(::Hash) | ||
|
||
plain_hash_match? || graphql_hash_match? | ||
end | ||
|
||
private | ||
|
||
def plain_hash_match? | ||
v = variants | ||
return default_variant?(@hash) unless v | ||
|
||
v.is_a?(Array) && v.size == 1 && v[0].is_a?(::Hash) && default_variant?(v[0]) | ||
end | ||
|
||
def graphql_hash_match? | ||
if @hash.include?(:hasOnlyDefaultVariant) || @hash.include?("hasOnlyDefaultVariant") | ||
return @hash[:hasOnlyDefaultVariant] || @hash["hasOnlyDefaultVariant"] | ||
end | ||
|
||
v = variants | ||
match = v.is_a?(::Hash) ? graphql_default_variant?(v) : false | ||
return match unless match && (@hash.include?(:totalVariants) || @hash.include?("totalVariants")) | ||
|
||
match && (@hash[:totalVariants] || @hash["totalVariants"]) == 1 | ||
end | ||
|
||
def variants | ||
@hash[:variants] || @hash["variants"] | ||
end | ||
|
||
def default_variant?(hash) | ||
match = default_title?(hash) | ||
# FIXME: should do like we do for GraphQL and not assume we have a title | ||
return match unless match && (hash.include?(:option1) || hash.include?("option1")) | ||
|
||
match &&= (hash[:option1] || hash["option1"]) == DefaultVariant::TITLE | ||
return match unless match && (hash.include?(:option2) || hash.include?("option2")) | ||
|
||
match && (hash[:option2] || hash["option2"]).nil? | ||
|
||
# option3 too! | ||
end | ||
|
||
def graphql_default_variant?(variants) | ||
edges = variants[:edges] || variants["edges"] | ||
return false unless edges.is_a?(Array) | ||
|
||
nodes = edges.map { |e| e[:node] || e["node"] } | ||
return false unless nodes.size == 1 && nodes[0].is_a?(::Hash) | ||
|
||
match = nil | ||
if nodes[0].include?(:title) || nodes[0].include?("title") | ||
match = default_title?(nodes[0]) | ||
end | ||
|
||
return false if match == false | ||
|
||
if nodes[0].include?(:selectedOptions) || nodes[0].include?("selectedOptions") | ||
match = graphql_default_option?(nodes[0][:selectedOptions] || nodes[0]["selectedOptions"]) | ||
end | ||
|
||
# Could still be nil so we force a boolean | ||
!!match | ||
end | ||
|
||
def graphql_default_option?(options) | ||
return false unless options.is_a?(Array) && options.size == 1 | ||
|
||
name = options[0][:name] || options[0]["name"] | ||
value = options[0][:value] || options[0]["value"] | ||
|
||
name == "Title" && value == DefaultVariant::TITLE | ||
end | ||
|
||
def default_title?(hash) | ||
hash[:title] == DefaultVariant::TITLE || hash["title"] == DefaultVariant::TITLE | ||
end | ||
end | ||
|
||
class << self | ||
def match?(object) | ||
handler(object).match? | ||
end | ||
|
||
private | ||
|
||
def handler(object) | ||
if object.is_a?(::Hash) | ||
DefaultVariant::Hash.new(object) | ||
else | ||
DefaultVariant::Object.new(object) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "lib/shopify/default_variant" | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "shopify-default_variant" | ||
spec.version = Shopify::DefaultVariant::VERSION | ||
spec.authors = ["sshaw"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = "Determine if the given Shopify product only has a default variant, or if the given variant is the default variant" | ||
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" | ||
spec.homepage = "https://github.com/ScreenStaring/shopify-default_variant" | ||
spec.license = "MIT" | ||
spec.required_ruby_version = ">= 2.4" | ||
|
||
spec.metadata["homepage_uri"] = spec.homepage | ||
spec.metadata["source_code_uri"] = spec.homepage | ||
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." | ||
|
||
# Specify which files should be added to the gem when it is released. | ||
# The `git ls-files -z` loads the files in the RubyGem that have been added into git. | ||
spec.files = Dir.chdir(File.expand_path(__dir__)) do | ||
`git ls-files -z`.split("\x0").reject do |f| | ||
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) | ||
end | ||
end | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler" | ||
spec.add_development_dependency "rake", "~> 13.0" | ||
spec.add_development_dependency "rspec", "~> 3.0" | ||
spec.add_development_dependency "shopify_api-graphql-tiny", "~> 0.1.1" | ||
spec.add_development_dependency "vcr" | ||
spec.add_development_dependency "dotenv" | ||
spec.add_development_dependency "webmock" | ||
end |
Oops, something went wrong.