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

Methods included from DiscogsWorksBuilder

#build_artist_array, #build_artist_role_stmts, #build_genres, #build_genres_and_styles, #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)



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



68
69
70
71
72
73
74
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 68

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



56
57
58
59
60
61
62
63
64
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 56

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
# 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)
    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
    complete_rdf_stmts.concat(build_master_statements(master_resp))
    # Now do the statements for the instance.
    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



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

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("Work1", "http://id.loc.gov/ontologies/bibframe/contribution", "Work1PrimaryContribution#{count}")
      stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
      stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", bf_agent_predicate, artist["name"])
      stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
      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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 90

def get_primary_instance_definition(response)
  stmts = []
  stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/hasInstance", "Instance1")
  stmts << contruct_stmt_uri_object("Instance1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
  stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/title", "Instance1Title")
  stmts << contruct_stmt_literal_object("Instance1Title", bf_main_title_predicate, response["title"])
  stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/identifiedBy", "IdentifierPrimary")
  stmts << contruct_stmt_uri_object("IdentifierPrimary", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
  stmts << contruct_stmt_literal_object("IdentifierPrimary", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
  stmts.concat(build_year_statements(response, "Instance"))
  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



78
79
80
81
82
83
84
85
86
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 78

def get_primary_work_definition(response)
  stmts = []
  stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
  stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/title", "Work1Title")
  stmts << contruct_stmt_literal_object("Work1Title", bf_main_title_predicate, response["title"])
  stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
  stmts.concat(build_year_statements(response, "Work"))
  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



48
49
50
51
52
# File 'lib/qa/authorities/discogs/discogs_translation.rb', line 48

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