Class: Qa::IriTemplateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/qa/iri_template_service.rb

Class Method Summary collapse

Class Method Details

.build_url(url_config:, substitutions:) ⇒ String

Construct an url from an IriTemplate making identified substitutions

Parameters:

  • url_config (Qa::IriTemplate::UrlConfig)

    configuration (json) holding the template and variable mappings

  • substitutions (HashWithIndifferentAccess)

    name-value pairs to substitute into the url template

Returns:

  • (String)

    url with substitutions



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/qa/iri_template_service.rb', line 9

def build_url(url_config:, substitutions:)
  # TODO: This is a very simple approach using direct substitution into the template string.
  #   Better would be to...
  #     * appropriately adds '?' or '&'
  #     * ensure proper escaping of values (e.g. value="A simple string" which is encoded as A%20simple%20string)
  #   Even more advanced would be to...
  #     * support BasicRepresentation (which is what it does now)
  #     * support ExplicitRepresentation
  #        * literal encoding for values (e.g. value="A simple string" becomes %22A%20simple%20string%22)
  #        * language encoding for values (e.g. value="A simple string" becomes value="A simple string"@en which is encoded as %22A%20simple%20string%22%40en)
  #        * type encoding for values (e.g. value=5.5 becomes value="5.5"^^http://www.w3.org/2001/XMLSchema#decimal which is encoded
  #                                         as %225.5%22%5E%5Ehttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23decimal)
  # Fuller implementations parse the template into component parts and then build the URL by adding parts in as applicable.
  url = url_config.template
  url_config.mapping.each do |m|
    key = m.variable
    url = url.gsub("{#{key}}", m.simple_value(substitutions[key]))
    url = url.gsub("{?#{key}}", m.parameter_value(substitutions[key]))
  end
  clean(url)
end