Class: URI::UID
Constant Summary
collapse
- VERSION =
UniversalID::VERSION
- SCHEME =
"uid"
- HOST =
"universalid"
Class Method Summary
collapse
Instance Method Summary
collapse
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
|
.decode ⇒ Object
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
|
.encoder ⇒ Object
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
|
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
#decode ⇒ Object
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
|
#inspect ⇒ Object
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
113
114
115
|
# File 'lib/uri/uid.rb', line 113
def invalid?
!valid?
end
|
#payload ⇒ Object
97
98
99
|
# File 'lib/uri/uid.rb', line 97
def payload
path[1..]
end
|
#valid? ⇒ 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
|