Module: Iriq::Explanation

Defined in:
lib/iriq/explanation.rb

Overview

Builds a per-segment explanation for a single identifier.

Explanation.explain("https://foo.com/users/123")
# => [
#      { value: "users", type: :literal,    variable: false },
#      { value: "123",   type: :integer_id, variable: true },
#    ]

Class Method Summary collapse

Class Method Details

.explain(input, classifier: SegmentClassifier.new) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/iriq/explanation.rb', line 12

def explain(input, classifier: SegmentClassifier.new)
  iri = input.is_a?(Identifier) ? input : Parser.parse(input)

  if iri.urn?
    explain_urn(iri, classifier)
  else
    iri.path_segments.map { |s| segment_entry(s, classifier) }
  end
end

.explain_urn(iri, classifier) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/iriq/explanation.rb', line 31

def explain_urn(iri, classifier)
  return [] unless iri.nss

  if iri.nss.include?(":")
    ns, value = iri.nss.split(":", 2)
    [
      { value: ns, type: :literal, variable: false },
      segment_entry(value, classifier),
    ]
  else
    [segment_entry(iri.nss, classifier)]
  end
end

.segment_entry(segment, classifier) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/iriq/explanation.rb', line 22

def segment_entry(segment, classifier)
  type = classifier.classify(segment)
  {
    value:    segment,
    type:     type,
    variable: classifier.variable?(type),
  }
end