Sha256: e6eaa09a778c99846bb8ed456969a9648ec9e59fe89245fbdbff98ef09f4be70

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module Qa::Authorities
  class Local < Qa::Authorities::Base
    
    attr_accessor :response
    
    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

    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

    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
    
    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
    
    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
    
    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
        
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qa-0.0.1 lib/qa/authorities/local.rb