Class: Qa::Authorities::LinkedData::TermConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/qa/authorities/linked_data/config/term_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ TermConfig

Returns a new instance of TermConfig.

Parameters:

  • config (Hash)

    the term portion of the config



9
10
11
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 9

def initialize(config)
  @term_config = config
end

Instance Method Details

#supports_term?True|False

Does this authority configuration have term defined?

Returns:

  • (True|False)

    true if term fetching is configured; otherwise, false



18
19
20
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 18

def supports_term?
  term_config.present?
end

#term_id_expects_id?True|False

Is the term_id substitution expected to be an ID?

Returns:

  • (True|False)

    true if the id substitution is expected to be an ID in the term url; otherwise, false



52
53
54
55
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 52

def term_id_expects_id?
  return false if term_config.nil? || !(term_config.key? :term_id)
  term_config[:term_id] == "ID"
end

#term_id_expects_uri?True|False

Is the term_id substitution expected to be a URI?

Returns:

  • (True|False)

    true if the id substitution is expected to be a URI in the term url; otherwise, false



45
46
47
48
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 45

def term_id_expects_uri?
  return false if term_config.nil? || !(term_config.key? :term_id)
  term_config[:term_id] == "URI"
end

#term_languageSymbol

Return the preferred language for literal value selection for term fetch. Only applies if the authority provides language encoded literals.

Returns:

  • (Symbol)

    the configured language for term fetch (default - :en)



59
60
61
62
63
64
65
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 59

def term_language
  return @term_language unless @term_language.nil?
  lang = Config.config_value(term_config, :language)
  return nil if lang.nil?
  lang = [lang] if lang.is_a? String
  @term_language = lang.collect(&:to_sym)
end

#term_qa_replacement_patternsHash

Return parameters that are required for QA api

Returns:

  • (Hash)

    the configured term url parameter mappings



111
112
113
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 111

def term_qa_replacement_patterns
  Config.config_value(term_config, :qa_replacement_patterns)
end

#term_replacement_countInteger

Return the number of possible replacement values to make in the term URL

Returns:

  • (Integer)

    the configured number of possible replacements in the term url



123
124
125
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 123

def term_replacement_count
  term_replacements.size
end

#term_replacementsHash

Return the replacement configurations

Returns:

  • (Hash)

    the configurations for term url replacements



129
130
131
132
133
134
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 129

def term_replacements
  return @term_replacements unless @term_replacements.nil?
  @term_replacements = {}
  @term_replacements = term_url_mappings.select { |k, _v| !term_qa_replacement_patterns.include?(k) } unless term_config.nil? || term_url_mappings.nil?
  @term_replacements
end

#term_replacements?True|False

Are there replacement parameters configured for term fetch?

Returns:

  • (True|False)

    true if there are replacement parameters configured for term fetch; otherwise, false



117
118
119
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 117

def term_replacements?
  term_replacement_count.positive?
end

#term_resultsHash

Return results predicates

Returns:

  • (Hash)

    all the configured predicates to pull out of the results



69
70
71
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 69

def term_results
  Config.config_value(term_config, :results)
end

#term_results_altlabel_predicateString

Return results altlabel_predicate

Returns:

  • (String)

    the configured predicate to use to extract altlabel values from the results



87
88
89
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 87

def term_results_altlabel_predicate
  Config.predicate_uri(term_results, :altlabel_predicate)
end

#term_results_broader_predicateString

Return results broader_predicate

Returns:

  • (String)

    the configured predicate to use to extract URIs for broader terms from the results



93
94
95
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 93

def term_results_broader_predicate
  Config.predicate_uri(term_results, :broader_predicate)
end

#term_results_id_predicateString

Return results id_predicate

Returns:

  • (String)

    the configured predicate to use to extract the id from the results



75
76
77
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 75

def term_results_id_predicate
  Config.predicate_uri(term_results, :id_predicate)
end

#term_results_label_predicateString

Return results label_predicate

Returns:

  • (String)

    the configured predicate to use to extract label values from the results



81
82
83
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 81

def term_results_label_predicate
  Config.predicate_uri(term_results, :label_predicate)
end

#term_results_narrower_predicateString

Return results narrower_predicate

Returns:

  • (String)

    the configured predicate to use to extract URIs for narrower terms from the results



99
100
101
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 99

def term_results_narrower_predicate
  Config.predicate_uri(term_results, :narrower_predicate)
end

#term_results_sameas_predicateString

Return results sameas_predicate

Returns:

  • (String)

    the configured predicate to use to extract URIs for sameas terms from the results



105
106
107
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 105

def term_results_sameas_predicate
  Config.predicate_uri(term_results, :sameas_predicate)
end

#term_subauthoritiesHash

Return the list of subauthorities for term fetch

Returns:

  • (Hash)

    the configurations for term url replacements



157
158
159
160
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 157

def term_subauthorities
  @term_subauthorities ||= {} if term_config.nil? || !(term_config.key? :subauthorities)
  @term_subauthorities ||= term_config[:subauthorities]
end

#term_subauthorities?True|False

Are there subauthorities configured for term fetch?

Returns:

  • (True|False)

    true if there are subauthorities configured term fetch; otherwise, false



138
139
140
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 138

def term_subauthorities?
  term_subauthority_count.positive?
end

#term_subauthority?(subauth_name) ⇒ True|False

Is a specific subauthority configured for term fetch?

Returns:

  • (True|False)

    true if the specified subauthority is configured for term fetch; otherwise, false



144
145
146
147
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 144

def term_subauthority?(subauth_name)
  subauth_name = subauth_name.to_sym if subauth_name.is_a? String
  term_subauthorities.key? subauth_name
end

#term_subauthority_countInteger

Return the number of subauthorities defined for term fetch

Returns:

  • (Integer)

    the number of subauthorities defined for term fetch



151
152
153
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 151

def term_subauthority_count
  term_subauthorities.size
end

#term_subauthority_replacement_patternHash

Return the replacement configurations

Returns:

  • (Hash)

    the configurations for term url replacements



164
165
166
167
168
169
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 164

def term_subauthority_replacement_pattern
  return {} unless term_subauthorities?
  @term_subauthority_replacement_pattern ||= {} if term_config.nil? || !term_subauthorities?
  pattern = term_qa_replacement_patterns[:subauth]
  @term_subauthority_replacement_pattern ||= { pattern: pattern, default: term_url_mappings[pattern.to_sym][:default] }
end

#term_urlHash

Return term url encoding defined in the configuration for this authority.

Returns:

  • (Hash)

    the configured term url



24
25
26
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 24

def term_url
  Config.config_value(term_config, :url)
end

#term_url_mappingsHash

Return term url parameter mapping defined in the configuration for this authority.

Returns:

  • (Hash)

    the configured term url parameter mappings with variable name as key



36
37
38
39
40
41
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 36

def term_url_mappings
  return @term_url_mappings unless @term_url_mappings.nil?
  mappings = Config.config_value(term_url, :mapping)
  return {} if mappings.nil?
  Hash[*mappings.collect { |m| [m[:variable].to_sym, m] }.flatten]
end

#term_url_templateString

Return term url template defined in the configuration for this authority.

Returns:

  • (String)

    the configured term url template



30
31
32
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 30

def term_url_template
  Config.config_value(term_url, :template)
end

#term_url_with_replacements(id, sub_auth = nil, replacements = {}) ⇒ String

Build a linked data authority term url

Parameters:

  • the (String)

    id

  • (optional) (String)

    subauthority key

  • (optional) (Hash)

    replacement values with { pattern_name (defined in YAML config) => value }

Returns:

  • (String)

    the term encoded url



176
177
178
179
180
181
182
183
# File 'lib/qa/authorities/linked_data/config/term_config.rb', line 176

def term_url_with_replacements(id, sub_auth = nil, replacements = {})
  return nil unless supports_term?
  sub_auth = sub_auth.to_sym if sub_auth.is_a? String
  url = Config.replace_pattern(term_url_template, term_qa_replacement_patterns[:term_id], id)
  url = Config.process_subauthority(url, term_subauthority_replacement_pattern, term_subauthorities, sub_auth) if term_subauthorities?
  url = Config.apply_replacements(url, term_replacements, replacements) if term_replacements?
  url
end