Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaw committed Mar 18, 2020
0 parents commit d08b2e2
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
Gemfile.lock

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
sudo: false
language: ruby
cache: bundler
rvm:
- 2.2
- 2.3
- 2.4
- 2.5
- 2.6
- 2.7

# https://docs.travis-ci.com/user/languages/ruby/#bundler-20
before_install:
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
- gem install bundler -v '< 2'

notifications:
email: false
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in shopify_url.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 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.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Shopify URL

[![Build Status](https://travis-ci.org/ScreenStaring/shopify_url.svg?branch=master)](https://travis-ci.org/ScreenStaring/shopify_url)

Build URLs to Shopify web pages.

## Usage

### Admin URLs

```rb
require "shopify_url"

url = ShopifyURL::Admin.new("shopname")

# https://shopname.myshopify.com/admin"
url.to_s

# https://shopname.myshopify.com/admin/orders
url.orders

# https://shopname.myshopify.com/admin/orders/6303508996
url.order(6303508996)

# https://shopname.myshopify.com/admin/products
url.products

# https://shopname.myshopify.com/admin/products/345323423
url.product(345323423)

# https://shopname.myshopify.com/admin/products/345323423/variants/2421342331
url.product(345323423).variant(2421342331)
```

See the RDocs for a complete list of methods.

### Store URLs
```rb
require "shopify_url"

url = ShopifyURL::Store.new("shopname") # you can also use your top-level domain

# https://shopname.myshopify.com/collections
url.collections

# https://shopname.myshopify.com/collections/amaaaaazing-thangz
url.collection("amaaaaazing-thangz")
```

See the RDocs for a complete list of methods.

### Query String Parameters

All Shopify URL generation methods accept a `Hash` of query string parameters:

```rb
url = ShopifyURL::Store.new("shopname") # you can also use your top-level domain

# https://shopname.myshopify.com/products/some-handle?tracking=data
url.products("some-handle", :tracking => "data")
```

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

---

Made by [ScreenStaring](http:https://screenstaring.com)
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
234 changes: 234 additions & 0 deletions lib/shopify_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
# frozen_string_literal: true
require "uri"

module ShopifyURL
VERSION = "0.0.1"
TOP_LEVEL_DOMAIN = ".myshopify.com"

class Linker
def initialize(shop)
raise ArgumentError, "shop required" if empty?(shop)

@host = shop.dup
@host.prepend("https://") unless @host =~ %r{\Ahttps?:https://}i
end

def to_s
@host.dup
end

alias to_str to_s
alias inspect to_s

protected

attr_reader :host

def id_required!(id)
# Add caller to remove this frame from stack
raise ArgumentError, "id required", caller if id.to_s.strip.empty?
end

def empty?(s)
s.to_s.strip.empty?
end

def q(params)
return unless params && params.respond_to?(:to_a)
URI.encode_www_form(params.to_a)
end
end

class Store < Linker
class Collections < String
def initialize(host, id = nil, qs = nil)
super host + "/collections"
self << "/" << id if id
self << "?" << qs if qs
end
end

class Products < String
def initialize(host, qs = nil)
super host + "/products"
self << "?" << qs if qs
end
end

class Product < String
def initialize(host, id, qs = nil)
super host + "/products/" << id.to_s
self << "?" << qs if qs
end
end

class Blogs < String
def initialize(host, category, qs = nil)
super host + "/blogs"
self << "/" << category
self << "?" << qs if qs
end

def entry(name, params = nil)
self << "/" << name
self << "?" << URI.encode_www_form(params.to_a) if params && params.any?
self
end
end

def initialize(shop)
super

host << TOP_LEVEL_DOMAIN unless @host =~ %r{\.[-0-9a-z]+\z} # non-ASCII domains!
host.freeze
end

def collection(id, query = nil)
id_required!(id)
Collections.new(host, id, q(query))
end

def collections(query = nil)
Collections.new(host, nil, q(query))
end

def product(id, query = nil)
id_required!(id)
Product.new(host, id, q(query))
end

def products(query = nil)
Products.new(host, q(query))
end

def page(id, query = nil)
url = host + "/pages/" << id.to_s
url << "?" << q(query) if query
url
end

def blogs(category, query = nil)
raise "category required" if empty?(category)
Blogs.new(host, category, q(query))
end
end

class Admin < Linker
class Customers < String
def initialize(host, id = nil, qs = nil)
super host + "/customers"
self << "/" << id.to_s if id
self << "?" << qs if qs
end
end

class Products < Store::Products
def inventory
self + "/inventory"
end
end

class Product < Store::Product
def variants(id, params = nil)
url = self + "/variants/" << id.to_s
url << "?" << URI.encode_www_form(params.to_a) if params && params.any?
url
end
end

class Order < String
def initialize(host, id, qs = nil)
super host + "/orders/" << id.to_s
self << "?" << qs if qs
end

def shipping_labels
ShippingLabel.new(self + "/shipping_labels")
end
end

class Orders < String
def initialize(host, qs = nil)
super host + "/orders"
self << "?" << qs if qs
end
end

class ShippingLabel < String
def new(params = nil)
url = self + "/new"
url << "?" << URI.encode_www_form(params.to_a) if params && params.any?
url
end
end

class DraftOrder < String
def initialize(base, id, qs = nil)
super base + "/draft_orders/" << id.to_s
self << "?" << qs if qs
end

def duplicate(params = nil)
url = self + "/duplicate"
url << "?" << URI.encode_www_form(params.to_a) if params && params.any?
url
end
end

class DraftOrders < String
def initialize(base, qs = nil)
super base + "/draft_orders"
self << "?" << qs if qs
end

def new(params = nil)
url = self + "/new"
url << "?" << URI.encode_www_form(params.to_a) if params && params.any?
url
end
end

def initialize(shop)
super
host << TOP_LEVEL_DOMAIN unless host.end_with?(TOP_LEVEL_DOMAIN)
host << "/admin"
host.freeze
end

def customer(id, query = nil)
id_required!(id)
Customers.new(host, id, q(query))
end

def customers(query = nil)
Customers.new(host, nil, q(query))
end

def draft_orders(query = nil)
DraftOrders.new(host, q(query))
end

def draft_order(id, query = nil)
id_required!(id)
DraftOrder.new(host, id, q(query))
end

def order(id, query = nil)
id_required!(id)
Order.new(host, id, q(query))
end

def orders(query = nil)
Orders.new(host, q(query))
end

def product(id, query = nil)
id_required!(id)
Product.new(host, id, q(query))
end

def products(query = nil)
Products.new(host, q(query))
end
end
end
Loading

0 comments on commit d08b2e2

Please sign in to comment.