Module: Newsmlg2::Utils

Defined in:
lib/newsmlg2/utils.rb

Overview

QCode <-> URI conversion using a document's catalogs.

Class Method Summary collapse

Class Method Details

.catalog_store_for(store_or_document) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/newsmlg2/utils.rb', line 34

def catalog_store_for(store_or_document)
  if store_or_document.is_a?(CatalogStore)
    store_or_document
  else
    store_or_document.catalog_store
  end
end

.qcode_to_uri(qcode, store_or_document) ⇒ Object

Expands a qcode (e.g. "ninat:text") to its full concept URI using the schemes declared in the document's catalogs.



12
13
14
15
16
17
18
# File 'lib/newsmlg2/utils.rb', line 12

def qcode_to_uri(qcode, store_or_document)
  store = catalog_store_for(store_or_document)
  alias_name, code = qcode.split(':', 2)
  raise ArgumentError, "not a qcode: #{qcode.inspect}" if code.nil?

  store.get_scheme_for_alias(alias_name).uri + code
end

.uri_to_qcode(uri, store_or_document) ⇒ Object

Compresses a concept URI back to a qcode using the schemes declared in the document's catalogs.



24
25
26
27
28
29
30
31
32
# File 'lib/newsmlg2/utils.rb', line 24

def uri_to_qcode(uri, store_or_document)
  store = catalog_store_for(store_or_document)
  idx = uri.rindex('/')
  raise ArgumentError, "not a concept URI: #{uri.inspect}" unless idx

  scheme_uri = uri[0..idx]
  code = uri[(idx + 1)..]
  "#{store.get_scheme_for_uri(scheme_uri).alias_attr}:#{code}"
end