Class: Iriq::Identifier
- Inherits:
-
Object
- Object
- Iriq::Identifier
- Defined in:
- lib/iriq/identifier.rb
Overview
Parsed identifier. Stores the original input alongside the structured fields extracted by the parser.
For URN-style inputs (‘urn:isbn:0451450523`) only `scheme` and `nss` (the Namespace Specific String) are populated; host/path are nil.
Instance Attribute Summary collapse
-
#fragment ⇒ Object
readonly
Returns the value of attribute fragment.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#nss ⇒ Object
readonly
Returns the value of attribute nss.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_segments ⇒ Object
readonly
Returns the value of attribute path_segments.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#canonical ⇒ Object
(also: #to_s)
Rebuild a canonical IRI-like string from the parsed fields.
- #hash ⇒ Object
-
#initialize(original:, scheme: nil, host: nil, port: nil, path: nil, path_segments: [], query: nil, query_params: {}, fragment: nil, nss: nil, kind: :url) ⇒ Identifier
constructor
A new instance of Identifier.
- #url? ⇒ Boolean
- #urn? ⇒ Boolean
Constructor Details
#initialize(original:, scheme: nil, host: nil, port: nil, path: nil, path_segments: [], query: nil, query_params: {}, fragment: nil, nss: nil, kind: :url) ⇒ Identifier
Returns a new instance of Identifier.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/iriq/identifier.rb', line 12 def initialize(original:, scheme: nil, host: nil, port: nil, path: nil, path_segments: [], query: nil, query_params: {}, fragment: nil, nss: nil, kind: :url) @original = original @scheme = scheme @host = host @port = port @path = path @path_segments = path_segments @query = query @query_params = query_params @fragment = fragment @nss = nss @kind = kind end |
Instance Attribute Details
#fragment ⇒ Object (readonly)
Returns the value of attribute fragment.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def fragment @fragment end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def host @host end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def kind @kind end |
#nss ⇒ Object (readonly)
Returns the value of attribute nss.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def nss @nss end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def original @original end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def path @path end |
#path_segments ⇒ Object (readonly)
Returns the value of attribute path_segments.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def path_segments @path_segments end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def port @port end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def query @query end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def query_params @query_params end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
8 9 10 |
# File 'lib/iriq/identifier.rb', line 8 def scheme @scheme end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
63 64 65 |
# File 'lib/iriq/identifier.rb', line 63 def ==(other) other.is_a?(Identifier) && other.canonical == canonical end |
#canonical ⇒ Object Also known as: to_s
Rebuild a canonical IRI-like string from the parsed fields. Preserves Unicode display form (no punycode / percent-encoding pass).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/iriq/identifier.rb', line 38 def canonical if urn? "urn:#{nss}" else out = +"" out << "#{scheme}://" if scheme out << host if host out << ":#{port}" if port has_query = query && !query.empty? has_fragment = fragment && !fragment.empty? if path_segments.any? out << "/" + path_segments.join("/") elsif has_query || has_fragment # RFC 3986: an authority with query/fragment but no path needs the # implied "/" to be a valid URI. out << "/" end out << "?#{query}" if has_query out << "##{fragment}" if has_fragment out end end |
#hash ⇒ Object
68 69 70 |
# File 'lib/iriq/identifier.rb', line 68 def hash canonical.hash end |
#url? ⇒ Boolean
32 33 34 |
# File 'lib/iriq/identifier.rb', line 32 def url? kind == :url end |
#urn? ⇒ Boolean
28 29 30 |
# File 'lib/iriq/identifier.rb', line 28 def urn? kind == :urn end |