Class: Qa::Authorities::Loc

Inherits:
WebServiceBase show all
Defined in:
lib/qa/authorities/loc.rb

Instance Attribute Summary

Attributes inherited from WebServiceBase

#raw_response, #response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WebServiceBase

#get_json, #results

Constructor Details

#initializeLoc

Initialze the Loc class with a query and get the http response from LOC’s server. This is set to a JSON object



8
9
# File 'lib/qa/authorities/loc.rb', line 8

def initialize
end

Class Method Details

.authority_valid?(authority) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/qa/authorities/loc.rb', line 83

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

.sub_authoritiesObject



87
88
89
# File 'lib/qa/authorities/loc.rb', line 87

def self.sub_authorities
  @sub_authorities ||= sub_authority_table.keys
end

.sub_authority_tableObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qa/authorities/loc.rb', line 24

def self.sub_authority_table
  @sub_authority_table ||=
    begin
      vocab_base_url = 'cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fvocabulary%2F'
      authority_base_url = 'cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fauthorities%2F'
      datatype_base_url = 'cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fdatatypes%2F'
      vocab_preservation_base_url = 'cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fvocabulary%2Fpreservation%2F'
      {
        'subjects' => authority_base_url,
        'names' => authority_base_url,
        'classification' => authority_base_url,
        'childrensSubjects' => authority_base_url,
        'genreForms' => authority_base_url,
        'graphicMaterials' => vocab_base_url,
        'organizations' => vocab_base_url,
        'relators' => vocab_base_url,
        'countries' => vocab_base_url,
        'geographicAreas' => vocab_base_url,
        'languages' => vocab_base_url,
        'iso639-1' => vocab_base_url,
        'iso639-2' => vocab_base_url,
        'iso639-5' => vocab_base_url,
        'edtf' => datatype_base_url,
        'preservation' => vocab_base_url,
        'actionsGranted' => vocab_base_url,
        'agentType' => vocab_base_url,
        'contentLocationType' => vocab_preservation_base_url,
        'copyrightStatus' => vocab_preservation_base_url,
        'cryptographicHashFunctions' => vocab_preservation_base_url,
        'environmentCharacteristic' => vocab_preservation_base_url,
        'environmentPurpose' => vocab_preservation_base_url,
        'eventRelatedAgentRole' => vocab_preservation_base_url,
        'eventRelatedObjectRole' => vocab_preservation_base_url,
        'eventType' => vocab_preservation_base_url,
        'formatRegistryRole' => vocab_preservation_base_url,
        'hardwareType' => vocab_preservation_base_url,
        'inhibitorTarget' => vocab_preservation_base_url,
        'inhibitorType' => vocab_preservation_base_url,
        'objectCategory' => vocab_preservation_base_url,
        'preservationLevelRole' => vocab_preservation_base_url,
        'relationshipSubType' => vocab_preservation_base_url,
        'relationshipType' => vocab_preservation_base_url,
        'rightsBasis' => vocab_preservation_base_url,
        'rightsRelatedAgentRole' => vocab_preservation_base_url,
        'signatureEncoding' => vocab_preservation_base_url,
        'signatureMethod' => vocab_preservation_base_url,
        'softwareType' => vocab_preservation_base_url,
        'storageMedium' => vocab_preservation_base_url
      }
    end
end

Instance Method Details

#find_record_in_response(raw_response, id) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/qa/authorities/loc.rb', line 113

def find_record_in_response(raw_response, id)
  raw_response.each do |single_response|
    next if single_response[0] != "atom:entry"
    single_response.each do |result_part|
      if (result_part[0] == 'atom:title' ||
          result_part[0] == 'atom:id') && id == result_part[2]
        return single_response
      end
    end
  end
  return nil
end

#get_full_record(id, sub_authority) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/qa/authorities/loc.rb', line 126

def get_full_record(id, sub_authority)
  search(id, sub_authority)
  full_record = find_record_in_response(@raw_response, id)

  if full_record.nil?
    # record not found
    return {}
  end

  parsed_result = {}
  full_record.each do |section|
    if section.class == Array
      label = section[0].split(':').last.to_s
      case label
      when 'title', 'id', 'updated', 'created'
        parsed_result[label] = section[2]
      when 'link'
        if section[1]['type'] != nil
          parsed_result[label + "||#{section[1]['type']}"] = section[1]["href"]
        else
          parsed_result[label] = section[1]["href"]
        end
      when 'author'
        author_list = []
        #FIXME: Find example with two authors to better understand this data.
        author_list << section[2][2]
        parsed_result[label] = author_list
      end
    end
  end
  parsed_result
end

#parse_authority_response(raw_response) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/qa/authorities/loc.rb', line 92

def parse_authority_response(raw_response)
  result = []
  raw_response.each do |single_response|
    next if single_response[0] != "atom:entry"
    id = nil
    label = ''
    single_response.each do |result_part|
      case result_part[0]
      when 'atom:title'
        label = result_part[2]
      when 'atom:id'
        id = result_part[2]
      end
    end

    id ||= label
    result << {"id"=>id, "label"=>label}
  end
  result
end

#search(q, sub_authority = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/qa/authorities/loc.rb', line 11

def search(q, sub_authority=nil)
  if ! (sub_authority.nil?  || Loc.sub_authorities.include?(sub_authority))
    @raw_response = nil
    @response = nil
    return
  end
  q += '*'
  authority_fragment = sub_authorityURL(sub_authority)
  query_url =  "http://id.loc.gov/search/?q=#{q}&q=#{authority_fragment}&format=json"
  @raw_response = get_json(query_url)
  @response = parse_authority_response(@raw_response)
end

#sub_authorityURL(sub_authority) ⇒ Object



77
78
79
80
81
# File 'lib/qa/authorities/loc.rb', line 77

def sub_authorityURL(sub_authority)
  base_url = Loc.sub_authority_table[sub_authority]
  return "" if base_url.nil?
  base_url + URI.escape(sub_authority)
end