class Qa::Authorities::Loc::GenericAuthority

Attributes

subauthority[R]

Public Class Methods

new(subauthority) click to toggle source
Calls superclass method
# File lib/qa/authorities/loc/generic_authority.rb, line 4
def initialize(subauthority)
  super()
  @subauthority = subauthority
end

Public Instance Methods

build_query_url(q) click to toggle source
# File lib/qa/authorities/loc/generic_authority.rb, line 27
def build_query_url(q)
  escaped_query = ERB::Util.url_encode(q)
  authority_fragment = Loc.get_url_for_authority(subauthority) + ERB::Util.url_encode(subauthority)
  "https://id.loc.gov/search/?q=#{escaped_query}&q=#{authority_fragment}&format=json"
end
find(id) click to toggle source
# File lib/qa/authorities/loc/generic_authority.rb, line 33
def find(id)
  json(find_url(id))
end
find_url(id) click to toggle source
# File lib/qa/authorities/loc/generic_authority.rb, line 37
def find_url(id)
  "https://id.loc.gov/authorities/#{@subauthority}/#{id}.json"
end
response(url) click to toggle source
# File lib/qa/authorities/loc/generic_authority.rb, line 11
def response(url)
  uri = URI(url)
  conn = Faraday.new "#{uri.scheme}://#{uri.host}"
  conn.options.params_encoder = Faraday::FlatParamsEncoder
  conn.get do |req|
    req.headers['Accept'] = 'application/json'
    req.url uri.path
    req.params = Rack::Utils.parse_query(uri.query)
  end
end

Private Instance Methods

loc_response_to_qa(data) click to toggle source

Simple conversion from LoC-based struct to QA hash

# File lib/qa/authorities/loc/generic_authority.rb, line 75
def loc_response_to_qa(data)
  {
    "id" => data.id || data.title,
    "label" => data.title
  }
end
parse_authority_response() click to toggle source

Reformats the data received from the LOC service

# File lib/qa/authorities/loc/generic_authority.rb, line 44
def parse_authority_response
  @raw_response.select { |response| response[0] == "atom:entry" }.map do |response|
    loc_response_to_qa(response_to_struct(response))
  end
end
response_to_struct(response) click to toggle source

Converts most of the atom data into an OpenStruct object.

Note that this is a pretty naive conversion. There should probably just be a class that properly translates and stores the various pieces of data, especially if this logic could be useful in other auth lookups.

# File lib/qa/authorities/loc/generic_authority.rb, line 55
def response_to_struct(response)
  contents = response.each_with_object({}) do |result_parts, result|
    next unless result_parts[0]
    key = result_parts[0].sub('atom:', '').sub('dcterms:', '')
    info = result_parts[1]
    val = result_parts[2]

    case key
    when 'title', 'id', 'name', 'updated', 'created'
      result[key] = val
    when 'link'
      result["links"] ||= []
      result["links"] << [info["type"], info["href"]]
    end
  end

  OpenStruct.new(contents)
end