Class: Qa::Authorities::Loc

Inherits:
WebServiceBase show all
Extended by:
Deprecation
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



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

def initialize
end

Class Method Details

.authority_valid?(authority) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/qa/authorities/loc.rb', line 85

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

.sub_authoritiesObject



89
90
91
# File 'lib/qa/authorities/loc.rb', line 89

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

.sub_authority_tableObject



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
75
76
# File 'lib/qa/authorities/loc.rb', line 26

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



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/qa/authorities/loc.rb', line 131

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

#full_record(id, sub_authority) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/qa/authorities/loc.rb', line 149

def 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

#get_full_record(id, sub_authority) ⇒ Object



144
145
146
147
# File 'lib/qa/authorities/loc.rb', line 144

def get_full_record(id, sub_authority)
  Deprecation.warn(Loc, "get_full_record is deprecated and will be removed in 0.1.0. Use full_record instead", caller)
  full_record(id, sub_authority)
end

#loc_response_to_qa(data) ⇒ Object

Simple conversion from LoC-based struct to QA hash



124
125
126
127
128
129
# File 'lib/qa/authorities/loc.rb', line 124

def loc_response_to_qa(data)
  {
    "id" => data.id || data.title,
    "label" => data.title
  }
end

#parse_authority_response(raw_responses) ⇒ Object



93
94
95
96
97
# File 'lib/qa/authorities/loc.rb', line 93

def parse_authority_response(raw_responses)
  raw_responses.select {|response| response[0] == "atom:entry"}.map do |response|
    loc_response_to_qa(response_to_struct(response))
  end
end

#response_to_struct(response) ⇒ Object

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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/qa/authorities/loc.rb', line 104

def response_to_struct(response)
  result = 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(result)
end

#search(q, sub_authority = nil) ⇒ Object



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

def search(q, sub_authority=nil)
  if ! (sub_authority.nil?  || Loc.sub_authorities.include?(sub_authority))
    @raw_response = nil
    @response = nil
    return
  end

  q = URI.escape(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



79
80
81
82
83
# File 'lib/qa/authorities/loc.rb', line 79

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