Class: Iriq::Identifier

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#fragmentObject (readonly)

Returns the value of attribute fragment.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def fragment
  @fragment
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def host
  @host
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def kind
  @kind
end

#nssObject (readonly)

Returns the value of attribute nss.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def nss
  @nss
end

#originalObject (readonly)

Returns the value of attribute original.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def original
  @original
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def path
  @path
end

#path_segmentsObject (readonly)

Returns the value of attribute path_segments.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def path_segments
  @path_segments
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def port
  @port
end

#queryObject (readonly)

Returns the value of attribute query.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def query
  @query
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



8
9
10
# File 'lib/iriq/identifier.rb', line 8

def query_params
  @query_params
end

#schemeObject (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

#canonicalObject 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

#hashObject



68
69
70
# File 'lib/iriq/identifier.rb', line 68

def hash
  canonical.hash
end

#url?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/iriq/identifier.rb', line 32

def url?
  kind == :url
end

#urn?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/iriq/identifier.rb', line 28

def urn?
  kind == :urn
end