Class: URI::GID

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

Defined Under Namespace

Classes: MissingModelIdError

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'])


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/global_id/uri/gid.rb', line 83

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

  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')


67
68
69
# File 'lib/global_id/uri/gid.rb', line 67

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


59
60
61
62
# File 'lib/global_id/uri/gid.rb', line 59

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


43
44
45
46
47
48
# File 'lib/global_id/uri/gid.rb', line 43

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

#to_sObject



96
97
98
99
# File 'lib/global_id/uri/gid.rb', line 96

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