Class: Qa::Authorities::Discogs::GenericAuthority
- 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
-
#instance_uri ⇒ Object
Returns the value of attribute instance_uri.
-
#primary_artists ⇒ Object
Returns the value of attribute primary_artists.
-
#selected_format ⇒ Object
Returns the value of attribute selected_format.
-
#work_uri ⇒ Object
Returns the value of attribute work_uri.
Attributes included from WebServiceBase
Instance Method Summary collapse
-
#build_query_url(q, tc) ⇒ String
The url.
-
#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.
-
#find_url(id, subauthority) ⇒ String
The url.
-
#initialize(subauthority) ⇒ GenericAuthority
constructor
A new instance of GenericAuthority.
- #json(url) ⇒ Object
-
#search(q, tc) ⇒ Object
Json results.
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
Methods inherited from Base
Constructor Details
#initialize(subauthority) ⇒ GenericAuthority
Returns a new instance of GenericAuthority.
16 17 18 19 20 21 22 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 16 def initialize() super() @subauthority = self.primary_artists = [] self.work_uri = "workn1" self.instance_uri = "instn1" end |
Instance Attribute Details
#instance_uri ⇒ Object
Returns the value of attribute instance_uri.
13 14 15 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 13 def instance_uri @instance_uri end |
#primary_artists ⇒ Object
Returns the value of attribute primary_artists.
13 14 15 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 13 def primary_artists @primary_artists end |
#selected_format ⇒ Object
Returns the value of attribute selected_format.
13 14 15 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 13 def selected_format @selected_format end |
#work_uri ⇒ Object
Returns the value of attribute work_uri.
13 14 15 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 13 def work_uri @work_uri end |
Instance Method Details
#build_query_url(q, tc) ⇒ String
Returns the url.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 67 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) url = "https://api.discogs.com/database/search?q=#{escaped_q}&type=#{tc.params['subauthority']}&page=#{page}&per_page=#{per_page}" url += "&key=#{discogs_key}&secret=#{discogs_secret}" if discogs_user_token.blank? url 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.
54 55 56 57 58 59 60 61 62 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 54 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.
86 87 88 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 86 def find_url(id, ) "https://api.discogs.com/#{}s/#{id}" end |
#json(url) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 90 def json(url) if discogs_user_token.present? uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri) request['Authorization'] = "Discogs token=#{discogs_user_token}" request['User-Agent'] = 'HykuApp/1.0' response = http.request(request) JSON.parse(response.body) else super end end |
#search(q, tc) ⇒ Object
Returns json results.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/qa/authorities/discogs/generic_authority.rb', line 27 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_user_token.present? || (discogs_key && discogs_secret) Rails.logger.error "Questioning Authority tried to call Discogs, but no user token, 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"] = (response) response_hash["response_header"] = build_response_header(response) return response_hash end (response) end |