Class: URI::GID

Inherits:
Generic
  • Object
show all
Defined in:
lib/global_id/uri/gid.rb

Defined Under Namespace

Classes: InvalidModelIdError, MissingModelIdError

Constant Summary collapse

COMPOSITE_MODEL_ID_MAX_SIZE =

Maximum size of a model id segment

20
COMPOSITE_MODEL_ID_DELIMITER =
"/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_idObject (readonly)

Returns the value of attribute model_id.



29
30
31
# File 'lib/global_id/uri/gid.rb', line 29

def model_id
  @model_id
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



29
30
31
# File 'lib/global_id/uri/gid.rb', line 29

def model_name
  @model_name
end

#paramsObject (readonly)

Returns the value of attribute params.



29
30
31
# File 'lib/global_id/uri/gid.rb', line 29

def params
  @params
end

Class Method Details

.build(args) ⇒ Object

Create a new URI::GID from components with argument check.

The allowed components are app, model_name, model_id and params, which can be either a hash or an array.

Using a hash:

URI::GID.build(app: 'bcx', model_name: 'Person', model_id: '1', params: { key: 'value' })

Using an array, the arguments must be in order [app, model_name, model_id, params]:

URI::GID.build(['bcx', 'Person', '1', key: 'value'])


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/global_id/uri/gid.rb', line 88

def build(args)
  parts = Util.make_components_hash(self, args)
  parts[:host] = parts[:app]
  model_id_segment = Array(parts[:model_id]).map { |p| CGI.escape(p.to_s) }.join(COMPOSITE_MODEL_ID_DELIMITER)
  parts[:path] = "/#{parts[:model_name]}/#{model_id_segment}"

  if parts[:params] && !parts[:params].empty?
    parts[:query] = URI.encode_www_form(parts[:params])
  end

  super parts
end

.create(app, model, params = nil) ⇒ Object

Shorthand to build a URI::GID from an app, a model and optional params.

URI::GID.create('bcx', Person.find(5), database: 'superhumans')


72
73
74
# File 'lib/global_id/uri/gid.rb', line 72

def create(app, model, params = nil)
  build app: app, model_name: model.class.name, model_id: model.id, params: params
end

.parse(uri) ⇒ Object

Create a new URI::GID by parsing a gid string with argument check.

URI::GID.parse 'gid://bcx/Person/1?key=value'

This differs from URI() and URI.parse which do not check arguments.

URI('gid://bcx')             # => URI::GID instance
URI.parse('gid://bcx')       # => URI::GID instance
URI::GID.parse('gid://bcx/') # => raises URI::InvalidComponentError


64
65
66
67
# File 'lib/global_id/uri/gid.rb', line 64

def parse(uri)
  generic_components = URI.split(uri) << nil << true # nil parser, true arg_check
  new(*generic_components)
end

.validate_app(app) ⇒ Object

Validates app‘s as URI hostnames containing only alphanumeric characters and hyphens. An ArgumentError is raised if app is invalid.

URI::GID.validate_app('bcx')     # => 'bcx'
URI::GID.validate_app('foo-bar') # => 'foo-bar'

URI::GID.validate_app(nil)       # => ArgumentError
URI::GID.validate_app('foo/bar') # => ArgumentError


48
49
50
51
52
53
# File 'lib/global_id/uri/gid.rb', line 48

def validate_app(app)
  parse("gid://#{app}/Model/1").app
rescue URI::Error
  raise ArgumentError, 'Invalid app name. ' \
    'App names must be valid URI hostnames: alphanumeric and hyphen characters only.'
end

Instance Method Details

#deconstruct_keys(_keys) ⇒ Object



107
108
109
# File 'lib/global_id/uri/gid.rb', line 107

def deconstruct_keys(_keys)
  {app: app, model_name: model_name, model_id: model_id, params: params}
end

#to_sObject



102
103
104
105
# File 'lib/global_id/uri/gid.rb', line 102

def to_s
  # Implement #to_s to avoid no implicit conversion of nil into string when path is nil
  "gid://#{app}#{path}#{'?' + query if query}"
end