Class: Qa::Authorities::Oclcts

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

Constant Summary collapse

SRU_SERVER_CONFIG =
YAML.load_file(Rails.root.join("config", "oclcts-authorities.yml"))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOclcts

Returns a new instance of Oclcts.



11
12
# File 'lib/qa/authorities/oclcts.rb', line 11

def initialize
end

Instance Attribute Details

#sub_authorityObject

Returns the value of attribute sub_authority.



9
10
11
# File 'lib/qa/authorities/oclcts.rb', line 9

def sub_authority
  @sub_authority
end

Class Method Details

.authority_valid?(sub_authority) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/qa/authorities/oclcts.rb', line 14

def self.authority_valid?(sub_authority)
  self.sub_authorities.include?(sub_authority)
end

.sub_authoritiesObject



18
19
20
# File 'lib/qa/authorities/oclcts.rb', line 18

def self.sub_authorities
  @sub_authorities ||= SRU_SERVER_CONFIG["authorities"].map { |sub_authority| sub_authority[0] }
end

Instance Method Details

#get_full_record(id, sub_authority) ⇒ Object



31
32
33
34
# File 'lib/qa/authorities/oclcts.rb', line 31

def get_full_record(id, sub_authority)
  raw_response = get_raw_response("id-lookup", id, sub_authority)
  parse_full_record(raw_response, id)
end

#get_raw_response(query_type, id, sub_authority) ⇒ Object



47
48
49
50
# File 'lib/qa/authorities/oclcts.rb', line 47

def get_raw_response(query_type, id, sub_authority)
  query_url = SRU_SERVER_CONFIG["url-pattern"][query_type].gsub("{query}", id).gsub("{id}", id).gsub("{authority-id}", sub_authority)
  Nokogiri::XML(open(query_url))
end

#parse_full_record(raw_xml, id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/qa/authorities/oclcts.rb', line 36

def parse_full_record(raw_xml, id)
  a = {}
  zthes_record = raw_xml.xpath("sru:searchRetrieveResponse/sru:records/sru:record/sru:recordData/Zthes/term[termId='#{id}']", 'sru' => 'http://www.loc.gov/zing/srw/')
  zthes_record.children.each do |child|
    if (child.is_a? Nokogiri::XML::Element) && (!child.children.nil?) && (child.children.size == 1) && (child.children.first.is_a? Nokogiri::XML::Text)
      a[child.name] = child.children.first.to_s
    end
  end
  a
end

#search(q, sub_authority = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/qa/authorities/oclcts.rb', line 22

def search(q, sub_authority=nil)
  raw_response = get_raw_response("prefix-query", q, sub_authority)
  r = Array.new
  raw_response.xpath('sru:searchRetrieveResponse/sru:records/sru:record/sru:recordData', 'sru' => 'http://www.loc.gov/zing/srw/').each do |record|
    r.append({"id" => record.xpath('Zthes/term/termId').first.content, "label" => record.xpath('Zthes/term/termName').first.content})
  end
  r
end