Class: Qa::Authorities::Loc::GenericAuthority

Inherits:
Base
  • Object
show all
Includes:
WebServiceBase
Defined in:
lib/qa/authorities/loc/generic_authority.rb

Instance Attribute Summary collapse

Attributes included from WebServiceBase

#raw_response

Instance Method Summary collapse

Methods included from WebServiceBase

#json

Methods inherited from Base

#all

Constructor Details

#initialize(subauthority) ⇒ GenericAuthority

Returns a new instance of GenericAuthority.



4
5
6
7
# File 'lib/qa/authorities/loc/generic_authority.rb', line 4

def initialize(subauthority)
  super()
  @subauthority = subauthority
end

Instance Attribute Details

#subauthorityObject (readonly)

Returns the value of attribute subauthority.



3
4
5
# File 'lib/qa/authorities/loc/generic_authority.rb', line 3

def subauthority
  @subauthority
end

Instance Method Details

#build_query_url(q) ⇒ Object



27
28
29
30
31
# 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) ⇒ Object



33
34
35
# File 'lib/qa/authorities/loc/generic_authority.rb', line 33

def find(id)
  json(find_url(id))
end

#find_url(id) ⇒ Object



37
38
39
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
# File 'lib/qa/authorities/loc/generic_authority.rb', line 22

def search(q)
  @raw_response = json(build_query_url(q))
  parse_authority_response
end