Module: Qa::Authorities::Discogs::DiscogsTranslation

Includes:
DiscogsInstanceBuilder, DiscogsUtils, DiscogsWorksBuilder
Included in:
GenericAuthority
Defined in:
lib/qa/authorities/discogs/discogs_translation.rb

Constant Summary

Constants included from DiscogsUtils

Qa::Authorities::Discogs::DiscogsUtils::DISCOGS_FORMATS_MAPPING, Qa::Authorities::Discogs::DiscogsUtils::DISCOGS_GENRE_MAPPING

Instance Method Summary collapse

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 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, #contruct_stmt_literal_object, #contruct_stmt_uri_object, #discogs_formats, #discogs_genres, #rdf_type_predicate, #rdfs_label_predicate

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

Instance Method Details

#build_graph(response, subauthority = "", format: :jsonld) ⇒ Array, String

Returns modified Discogs data in json-ld format. The data is first structured as RDF statements that use the BIBFRAME ontology. Where applicable, Discogs terms are mapped to the URIs of corresponding objects in the Library of Congress vocabulary.

Parameters:

  • the (Hash)

    http response from discogs

  • the (String)

    subauthority

Returns:

  • (Array, String)

    requested RDF serialzation (supports: jsonld Array, n3 String, n-triples String)



15
16
17
18
19
20
21
22
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 15

def build_graph(response, subauthority = "", format: :jsonld)
  graph = RDF::Graph.new

  rdf_statements = compile_rdf_statements(response, subauthority)
  graph.insert_statements(rdf_statements)

  graph.dump(format, standard_prefixes: true)
end

#build_instance_statements(response) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

Returns:

  • (Array)

    rdf statements



71
72
73
74
75
76
77
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 71

def build_instance_statements(response)
  # get the statements that define the instance
  instance_stmts = get_primary_instance_definition(response)
  instance_stmts.concat(get_format_stmts(response))
  instance_stmts.concat(get_labels_stmts(response))
  instance_stmts.concat(get_identifiers_stmts(response))
end

#build_master_statements(response) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

Returns:

  • (Array)

    rdf statements



59
60
61
62
63
64
65
66
67
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 59

def build_master_statements(response)
  # get the statements that define the primary work
  master_stmts = get_primary_work_definition(response)
  master_stmts.concat(get_primary_artists_stmts(response))
  master_stmts.concat(get_extra_artists_stmts(response))
  master_stmts.concat(get_genres_stmts(response))
  # get the statements that define the secondary works by converting the tracklist
  master_stmts.concat(get_tracklist_artists_stmts(response))
end

#compile_rdf_statements(response, subauthority) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

  • the (String)

    subauthority

Returns:

  • (Array)

    rdf statements



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 27

def compile_rdf_statements(response, subauthority)
  complete_rdf_stmts = []
  # The necessary statements depend on the subauthority. If the subauthority is master,
  # all we need is a work and not an instance. If there's no subauthority, we can determine
  # if the discogs record is a master because it will have a main_release field.
  if master_only(response, subauthority)
    self.work_uri = response["uri"]
    complete_rdf_stmts.concat(build_master_statements(response))
  else
    # If the subauthority is not "master," we need to define an instance as well as a
    # work. If the discogs record has a master_id, fetch that and use the results to
    # build the statements for the work.
    master_resp = response["master_id"].present? ? json("https://api.discogs.com/masters/#{response['master_id']}") : response
    self.work_uri = master_resp["uri"] if master_resp["uri"].present? && master_resp["uri"].include?("master")
    complete_rdf_stmts.concat(build_master_statements(master_resp))
    # Now do the statements for the instance.
    self.instance_uri = response["uri"] if response["uri"].present?
    complete_rdf_stmts.concat(build_instance_statements(response))
  end
end

#get_primary_artists_stmts(response) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

Returns:

  • (Array)

    rdf statements



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 109

def get_primary_artists_stmts(response)
  stmts = []
  # can have multiple artists as primary contributors to the work; need to distinguish among them
  count = 1
  # for secondary contributors to the work
  if response["artists"].present?
    response["artists"].each do |artist|
      # we need the primary artists later when we loop through the track list, so build this array
      primary_artists << artist

      stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn#{count}")
      stmts << contruct_stmt_uri_object("contrbn#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
      stmts << contruct_stmt_uri_object("contrbn#{count}", bf_agent_predicate, "agentn#{count}")
      stmts << contruct_stmt_uri_object("agentn#{count}", rdf_type_predicate, bf_agent_type_object)
      stmts << contruct_stmt_literal_object("agentn#{count}", rdfs_label_predicate, artist["name"])
      count += 1
    end
  end
  stmts
end

#get_primary_instance_definition(response) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

Returns:

  • (Array)

    rdf statements



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 94

def get_primary_instance_definition(response)
  stmts = []
  stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/hasInstance", instance_uri)
  stmts << contruct_stmt_uri_object(instance_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
  stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen2")
  stmts << contruct_stmt_literal_object("titlen2", bf_main_title_predicate, response["title"])
  stmts << contruct_stmt_uri_object("titlen2", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
  stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/identifiedBy", "widn1")
  stmts << contruct_stmt_uri_object("widn1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
  stmts << contruct_stmt_literal_object("widn1", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

#get_primary_work_definition(response) ⇒ Array

Returns rdf statements.

Parameters:

  • the (Hash)

    http response from discogs

Returns:

  • (Array)

    rdf statements



81
82
83
84
85
86
87
88
89
90
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 81

def get_primary_work_definition(response)
  stmts = []
  stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
  stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen1")
  stmts << contruct_stmt_literal_object("titlen1", bf_main_title_predicate, response["title"])
  stmts << contruct_stmt_uri_object("titlen1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
  stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
  stmts << contruct_stmt_literal_object(work_uri, "http://id.loc.gov/ontologies/bibframe/originDate", response["year"].to_s) if response["year"].present?
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
end

#master_only(response, subauthority) ⇒ Boolean

Returns true if the subauthority is “master” or the response contains a master

Parameters:

  • the (Hash)

    http response from discogs

  • the (String)

    subauthority

Returns:

  • (Boolean)

    returns true if the subauthority is “master” or the response contains a master



51
52
53
54
55
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 51

def master_only(response, subauthority)
  return true if subauthority == "master"
  return true if response["main_release"].present?
  false
end