tiny_gid 0.0.1 → 0.0.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Changes +8 -0
  3. data/README.md +17 -1
  4. data/lib/tiny_gid.rb +25 -15
  5. metadata +7 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a33939d63b7efe92195e8ff5065083e03061920f0bcafc0b853c43d371c5bf6
4
- data.tar.gz: a6245c347ca86b527a67480c888939d57a4070e64d4668c540cf5c09ad971a0f
3
+ metadata.gz: 90c32039e2d334d95e380dfcb236d92835f8f47206c32a7a6c618a92043bb5dd
4
+ data.tar.gz: 1179c6460da059639cada21299af8aeec2363ec0e8baf66b9ba1b8a82521cba4
5
5
  SHA512:
6
- metadata.gz: 7d57726cc44177123d78a1f7be65ecbcd5dc941c2a58c3238c889a49e8b388c2800fe734011d6c5e3f05f0b99c367f86e664ffaa1b8e54196528697c06a83254
7
- data.tar.gz: 2bb181b90609740663d80cf2a30fc5c3ed80e542ce50d9322272823122cdb86e57d123ea17021b3fbcf3df5a2245621e825d9a8358da86a88b41340a68b7ba25
6
+ metadata.gz: 2b2c322cd031586cd4d6aa7b82bcfc876d834b2b7301f3cd077890f28e245c274c20b5b2b5479030d7ddaeee29e8da1d96b2549b45035ed109f0c219d10f0654
7
+ data.tar.gz: e64c77cf0ce216c784bbaaf01768832e75763067e8824ebeb506610df3c46df99087d5bc077a6dae9634667dbee0fff09f2759946a196371f47f3deafadf6e3c
data/Changes ADDED
@@ -0,0 +1,8 @@
1
+ v0.0.3 2025-12-25
2
+ --------------------
3
+ Add TinyGID.parse
4
+ Add TinyGID.to_sc
5
+
6
+ v0.0.2 2025-12-11
7
+ --------------------
8
+ Make gid.app block form thread-safe
data/README.md CHANGED
@@ -53,6 +53,22 @@ gid = TinyGID.new("shopify")
53
53
  gid.Product(123)
54
54
  ```
55
55
 
56
+ ### Parsing
57
+
58
+ ```rb
59
+ app, model, id = gid.parse("gid://shopify/Product/123")
60
+ app, model, id, params = gid.parse("gid://shopify/Product/123?foo=bar")
61
+ ```
62
+
63
+ `params` is a `Hash`
64
+
65
+ If you just want the scalar ID:
66
+
67
+ ```rb
68
+ id = gid.to_sc("gid://shopify/Product/123") # "123"
69
+ id = gid.to_sc("gid://shopify/Product/123?foo=bar") # "123"
70
+ ```
71
+
56
72
  ## Why Not Use GlobalID‽
57
73
 
58
74
  [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
@@ -69,7 +85,7 @@ Don't yah think?
69
85
 
70
86
  Using application/x-www-form-urlencoded encoding to match GlobalID. Should probably support URL encoding too.
71
87
 
72
- To put that in 21st century speak: spaces with be replaced with `+` not `%20`.
88
+ To put that in 21st century speak: spaces will be replaced with `+` not `%20`.
73
89
 
74
90
  ## Signed Global IDs
75
91
 
data/lib/tiny_gid.rb CHANGED
@@ -3,8 +3,9 @@
3
3
  require "uri"
4
4
 
5
5
  module TinyGID
6
- VERSION = "0.0.1"
6
+ VERSION = "0.0.3"
7
7
  FORMAT = "gid://%s/%s/%s"
8
+ REGEX = %r{\Agid://([^/]+)/([^/]+)/([^/?]+)(?:\?(.*?))?\z}
8
9
 
9
10
  module MethodMissing # :nodoc:
10
11
  def method_missing(name, *arguments, &block)
@@ -17,7 +18,7 @@ module TinyGID
17
18
  raise TypeError, "gid::#{name} params must be a Hash. Received: #{params.class}" unless params.is_a?(Hash)
18
19
 
19
20
  app = params.delete(:__app__) || TinyGID.app
20
- raise "gid::#{name} cannot be generated: missing app" unless app
21
+ raise "gid::#{name} cannot be generated: missing app name" unless app
21
22
 
22
23
  gid = sprintf(FORMAT, app, name, URI.encode_www_form_component(id))
23
24
  return gid unless params.any?
@@ -37,15 +38,13 @@ module TinyGID
37
38
 
38
39
  def app(name = nil)
39
40
  if block_given?
40
- raise ArgumentError, "block provided without a app to scope to" unless name
41
+ raise ArgumentError, "block provided without an app name to scope to" unless name
41
42
 
42
43
  begin
43
- og = @app
44
- @app = name
45
-
44
+ Thread.current[:__tiny_gid_app__] = name
46
45
  yield self
47
46
  ensure
48
- @app = og
47
+ Thread.current[:__tiny_gid_app__] = nil
49
48
  end
50
49
 
51
50
  return
@@ -54,16 +53,27 @@ module TinyGID
54
53
  # No block but given a name, just set it :|
55
54
  @app = name if name
56
55
 
57
- return @app if @app
58
- return Rails.application.name if defined?(Rails) && Rails.respond_to?(:application)
56
+ case
57
+ when Thread.current[:__tiny_gid_app__]
58
+ Thread.current[:__tiny_gid_app__]
59
+ when @app
60
+ @app
61
+ when defined?(Rails) && Rails.respond_to?(:application)
62
+ Rails.application.name
63
+ end
64
+ end
65
+
66
+ def parse(gid)
67
+ raise ArgumentError, "'#{gid}' is not a valid global id" unless gid =~ REGEX
68
+
69
+ parsed = [$1, $2, $3]
70
+ parsed << Hash[URI.decode_www_form($4)] if $4 && !$4.empty?
71
+ parsed
59
72
  end
60
73
 
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
74
+ def to_sc(gid)
75
+ parse(gid)[2]
76
+ end
67
77
  end
68
78
 
69
79
  def initialize(app)
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_gid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skye Shaw
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 2025-12-11 00:00:00.000000000 Z
11
+ date: 2025-12-26 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: rspec
@@ -59,6 +60,7 @@ executables: []
59
60
  extensions: []
60
61
  extra_rdoc_files: []
61
62
  files:
63
+ - Changes
62
64
  - LICENSE.txt
63
65
  - README.md
64
66
  - Rakefile
@@ -71,6 +73,7 @@ licenses:
71
73
  metadata:
72
74
  homepage_uri: https://github.com/sshaw/tiny_gid
73
75
  source_code_uri: https://github.com/sshaw/tiny_gid
76
+ post_install_message:
74
77
  rdoc_options: []
75
78
  require_paths:
76
79
  - lib
@@ -85,7 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
90
  requirements: []
88
- rubygems_version: 3.6.2
91
+ rubygems_version: 3.5.16
92
+ signing_key:
89
93
  specification_version: 4
90
94
  summary: Tiny class to build Global ID (gid://) strings from scalar values
91
95
  test_files: []