class GlobalID

Constants

PATH_REGEXP

Attributes

app[R]
app[R]
model_id[R]
model_name[R]
uri[R]

Public Class Methods

app=(app) click to toggle source
# File lib/global_id/global_id.rb, line 27
def app=(app)
  @app = validate_app(app)
end
create(model, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 11
def create(model, options = {})
  app = options.fetch :app, GlobalID.app
  raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
  new URI("gid://#{app}/#{model.class.name}/#{model.id}"), options
end
find(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 17
def find(gid, options = {})
  parse(gid, options).try(:find, options)
end
new(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 52
def initialize(gid, options = {})
  extract_uri_components gid
end
parse(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 21
def parse(gid, options = {})
  gid.is_a?(self) ? gid : new(gid, options)
rescue URI::Error
  parse_encoded_gid(gid, options)
end
validate_app(app) click to toggle source
# File lib/global_id/global_id.rb, line 31
def validate_app(app)
  URI.parse('gid:///').hostname = app
rescue URI::InvalidComponentError
  raise ArgumentError, 'Invalid app name. ' \
    'App names must be valid URI hostnames: alphanumeric and hyphen characters only.'
end

Private Class Methods

parse_encoded_gid(gid, options) click to toggle source
# File lib/global_id/global_id.rb, line 39
def parse_encoded_gid(gid, options)
  new(Base64.urlsafe_decode64(repad_gid(gid)), options) rescue nil
end
repad_gid(gid) click to toggle source

We removed the base64 padding character = during to_param, now we’re adding it back so decoding will work

# File lib/global_id/global_id.rb, line 44
def repad_gid(gid)
  padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
  gid + ('=' * padding_chars)
end

Public Instance Methods

==(other) click to toggle source
# File lib/global_id/global_id.rb, line 64
def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end
find(options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 56
def find(options = {})
  Locator.locate self, options
end
model_class() click to toggle source
# File lib/global_id/global_id.rb, line 60
def model_class
  model_name.constantize
end
to_param() click to toggle source
# File lib/global_id/global_id.rb, line 72
def to_param
  # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
  Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
end
to_s() click to toggle source
# File lib/global_id/global_id.rb, line 68
def to_s
  @uri.to_s
end

Private Instance Methods

extract_uri_components(gid) click to toggle source

Pending a URI::GID to handle validation

# File lib/global_id/global_id.rb, line 81
def extract_uri_components(gid)
  @uri = gid.is_a?(URI) ? gid : URI.parse(gid)
  raise URI::BadURIError, "Not a gid:// URI scheme: #{@uri.inspect}" unless @uri.scheme == 'gid'

  if @uri.path =~ PATH_REGEXP
    @app = @uri.host
    @model_name = $1
    @model_id = $2
  else
    raise URI::InvalidURIError, "Expected a URI like gid://app/Person/1234: #{@uri.inspect}"
  end
end