Class: Qa::Authorities::Discogs::GenericAuthority

Inherits:
Base
  • Object
show all
Includes:
DiscogsTranslation, DiscogsUtils, WebServiceBase
Defined in:
lib/qa/authorities/discogs/generic_authority.rb

Constant Summary

Constants included from DiscogsUtils

DiscogsUtils::DISCOGS_FORMATS_MAPPING, DiscogsUtils::DISCOGS_GENRE_MAPPING

Instance Attribute Summary collapse

Attributes included from WebServiceBase

#raw_response

Instance Method Summary collapse

Methods included from DiscogsUtils

#bf_agent_predicate, #bf_agent_type_object, #bf_main_title_predicate, #bf_role_predicate, #bf_role_type_object, #build_playing_speed_stmts, #build_role_stmts, #build_year_statements, #check_for_msg_response, #contruct_stmt_literal_object, #contruct_stmt_uri_object, #discogs_formats, #discogs_genres, #format, #graph_format?, #jsonld?, #n3?, #ntriples?, #rdf_type_predicate, #rdfs_label_predicate

Methods included from DiscogsTranslation

#build_graph, #build_instance_statements, #build_master_statements, #compile_rdf_statements, #get_primary_artists_stmts, #get_primary_instance_definition, #get_primary_work_definition, #master_only

Methods included from DiscogsInstanceBuilder

#build_base_materials, #build_format_characteristics, #build_format_desc_stmts, #build_format_name_stmts, #build_provision_activity_stmts, #get_format_stmts, #get_identifiers_stmts, #get_labels_stmts

Methods included from DiscogsWorksBuilder

#build_artist_array, #build_genres, #build_secondary_works, #build_track_artists, #build_track_extraartists, #get_extra_artists_stmts, #get_genres_stmts, #get_tracklist_artists_stmts

Methods included from WebServiceBase

#json, #response

Methods inherited from Base

#all

Constructor Details

#initialize(subauthority) ⇒ GenericAuthority

Returns a new instance of GenericAuthority.

Parameters:

  • subauthority (String)

    to use



13
14
15
16
17
18
19
# 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

Instance Attribute Details

#instance_uriObject

Returns the value of attribute instance_uri.



10
11
12
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 10

def instance_uri
  @instance_uri
end

#primary_artistsObject

Returns the value of attribute primary_artists.



10
11
12
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 10

def primary_artists
  @primary_artists
end

#selected_formatObject

Returns the value of attribute selected_format.



10
11
12
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 10

def selected_format
  @selected_format
end

#work_uriObject

Returns the value of attribute work_uri.



10
11
12
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 10

def work_uri
  @work_uri
end

Instance Method Details

#build_query_url(q, tc) ⇒ String

Returns the url.

Parameters:

  • the (String)

    query

  • the (String)

    subauthority

Returns:

  • (String)

    the url



64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object

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.

Parameters:

  • the (String)

    Discogs id of the selected item

  • QA::TermsController (Class)

Returns:

  • results in requested format (supports: json, jsonld, n3, ntriples)



51
52
53
54
55
56
57
58
59
# 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) ⇒ String

Returns the url.

Parameters:

  • the (String)

    id of the selected item

  • the (String)

    subauthority

Returns:

  • (String)

    the url



81
82
83
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 81

def find_url(id, subauthority)
  "https://api.discogs.com/#{subauthority}s/#{id}"
end

#search(q, tc) ⇒ Object

Returns json results.

Parameters:

  • the (String)

    query

  • QA::TermsController (Class)

Returns:

  • json results



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 24

def search(q, tc)
  # Discogs distinguishes between masters and releases, where a release represents a specific
  # physical or digital object and a master represents a set of similar releases. Use of a
  # subauthority (e.g., /qa/search/discogs/master) will target a specific type. Using the "all"
  #  subauthority will search for both types.
  unless discogs_key && discogs_secret
    Rails.logger.error "Questioning Authority tried to call Discogs, but no secret and/or key were set."
    return []
  end
  response = json(build_query_url(q, tc))
  if tc.params["response_header"] == "true"
    response_hash = {}
    response_hash["results"] = parse_authority_response(response)
    response_hash["response_header"] = build_response_header(response)
    return response_hash
  end
  parse_authority_response(response)
end