Class: Qa::IriTemplate::VariableMap

Inherits:
Object
  • Object
show all
Defined in:
app/models/qa/iri_template/variable_map.rb

Constant Summary collapse

TYPE =
"IriTemplateMapping".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variable_map) ⇒ VariableMap

Returns a new instance of VariableMap.

Parameters:

  • variable_map (Hash)

    configuration hash for the variable map

Options Hash (variable_map):

  • :variable (String) — default: required

    name of the variable in the template (e.g. ?query has the name ‘query’)

  • :property (String) — default: optional

    always “hydra:freetextQuery” # TODO what other values are supported and what do they mean

  • :required (Boolean) — default: required

    is this variable required

  • :default (String) — default: optional

    value to use if a value is not provided in the request (default: ”)

  • :encode (Boolean) — default: optional

    whether to url_encode the value (default: false)



19
20
21
22
23
24
25
# File 'app/models/qa/iri_template/variable_map.rb', line 19

def initialize(variable_map)
  @variable = Qa::LinkedData::Config::Helper.fetch_required(variable_map, :variable, nil)
  @required = Qa::LinkedData::Config::Helper.fetch_boolean(variable_map, :required, nil)
  @default = Qa::LinkedData::Config::Helper.fetch(variable_map, :default, '').to_s
  @encode = Qa::LinkedData::Config::Helper.fetch_boolean(variable_map, :encode, false)
  @property = Qa::LinkedData::Config::Helper.fetch(variable_map, :property, 'hydra:freetextQuery')
end

Instance Attribute Details

#variableObject (readonly)

Returns the value of attribute variable.



9
10
11
# File 'app/models/qa/iri_template/variable_map.rb', line 9

def variable
  @variable
end

Instance Method Details

#parameter_value(sub_value = nil) ⇒ Object

Parameter and value to use in substitution, using default is one isn’t passed in. Use when template url specifies variable as ?var_name.

Parameters:

  • value (Object)

    to use if it exists

Returns:

  • the parameter and value to use (e.g. ‘language=fr’)



41
42
43
44
45
# File 'app/models/qa/iri_template/variable_map.rb', line 41

def parameter_value(sub_value = nil)
  simple_value = simple_value(sub_value)
  return '' if simple_value.blank?
  "#{variable}=#{simple_value}"
end

#simple_value(sub_value = nil) ⇒ Object

Value to use in substitution, using default if one isn’t passed in. Use when template url specifies variable as var_name.

Parameters:

  • value (Object)

    to use if it exists

Returns:

  • the value to use (e.g. ‘fr’)

Raises:



30
31
32
33
34
35
36
# File 'app/models/qa/iri_template/variable_map.rb', line 30

def simple_value(sub_value = nil)
  raise Qa::IriTemplate::MissingParameter, "#{variable} is required, but missing" if sub_value.blank? && required?
  return default if sub_value.blank?
  sub_value = sub_value.to_s
  sub_value = ERB::Util.url_encode(sub_value).gsub(".", "%2E") if encode?
  sub_value
end