Class: Qa::Authorities::Local

Inherits:
Base
  • Object
show all
Defined in:
lib/qa/authorities/local.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#query_url, #raw_response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

authority_valid?, #results

Constructor Details

#initialize(q, sub_authority) ⇒ Local

Returns a new instance of Local.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/qa/authorities/local.rb', line 6

def initialize(q, sub_authority)
  begin
    sub_authority_hash = YAML.load(File.read(File.join(Qa::Authorities::Local.sub_authorities_path, "#{sub_authority}.yml")))
  rescue
    sub_authority_hash = {}
  end
  @terms = normalize_terms(sub_authority_hash.fetch(:terms, []))
  if q.blank?
    self.response = @terms
  else
    sub_terms = []
    @terms.each { |term| sub_terms << term if term[:term].downcase.start_with?(q.downcase) }
    self.response = sub_terms
  end
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/qa/authorities/local.rb', line 4

def response
  @response
end

Class Method Details

.sub_authoritiesObject



62
63
64
65
66
# File 'lib/qa/authorities/local.rb', line 62

def self.sub_authorities
  sub_auths = []
  Dir.foreach(Qa::Authorities::Local.sub_authorities_path) { |file| sub_auths << File.basename(file, File.extname(file)) }
  sub_auths
end

.sub_authorities_pathObject



53
54
55
56
57
58
59
60
# File 'lib/qa/authorities/local.rb', line 53

def self.sub_authorities_path
  config_path = AUTHORITIES_CONFIG[:local_path]
  if config_path.starts_with?(File::Separator)
    config_path
  else
    File.join(Rails.root, config_path)
  end
end

Instance Method Details

#get_full_record(id) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/qa/authorities/local.rb', line 43

def get_full_record(id)
  target_term = {}
  @terms.each do |term|
    if term[:id] == id
      target_term = term
    end
  end
  target_term.to_json
end

#normalize_terms(terms) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/qa/authorities/local.rb', line 22

def normalize_terms(terms)
  normalized_terms = []
  terms.each do |term|
    if term.is_a? String
      normalized_terms << { :id => term, :term => term }
    else
      term[:id] = term[:id] || term[:term]
      normalized_terms << term
    end
  end
  normalized_terms
end

#parse_authority_responseObject



35
36
37
38
39
40
41
# File 'lib/qa/authorities/local.rb', line 35

def parse_authority_response
  parsed_response = []
  self.response.each do |res|
    parsed_response << { :id => res[:id], :label => res[:term] }
  end
  self.response = parsed_response
end