Class: URI::UID

Inherits:
Generic
  • Object
show all
Includes:
UniversalID::Extensions::GlobalIDUIDExtension
Defined in:
lib/uri/uid.rb

Constant Summary collapse

VERSION =
UniversalID::VERSION
SCHEME =
"uid"
HOST =
"universalid"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UniversalID::Extensions::GlobalIDUIDExtension

included, #to_global_id_model

Class Method Details

.build(object, options = {}, &block) ⇒ Object



36
37
38
39
# File 'lib/uri/uid.rb', line 36

def build(object, options = {}, &block)
  path = "/#{encode(object, options, &block)}"
  parse "#{SCHEME}://#{HOST}#{path}##{fingerprint(object)}"
end

.build_string(payload, object = nil) ⇒ Object



32
33
34
# File 'lib/uri/uid.rb', line 32

def build_string(payload, object = nil)
  "#{SCHEME}://#{HOST}/#{payload}##{fingerprint(object)}"
end

.decodeObject



46
47
48
# File 'lib/uri/uid.rb', line 46

def decode(...)
  encoder.decode(...)
end

.encode(object, options = {}) ⇒ Object



41
42
43
44
# File 'lib/uri/uid.rb', line 41

def encode(object, options = {})
  return yield(object, options) if block_given?
  encoder.encode object, options
end

.encoderObject



14
15
16
# File 'lib/uri/uid.rb', line 14

def encoder
  UniversalID::Encoder
end

.fingerprint(object) ⇒ Object



18
19
20
# File 'lib/uri/uid.rb', line 18

def fingerprint(object)
  encode fingerprint_components(object)
end

.newURI::UID

Creates a new URI::UID with the given URI components. SEE: ruby-doc.org/3.2.2/stdlibs/uri/URI/Generic.html#method-c-new

# @raise [URI::InvalidURIError] if the URI is malformed.

Parameters:

  • scheme (String)

    the scheme component.

  • userinfo (String)

    the userinfo component.

  • host (String)

    the host component.

  • port (Integer)

    the port component.

  • registry (String)

    the registry component.

  • path (String)

    the path component.

  • opaque (String)

    the opaque component.

  • query (String)

    the query component.

  • fragment (String)

    the fragment component.

  • parser (URI::Parser)

    the parser to use for the URI, defaults to DEFAULT_PARSER.

  • arg_check (Boolean)

    whether to check arguments, defaults to false.

Returns:

  • (URI::UID)

    the new URI::UID instance.

Raises:

  • (ArgumentError)

    if the number of arguments is incorrect or an argument is of the wrong type.

  • (TypeError)

    if an argument is not of the expected type.

  • (URI::InvalidComponentError)

    if a component of the URI is not valid.

  • (URI::BadURIError)

    if the URI is in a bad or unexpected state.



70
71
72
73
74
75
76
77
78
# File 'lib/uri/uid.rb', line 70

def new(...)
  super.tap do |uri|
    if uri.invalid?
      raise ::URI::InvalidComponentError, "Scheme must be `#{SCHEME}`" if uri.scheme != SCHEME
      raise ::URI::InvalidComponentError, "Host must be `#{HOST}`" if uri.host != HOST
      raise ::URI::InvalidComponentError, "Unable to parse `payload` from the path component!" if uri.payload.strip.empty?
    end
  end
end

.parse(value) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/uri/uid.rb', line 22

def parse(value)
  return nil if value.nil?
  return value if value.is_a?(self)

  value = value.to_s
  return nil if value.strip.empty?

  new(*::URI.split(value))
end

Instance Method Details

#decodeObject



117
118
119
120
121
122
# File 'lib/uri/uid.rb', line 117

def decode
  return nil unless valid?
  return yield(decode_payload, *decode_fingerprint) if block_given?

  decode_payload
end

#deconstruct_keys(_keys) ⇒ Object



124
125
126
# File 'lib/uri/uid.rb', line 124

def deconstruct_keys(_keys)
  {scheme: scheme, host: host, path: path, fragment: fragment}
end

#fingerprint(decode: false) ⇒ Object



101
102
103
104
# File 'lib/uri/uid.rb', line 101

def fingerprint(decode: false)
  return decode_fingerprint if decode
  fragment
end

#inspectObject



128
129
130
# File 'lib/uri/uid.rb', line 128

def inspect
  "#<URI::UID payload=#{payload.truncate 40}, fingerprint=#{fingerprint.truncate 40}>"
end

#invalid?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/uri/uid.rb', line 113

def invalid?
  !valid?
end

#payloadObject



97
98
99
# File 'lib/uri/uid.rb', line 97

def payload
  path[1..]
end

#valid?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/uri/uid.rb', line 106

def valid?
  case self
  in scheme: SCHEME, host: HOST, path: p, fragment: _ if p.size >= 8 then return true
  else false
  end
end