class Qa::Authorities::Discogs::GenericAuthority

Attributes

instance_uri[RW]
primary_artists[RW]
selected_format[RW]
work_uri[RW]

Public Class Methods

new(subauthority) click to toggle source

@param [String] subauthority to use

Calls superclass method
# File lib/qa/authorities/discogs/generic_authority.rb, line 13
def initialize(subauthority)
  super()
  @subauthority = subauthority
  self.primary_artists = []
  self.work_uri = "workn1"
  self.instance_uri = "instn1"
end

Public Instance Methods

build_query_url(q, tc) click to toggle source

@param [String] the query @param [String] the subauthority @return [String] the url

# File lib/qa/authorities/discogs/generic_authority.rb, line 64
def build_query_url(q, tc)
  page = nil
  per_page = nil
  if tc.params["startRecord"].present?
    page = (tc.params["startRecord"].to_i - 1) / tc.params["maxRecords"].to_i + 1
    per_page = tc.params["maxRecords"]
  else
    page = tc.params["page"]
    per_page = tc.params["per_page"]
  end
  escaped_q = ERB::Util.url_encode(q)
  "https://api.discogs.com/database/search?q=#{escaped_q}&type=#{tc.params['subauthority']}&page=#{page}&per_page=#{per_page}&key=#{discogs_key}&secret=#{discogs_secret}"
end
find(id, tc) click to toggle source

If the subauthority = “all” (though it shouldn’t), call the fetch_discogs_results method to determine whether the id matches a release or master. And if the requested format is json-ld, call the build_graph method in the translation module; otherwise, just return the Discogs json. check the response to determine if it should go to the translation module.

@param [String] the Discogs id of the selected item @param [Class] QA::TermsController @return results in requested format (supports: json, jsonld, n3, ntriples)

# File lib/qa/authorities/discogs/generic_authority.rb, line 51
def find(id, tc)
  response = tc.params["subauthority"].include?("all") ? fetch_discogs_results(id) : json(find_url(id, tc.params["subauthority"]))
  self.selected_format = tc.params["format"]
  return response if response["message"].present?
  return build_graph(response, format: :jsonld) if jsonld?(tc)
  return build_graph(response, format: :n3) if n3?(tc)
  return build_graph(response, format: :ntriples) if ntriples?(tc)
  response
end
find_url(id, subauthority) click to toggle source

@param [String] the id of the selected item @param [String] the subauthority @return [String] the url

# File lib/qa/authorities/discogs/generic_authority.rb, line 81
def find_url(id, subauthority)
  "https://api.discogs.com/#{subauthority}s/#{id}"
end

Private Instance Methods

assemble_search_context(result) click to toggle source

@param [Hash] the results hash from the JSON returned by Discogs

# File lib/qa/authorities/discogs/generic_authority.rb, line 158
def assemble_search_context(result)
  [{ "property" => "Image URL", "values" => get_context_for_string(result['cover_image']) },
   { "property" => "Year", "values" => get_context_for_string(result['year']) },
   { "property" => "Record Labels", "values" => get_context_for_array(result['label']) },
   { "property" => "Formats", "values" => get_context_for_array(result['format']) },
   { "property" => "Type", "values" => get_context_for_string(result['type']) }]
end
build_response_header(response) click to toggle source

@param [Hash] the http response from discogs @param [Class] QA::TermsController @example returns parsed discogs pagination data

# File lib/qa/authorities/discogs/generic_authority.rb, line 142
def build_response_header(response)
  start_record = (response['pagination']['page'] - 1) * response['pagination']['per_page'] + 1
  rh_hash = {}
  rh_hash['start_record'] = start_record
  rh_hash['requested_records'] = response['pagination']['per_page']
  rh_hash['retrieved_records'] = response['results'].length
  rh_hash['total_records'] = response['pagination']['items']
  rh_hash
end
build_uri(result) click to toggle source

@param [Hash] the results hash from the JSON returned by Discogs

# File lib/qa/authorities/discogs/generic_authority.rb, line 153
def build_uri(result)
  result['uri'].present? ? "https://www.discogs.com" + result['uri'].to_s : result['resource_url'].to_s
end
fetch_discogs_results(id) click to toggle source

In the unusual case that we have an id and the subauthority is “all”, we don’t know which Discogs url to use. If the id is a match for both a release and a master (or neither), return that info as the message. Otherwise, return the data for the release or master. @param [String] the id of the selected item @return json results

# File lib/qa/authorities/discogs/generic_authority.rb, line 92
def fetch_discogs_results(id)
  release_resp = json(find_url(id, "release"))
  master_resp = json(find_url(id, "master"))
  message_status = check_for_msg_response(release_resp, master_resp)

  return { "message" => "Neither a master nor a release matches the requested ID." } if message_status == "no responses"
  if message_status == "two responses"
    return { "message" => "Both a master and a release match the requested ID.",
             "resource_url" => ["https://api.discogs.com/masters/#{id}", "https://api.discogs.com/releases/#{id}"] }
  end
  return master_resp unless master_resp.key?("message")
  # return release_resp unless release_resp.key?("message")
  release_resp
end
get_context_for_array(item) click to toggle source

checks if the param is null, returns appropriate value @param [Array] returns an empty array if item is not presentr

# File lib/qa/authorities/discogs/generic_authority.rb, line 174
def get_context_for_array(item)
  item.present? ? item : [""]
end
get_context_for_string(item) click to toggle source

checks if the param is null, returns appropriate value @param [String] returns an empty string if item is not presentr

# File lib/qa/authorities/discogs/generic_authority.rb, line 168
def get_context_for_string(item)
  [item.present? ? item.to_s : ""]
end
parse_authority_response(response) click to toggle source

@param [Hash] the http response from discogs @example returns parsed discogs data with context [{

"uri": "https://www.discogs.com/Frank-Sinatra-And-The-Modernaires-Sorry-Why-Remind-Me/release/4212473",
"id": "4212473",
"label": "Frank Sinatra And The Modernaires - Sorry / Why Remind Me",
"context": [{
        "property": "Image URL",
        "values": ["https://img.discogs.com/1358693671-5430.jpeg.jpg"]
}, {
        "property": "Year",
        "values": ["1950"]
}, {
        "property": "Record Labels",
        "values": ["Columbia"]
}, {
        "property": "Formats",
        "values": ["Shellac", "10\"", "78 RPM"]
}, {
        "property": "Type",
        "values": ["release"]
}]

}]

# File lib/qa/authorities/discogs/generic_authority.rb, line 130
def parse_authority_response(response)
  response['results'].map do |result|
    { 'uri' => build_uri(result),
      'id' => result['id'].to_s,
      'label' => result['title'].to_s,
      'context' => assemble_search_context(result) }
  end
end