tiny_gid 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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +8 -0
- data/lib/gid.rb +7 -0
- data/lib/tiny_gid/gid.rb +5 -0
- data/lib/tiny_gid.rb +82 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3a33939d63b7efe92195e8ff5065083e03061920f0bcafc0b853c43d371c5bf6
|
|
4
|
+
data.tar.gz: a6245c347ca86b527a67480c888939d57a4070e64d4668c540cf5c09ad971a0f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7d57726cc44177123d78a1f7be65ecbcd5dc941c2a58c3238c889a49e8b388c2800fe734011d6c5e3f05f0b99c367f86e664ffaa1b8e54196528697c06a83254
|
|
7
|
+
data.tar.gz: 2bb181b90609740663d80cf2a30fc5c3ed80e542ce50d9322272823122cdb86e57d123ea17021b3fbcf3df5a2245621e825d9a8358da86a88b41340a68b7ba25
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 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,84 @@
|
|
|
1
|
+
# TinyGID
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Tiny class to build Global ID (gid://) strings from scalar values.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
`gem install tiny_gid` or for Bundler: `gem "tiny_gid", :require => "gid"`
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Setting an app name is required. If Rails is installed `Rails.application.name` is used.
|
|
13
|
+
|
|
14
|
+
```rb
|
|
15
|
+
require "gid"
|
|
16
|
+
|
|
17
|
+
gid.app = "shopify"
|
|
18
|
+
|
|
19
|
+
gid::Product(123) # "gid://shopify/Product/123"
|
|
20
|
+
gid::ProductVariant(123) # "gid://shopify/ProductVariant/123"
|
|
21
|
+
gid::InventoryLevel(123, :inventory_item_id => 456) # "gid://shopify/InventoryLevel/123?inventory_item_id=456"
|
|
22
|
+
|
|
23
|
+
# Use something besides gid.app for the duration of the block:
|
|
24
|
+
gid.app("something-amaaaazing") do
|
|
25
|
+
gid::User(1) # "gid://something-amaaaazing/User/1"
|
|
26
|
+
gid::Post(99) # "gid://something-amaaaazing/User/99"
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This will also import the `GID` class:
|
|
31
|
+
```rb
|
|
32
|
+
gid = GID.new("shopify")
|
|
33
|
+
gid::Product(123) # "gid://shopify/Product/123"
|
|
34
|
+
|
|
35
|
+
# You don't have to use :: of course ;)
|
|
36
|
+
gid.Product(123) # "gid://shopify/Product/123"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If you don't want the (pesky?) `gid` method you can require: `tiny_gid/gid` which gives you `GID` only.
|
|
40
|
+
|
|
41
|
+
If `GID` creates a conflict in the top-level namespace use `TinyGID`:
|
|
42
|
+
```rb
|
|
43
|
+
require "tiny_gid"
|
|
44
|
+
|
|
45
|
+
TinyGID.app = "shopify"
|
|
46
|
+
TinyGID::Product(123) # "gid://shopify/Product/123"
|
|
47
|
+
|
|
48
|
+
TinyGID.app("something-amaaaazing") do |gid|
|
|
49
|
+
gid::User(1) # "gid://something-amaaaazing/User/1"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
gid = TinyGID.new("shopify")
|
|
53
|
+
gid.Product(123)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Why Not Use GlobalID‽
|
|
57
|
+
|
|
58
|
+
[GlobalID](https://github.com/rails/globalid) is nice but it primarily deals with IDs backed by an instance of a class, e.g. Rails model, whereas
|
|
59
|
+
TinyGID is for scalars.
|
|
60
|
+
|
|
61
|
+
GlobalID does indeed have version that can be used with scalars but it's a bit verbose and not good for developer productivity:
|
|
62
|
+
```rb
|
|
63
|
+
URI::GID.build(:app => "foo", :model_name => "User", :model_id => "123", :params => { :foo => "bar" })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Don't yah think?
|
|
67
|
+
|
|
68
|
+
## ID Encoding/Escaping
|
|
69
|
+
|
|
70
|
+
Using application/x-www-form-urlencoded encoding to match GlobalID. Should probably support URL encoding too.
|
|
71
|
+
|
|
72
|
+
To put that in 21st century speak: spaces with be replaced with `+` not `%20`.
|
|
73
|
+
|
|
74
|
+
## Signed Global IDs
|
|
75
|
+
|
|
76
|
+
No support. Use GlobalID :)
|
|
77
|
+
|
|
78
|
+
## Author
|
|
79
|
+
|
|
80
|
+
Skye Shaw [skye.shaw -AT- gmail.com]
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
Released under the MIT License: http://www.opensource.org/licenses/MIT
|
data/Rakefile
ADDED
data/lib/gid.rb
ADDED
data/lib/tiny_gid/gid.rb
ADDED
data/lib/tiny_gid.rb
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module TinyGID
|
|
6
|
+
VERSION = "0.0.1"
|
|
7
|
+
FORMAT = "gid://%s/%s/%s"
|
|
8
|
+
|
|
9
|
+
module MethodMissing # :nodoc:
|
|
10
|
+
def method_missing(name, *arguments, &block)
|
|
11
|
+
# TODO: need to ensure we're not calling ourselves
|
|
12
|
+
|
|
13
|
+
id = arguments.shift
|
|
14
|
+
raise ArgumentError, "gid::#{name} requires an ID" unless id
|
|
15
|
+
|
|
16
|
+
params = (arguments[0] || {}).dup
|
|
17
|
+
raise TypeError, "gid::#{name} params must be a Hash. Received: #{params.class}" unless params.is_a?(Hash)
|
|
18
|
+
|
|
19
|
+
app = params.delete(:__app__) || TinyGID.app
|
|
20
|
+
raise "gid::#{name} cannot be generated: missing app" unless app
|
|
21
|
+
|
|
22
|
+
gid = sprintf(FORMAT, app, name, URI.encode_www_form_component(id))
|
|
23
|
+
return gid unless params.any?
|
|
24
|
+
|
|
25
|
+
gid << "?" << URI.encode_www_form(params)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
include MethodMissing # 🤸♀️
|
|
30
|
+
extend MethodMissing # 🤸♂️
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
# In GlobalID app names must be valid URI hostnames: alphanumeric and hyphen characters only
|
|
34
|
+
def app=(name)
|
|
35
|
+
@app = name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def app(name = nil)
|
|
39
|
+
if block_given?
|
|
40
|
+
raise ArgumentError, "block provided without a app to scope to" unless name
|
|
41
|
+
|
|
42
|
+
begin
|
|
43
|
+
og = @app
|
|
44
|
+
@app = name
|
|
45
|
+
|
|
46
|
+
yield self
|
|
47
|
+
ensure
|
|
48
|
+
@app = og
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
return
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# No block but given a name, just set it :|
|
|
55
|
+
@app = name if name
|
|
56
|
+
|
|
57
|
+
return @app if @app
|
|
58
|
+
return Rails.application.name if defined?(Rails) && Rails.respond_to?(:application)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Necessary?
|
|
62
|
+
# def to_sc(gid)
|
|
63
|
+
# # TODO:
|
|
64
|
+
# # value, params = TinyGID.to_sc("gid://shopify/Product/123?is_a=headache")
|
|
65
|
+
# gid.to_s.split("/")[-1]
|
|
66
|
+
# end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def initialize(app)
|
|
70
|
+
raise ArgumentError "app required" if app.to_s.strip.empty?
|
|
71
|
+
@app = app
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def method_missing(name, *arguments, &block)
|
|
75
|
+
id = arguments.shift
|
|
76
|
+
|
|
77
|
+
options = arguments.shift || {}
|
|
78
|
+
options = options.merge(:__app__ => @app) unless options.include?(:app)
|
|
79
|
+
|
|
80
|
+
super(name, id, options, &block)
|
|
81
|
+
end
|
|
82
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tiny_gid
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Skye Shaw
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2025-12-11 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rspec
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: bundler
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
description: TinyGID provides a compact syntax for building Global ID strings for
|
|
55
|
+
things like GraphQL APIs and Rails apps
|
|
56
|
+
email:
|
|
57
|
+
- skye.shaw@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- LICENSE.txt
|
|
63
|
+
- README.md
|
|
64
|
+
- Rakefile
|
|
65
|
+
- lib/gid.rb
|
|
66
|
+
- lib/tiny_gid.rb
|
|
67
|
+
- lib/tiny_gid/gid.rb
|
|
68
|
+
homepage: https://github.com/sshaw/tiny_gid
|
|
69
|
+
licenses:
|
|
70
|
+
- MIT
|
|
71
|
+
metadata:
|
|
72
|
+
homepage_uri: https://github.com/sshaw/tiny_gid
|
|
73
|
+
source_code_uri: https://github.com/sshaw/tiny_gid
|
|
74
|
+
rdoc_options: []
|
|
75
|
+
require_paths:
|
|
76
|
+
- lib
|
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - ">="
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '0'
|
|
87
|
+
requirements: []
|
|
88
|
+
rubygems_version: 3.6.2
|
|
89
|
+
specification_version: 4
|
|
90
|
+
summary: Tiny class to build Global ID (gid://) strings from scalar values
|
|
91
|
+
test_files: []
|