class Qa::Authorities::Loc::GenericAuthority
Attributes
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
search(q)
click to toggle source
# File lib/qa/authorities/loc/generic_authority.rb, line 22 def search(q) @raw_response = json(build_query_url(q)) parse_authority_response 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
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