qa 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. checksums.yaml +5 -5
  2. data/app/controllers/qa/linked_data_terms_controller.rb +17 -39
  3. data/app/controllers/qa/terms_controller.rb +26 -2
  4. data/app/services/qa/linked_data/authority_url_service.rb +35 -21
  5. data/app/services/qa/linked_data/graph_service.rb +2 -4
  6. data/app/services/qa/linked_data/request_header_service.rb +97 -0
  7. data/lib/qa/authorities/discogs/discogs_instance_builder.rb +17 -19
  8. data/lib/qa/authorities/discogs/discogs_translation.rb +26 -22
  9. data/lib/qa/authorities/discogs/discogs_utils.rb +32 -20
  10. data/lib/qa/authorities/discogs/discogs_works_builder.rb +26 -47
  11. data/lib/qa/authorities/discogs/generic_authority.rb +64 -33
  12. data/lib/qa/authorities/getty/tgn.rb +1 -1
  13. data/lib/qa/authorities/linked_data/find_term.rb +60 -18
  14. data/lib/qa/authorities/linked_data/search_query.rb +43 -15
  15. data/lib/qa/authorities/loc/generic_authority.rb +2 -2
  16. data/lib/qa/version.rb +1 -1
  17. data/spec/controllers/linked_data_terms_controller_spec.rb +36 -0
  18. data/spec/controllers/terms_controller_spec.rb +26 -2
  19. data/spec/fixtures/discogs-find-response-json.json +1 -1
  20. data/spec/lib/authorities/discogs/generic_authority_spec.rb +103 -9
  21. data/spec/lib/authorities/getty/tgn_spec.rb +2 -2
  22. data/spec/lib/authorities/linked_data/find_term_spec.rb +11 -11
  23. data/spec/lib/authorities/linked_data/search_query_spec.rb +9 -9
  24. data/spec/lib/authorities/loc_spec.rb +9 -9
  25. data/spec/services/linked_data/authority_url_service_spec.rb +14 -8
  26. data/spec/services/linked_data/graph_service_spec.rb +2 -1
  27. data/spec/services/linked_data/request_header_service_spec.rb +124 -0
  28. metadata +9 -7
@@ -11,14 +11,14 @@ module Qa::Authorities
11
11
  # to the URIs of corresponding objects in the Library of Congress vocabulary.
12
12
  # @param [Hash] the http response from discogs
13
13
  # @param [String] the subauthority
14
- # @return [Array] jsonld
15
- def build_graph(response, subauthority = "")
14
+ # @return [Array, String] requested RDF serialzation (supports: jsonld Array, n3 String, n-triples String)
15
+ def build_graph(response, subauthority = "", format: :jsonld)
16
16
  graph = RDF::Graph.new
17
17
 
18
18
  rdf_statements = compile_rdf_statements(response, subauthority)
19
19
  graph.insert_statements(rdf_statements)
20
20
 
21
- graph.dump(:jsonld, standard_prefixes: true)
21
+ graph.dump(format, standard_prefixes: true)
22
22
  end
23
23
 
24
24
  # @param [Hash] the http response from discogs
@@ -30,14 +30,16 @@ module Qa::Authorities
30
30
  # all we need is a work and not an instance. If there's no subauthority, we can determine
31
31
  # if the discogs record is a master because it will have a main_release field.
32
32
  if master_only(response, subauthority)
33
+ self.work_uri = response["uri"].present? ? response["uri"] : response["resource_url"]
33
34
  complete_rdf_stmts.concat(build_master_statements(response))
34
35
  else
35
- # If the subauthority is not "master," we need to define an instance as well as a
36
+ # If the subauthority is "release," we need to define an instance as well as a
36
37
  # work. If the discogs record has a master_id, fetch that and use the results to
37
38
  # build the statements for the work.
38
39
  master_resp = response["master_id"].present? ? json("https://api.discogs.com/masters/#{response['master_id']}") : response
39
40
  complete_rdf_stmts.concat(build_master_statements(master_resp))
40
- # Now do the statements for the instance.
41
+ # Now do the statements for the release/instance.
42
+ self.instance_uri = response["uri"].present? ? response["uri"] : response["resource_url"]
41
43
  complete_rdf_stmts.concat(build_instance_statements(response))
42
44
  end
43
45
  end
@@ -77,11 +79,12 @@ module Qa::Authorities
77
79
  # @return [Array] rdf statements
78
80
  def get_primary_work_definition(response)
79
81
  stmts = []
80
- stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
81
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/title", "Work1Title")
82
- stmts << contruct_stmt_literal_object("Work1Title", bf_main_title_predicate, response["title"])
83
- stmts << contruct_stmt_uri_object("Work1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
84
- stmts.concat(build_year_statements(response, "Work"))
82
+ stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
83
+ stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen1")
84
+ stmts << contruct_stmt_literal_object("titlen1", bf_main_title_predicate, response["title"])
85
+ stmts << contruct_stmt_uri_object("titlen1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
86
+ stmts << contruct_stmt_uri_object(work_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
87
+ stmts << contruct_stmt_literal_object(work_uri, "http://id.loc.gov/ontologies/bibframe/originDate", response["year"].to_s) if response["year"].present?
85
88
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
86
89
  end
87
90
 
@@ -89,14 +92,14 @@ module Qa::Authorities
89
92
  # @return [Array] rdf statements
90
93
  def get_primary_instance_definition(response)
91
94
  stmts = []
92
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/hasInstance", "Instance1")
93
- stmts << contruct_stmt_uri_object("Instance1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
94
- stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/title", "Instance1Title")
95
- stmts << contruct_stmt_literal_object("Instance1Title", bf_main_title_predicate, response["title"])
96
- stmts << contruct_stmt_uri_object("Instance1", "http://id.loc.gov/ontologies/bibframe/identifiedBy", "IdentifierPrimary")
97
- stmts << contruct_stmt_uri_object("IdentifierPrimary", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
98
- stmts << contruct_stmt_literal_object("IdentifierPrimary", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
99
- stmts.concat(build_year_statements(response, "Instance"))
95
+ stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/instanceOf", work_uri)
96
+ stmts << contruct_stmt_uri_object(instance_uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Instance")
97
+ stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/title", "titlen2")
98
+ stmts << contruct_stmt_literal_object("titlen2", bf_main_title_predicate, response["title"])
99
+ stmts << contruct_stmt_uri_object("titlen2", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
100
+ stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/identifiedBy", "widn1")
101
+ stmts << contruct_stmt_uri_object("widn1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Identifier")
102
+ stmts << contruct_stmt_literal_object("widn1", "http://www.w3.org/1999/02/22-rdf-syntax-ns#value", response["id"])
100
103
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
101
104
  end
102
105
 
@@ -112,10 +115,11 @@ module Qa::Authorities
112
115
  # we need the primary artists later when we loop through the track list, so build this array
113
116
  primary_artists << artist
114
117
 
115
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/contribution", "Work1PrimaryContribution#{count}")
116
- stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
117
- stmts << contruct_stmt_uri_object("Work1PrimaryContribution#{count}", bf_agent_predicate, artist["name"])
118
- stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
118
+ stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn#{count}")
119
+ stmts << contruct_stmt_uri_object("contrbn#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
120
+ stmts << contruct_stmt_uri_object("contrbn#{count}", bf_agent_predicate, "agentn#{count}")
121
+ stmts << contruct_stmt_uri_object("agentn#{count}", rdf_type_predicate, bf_agent_type_object)
122
+ stmts << contruct_stmt_literal_object("agentn#{count}", rdfs_label_predicate, artist["name"])
119
123
  count += 1
120
124
  end
121
125
  end
@@ -9,9 +9,11 @@ module Qa::Authorities
9
9
  # @param [String] either a string used to create a unique URI or an LOC uri in string format
10
10
  # @param [String] or [Class] either a BIBFRAME property uri in string format or an RDF::URI
11
11
  # @param [String] or [Class] strings can be a label or BIBFRAME class uri; class is always RDF::URI
12
- # @return [Class] RDF::Statement with uri as the object
12
+ # @return [Class] RDF::Statement with either a uri or a bnode as the object
13
13
  def contruct_stmt_uri_object(subject, predicate, object)
14
- RDF::Statement(RDF::URI.new(subject), RDF::URI(predicate), RDF::URI.new(object))
14
+ s = subject.include?("http") ? RDF::URI.new(subject) : subject.to_sym
15
+ o = object.to_s.include?("http") ? RDF::URI.new(object) : object.to_sym
16
+ RDF::Statement(s, RDF::URI(predicate), o)
15
17
  end
16
18
 
17
19
  # Constructs an RDF statement where the subject and predicate are URIs and the object is a literal
@@ -20,7 +22,8 @@ module Qa::Authorities
20
22
  # @param [String] or [Class] strings can be a label or BIBFRAME class uri; class is always RDF::URI
21
23
  # @return [Class] RDF::Statement with a literal as the object
22
24
  def contruct_stmt_literal_object(subject, predicate, object)
23
- RDF::Statement(RDF::URI.new(subject), RDF::URI(predicate), RDF::Literal.new(object))
25
+ s = subject.include?("http") ? RDF::URI.new(subject) : subject.to_sym
26
+ RDF::Statement(s, RDF::URI(predicate), RDF::Literal.new(object))
24
27
  end
25
28
 
26
29
  # frequently used predicates and objects
@@ -41,7 +44,7 @@ module Qa::Authorities
41
44
  end
42
45
 
43
46
  def bf_agent_type_object
44
- RDF::URI("http://id.loc.gov/ontologies/bibframe/Agent")
47
+ "http://id.loc.gov/ontologies/bibframe/Agent"
45
48
  end
46
49
 
47
50
  def bf_role_predicate
@@ -49,7 +52,7 @@ module Qa::Authorities
49
52
  end
50
53
 
51
54
  def bf_role_type_object
52
- RDF::URI("http://id.loc.gov/ontologies/bibframe/Role")
55
+ "http://id.loc.gov/ontologies/bibframe/Role"
53
56
  end
54
57
 
55
58
  def discogs_genres
@@ -62,27 +65,36 @@ module Qa::Authorities
62
65
 
63
66
  # both the work and the instance require a statement for the release year
64
67
  # @param [Hash] the http response from discogs
65
- # @param [String] either "Work" or "Instance"
66
68
  # @return [Array] rdf statements
67
- def build_year_statements(response, type)
69
+ def build_year_statements(response)
68
70
  year_stmts = []
69
- if type == "Work" && response["year"].present?
70
- year_stmts = get_year_rdf(type + "1", response["year"])
71
- elsif response["released"].present?
72
- year_stmts = get_year_rdf(type + "1", response["released"])
73
- end
71
+ year_stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/provisionActivity", "daten1")
72
+ year_stmts << contruct_stmt_uri_object("daten1", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Publication")
73
+ year_stmts << contruct_stmt_literal_object("daten1", RDF::URI("http://id.loc.gov/ontologies/bibframe/date"), response["released"].to_s)
74
74
  year_stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
75
75
  end
76
76
 
77
- # @param [String] either "Work1" or "Instance1"
78
- # @param [String] 4-digit year in string format
77
+ # @param [Hash] the extraartists defined at the release level, not the track level
78
+ # @param [Integer] gives the role a unique uri
79
+ # @param [String] the entity type name
79
80
  # @return [Array] rdf statements
80
- def get_year_rdf(type, year)
81
- year_stmts = []
82
- year_stmts << contruct_stmt_uri_object(type, "http://id.loc.gov/ontologies/bibframe/provisionActivity", "#{type}ProvisionActivityDate")
83
- year_stmts << contruct_stmt_uri_object("#{type}ProvisionActivityDate", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/ProvisionActivity")
84
- # Full RDF statement syntax as this one requires a datatype
85
- year_stmts << RDF::Statement(RDF::URI.new("#{type}ProvisionActivityDate"), RDF::URI("http://id.loc.gov/ontologies/bibframe/date"), RDF::Literal.new(year.to_s, datatype: RDF::XSD.date))
81
+ def build_role_stmts(subject_node, role_node, label)
82
+ stmts = []
83
+ stmts << contruct_stmt_uri_object(subject_node, bf_role_predicate, role_node)
84
+ stmts << contruct_stmt_uri_object(role_node, rdf_type_predicate, bf_role_type_object)
85
+ stmts << contruct_stmt_literal_object(role_node, rdfs_label_predicate, label)
86
+ stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
87
+ end
88
+
89
+ # @param [String] the playing speed in string format
90
+ # @param [Integer] gives the playing speed a unique uri
91
+ # @return [Array] rdf statements
92
+ def build_playing_speed_stmts(label, count)
93
+ stmts = []
94
+ stmts << contruct_stmt_uri_object(instance_uri, "http://id.loc.gov/ontologies/bibframe/soundCharacteristic", "speed#{count}")
95
+ stmts << contruct_stmt_uri_object("speed#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/PlayingSpeed")
96
+ stmts << contruct_stmt_literal_object("speed#{count}", rdfs_label_predicate, label)
97
+ stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
86
98
  end
87
99
  end
88
100
  end
@@ -1,7 +1,7 @@
1
1
  require 'rdf'
2
2
  module Qa::Authorities
3
3
  module Discogs
4
- module DiscogsWorksBuilder # rubocop:disable Metrics/ModuleLength
4
+ module DiscogsWorksBuilder
5
5
  include Discogs::DiscogsUtils
6
6
 
7
7
  # @param [Hash] the http response from discogs
@@ -12,27 +12,17 @@ module Qa::Authorities
12
12
  count = 1
13
13
  return stmts unless response["extraartists"].present?
14
14
  response["extraartists"].each do |artist|
15
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/contribution", "Work1SecondaryContribution#{count}")
16
- stmts << contruct_stmt_uri_object("Work1SecondaryContribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Contribution")
17
- stmts << contruct_stmt_uri_object("Work1SecondaryContribution#{count}", bf_agent_predicate, artist["name"])
18
- stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
19
- stmts += build_artist_role_stmts(artist, count)
15
+ stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn1#{count}")
16
+ stmts << contruct_stmt_uri_object("contrbn1#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Contribution")
17
+ stmts << contruct_stmt_uri_object("contrbn1#{count}", bf_agent_predicate, "agentn1#{count}")
18
+ stmts << contruct_stmt_uri_object("agentn1#{count}", rdf_type_predicate, bf_agent_type_object)
19
+ stmts << contruct_stmt_literal_object("agentn1#{count}", rdfs_label_predicate, artist["name"])
20
+ stmts += build_role_stmts("agentn1#{count}", "rolen1#{count}", artist["role"])
20
21
  count += 1
21
22
  end
22
23
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
23
24
  end
24
25
 
25
- # @param [Hash] the extraartists defined at the release level, not the track level
26
- # @param [Integer] gives the role a unique uri
27
- # @return [Array] rdf statements
28
- def build_artist_role_stmts(artist, count)
29
- stmts = []
30
- stmts << contruct_stmt_uri_object(artist["name"], bf_role_predicate, "Work1SecondaryContributor_Role#{count}")
31
- stmts << contruct_stmt_uri_object("Work1SecondaryContributor_Role#{count}", rdf_type_predicate, bf_role_type_object)
32
- stmts << contruct_stmt_literal_object("Work1SecondaryContributor_Role#{count}", rdfs_label_predicate, artist["role"])
33
- stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
34
- end
35
-
36
26
  # @param [Hash] the http response from discogs
37
27
  # @return [Array] rdf statements
38
28
  def get_genres_stmts(response)
@@ -54,24 +44,13 @@ module Qa::Authorities
54
44
  stmts = []
55
45
  dg = discogs_genres[genre.gsub(/\s+/, "")]
56
46
  if dg.present?
57
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/genreForm", dg["uri"])
47
+ stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/genreForm", dg["uri"])
58
48
  stmts << contruct_stmt_uri_object(dg["uri"], rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/GenreForm")
59
49
  stmts << contruct_stmt_literal_object(dg["uri"], rdfs_label_predicate, dg["label"])
60
50
  end
61
51
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
62
52
  end
63
53
 
64
- # @param [String] the uri of the genreForm
65
- # @param [String] the genreForm label
66
- # @return [Array] rdf statements
67
- def build_genres_and_styles(uri, dg_label)
68
- stmts = []
69
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/genreForm", uri)
70
- stmts << contruct_stmt_uri_object(uri, rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/GenreForm")
71
- stmts << contruct_stmt_literal_object(uri, rdfs_label_predicate, dg_label)
72
- stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
73
- end
74
-
75
54
  # @param [Hash] the http response from discogs
76
55
  # @return [Array] rdf statements
77
56
  def get_tracklist_artists_stmts(response)
@@ -103,13 +82,13 @@ module Qa::Authorities
103
82
  # @return [Array] rdf statements
104
83
  def build_secondary_works(track, w_count)
105
84
  stmts = []
106
- stmts << contruct_stmt_uri_object("Work1", "http://id.loc.gov/ontologies/bibframe/hasPart", "Work#{w_count}")
107
- stmts << contruct_stmt_uri_object("Work#{w_count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
108
- stmts << contruct_stmt_uri_object("Work#{w_count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
109
- stmts << contruct_stmt_uri_object("Work#{w_count}", "http://id.loc.gov/ontologies/bibframe/title", "Work#{w_count}Title")
110
- stmts << contruct_stmt_uri_object("Work#{w_count}Title", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
111
- stmts << contruct_stmt_literal_object("Work#{w_count}Title", bf_main_title_predicate, track["title"])
112
- stmts << contruct_stmt_literal_object("Work#{w_count}", "http://id.loc.gov/ontologies/bibframe/duration", track["duration"]) if track["duration"].present?
85
+ stmts << contruct_stmt_uri_object(work_uri, "http://id.loc.gov/ontologies/bibframe/hasPart", "workn#{w_count}")
86
+ stmts << contruct_stmt_uri_object("workn#{w_count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Work")
87
+ stmts << contruct_stmt_uri_object("workn#{w_count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Audio")
88
+ stmts << contruct_stmt_uri_object("workn#{w_count}", "http://id.loc.gov/ontologies/bibframe/title", "titlen3#{w_count}")
89
+ stmts << contruct_stmt_uri_object("titlen3#{w_count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Title")
90
+ stmts << contruct_stmt_literal_object("titlen3#{w_count}", bf_main_title_predicate, track["title"])
91
+ stmts << contruct_stmt_literal_object("workn#{w_count}", "http://id.loc.gov/ontologies/bibframe/duration", track["duration"]) if track["duration"].present?
113
92
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
114
93
  end
115
94
 
@@ -120,10 +99,11 @@ module Qa::Authorities
120
99
  stmts = []
121
100
  count = 1
122
101
  artists.each do |artist|
123
- stmts << contruct_stmt_uri_object("Work#{w_count}", "http://id.loc.gov/ontologies/bibframe/contribution", "Work#{w_count}PrimaryContribution#{count}")
124
- stmts << contruct_stmt_uri_object("Work#{w_count}PrimaryContribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
125
- stmts << contruct_stmt_uri_object("Work#{w_count}PrimaryContribution#{count}", bf_agent_predicate, artist["name"])
126
- stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
102
+ stmts << contruct_stmt_uri_object("workn#{w_count}", "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn#{w_count}#{count}")
103
+ stmts << contruct_stmt_uri_object("contrbn#{w_count}#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bflc/PrimaryContribution")
104
+ stmts << contruct_stmt_uri_object("contrbn#{w_count}#{count}", bf_agent_predicate, "agentn#{w_count}#{count}")
105
+ stmts << contruct_stmt_uri_object("agentn#{w_count}#{count}", rdf_type_predicate, bf_agent_type_object)
106
+ stmts << contruct_stmt_literal_object("agentn#{w_count}#{count}", rdfs_label_predicate, artist["name"])
127
107
  count += 1
128
108
  end
129
109
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
@@ -137,13 +117,12 @@ module Qa::Authorities
137
117
  # to distinguish among contributors to a track/work and their roles
138
118
  count = 1
139
119
  extraartists.each do |artist|
140
- stmts << contruct_stmt_uri_object("Work#{w_count}", "http://id.loc.gov/ontologies/bibframe/contribution", "Work#{w_count}Contribution#{count}")
141
- stmts << contruct_stmt_uri_object("Work#{w_count}Contribution#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Contribution")
142
- stmts << contruct_stmt_uri_object("Work#{w_count}Contribution#{count}", bf_agent_predicate, artist["name"])
143
- stmts << contruct_stmt_uri_object(artist["name"], rdf_type_predicate, bf_agent_type_object)
144
- stmts << contruct_stmt_uri_object(artist["name"], bf_role_predicate, "Work#{w_count}ContributorRole#{count}")
145
- stmts << contruct_stmt_uri_object("Work#{w_count}ContributorRole#{count}", rdf_type_predicate, bf_role_type_object)
146
- stmts << contruct_stmt_literal_object("Work#{w_count}ContributorRole#{count}", rdfs_label_predicate, artist["role"])
120
+ stmts << contruct_stmt_uri_object("workn#{w_count}", "http://id.loc.gov/ontologies/bibframe/contribution", "contrbn#{w_count}2#{count}")
121
+ stmts << contruct_stmt_uri_object("contrbn#{w_count}2#{count}", rdf_type_predicate, "http://id.loc.gov/ontologies/bibframe/Contribution")
122
+ stmts << contruct_stmt_uri_object("contrbn#{w_count}2#{count}", bf_agent_predicate, "agentn#{w_count}2#{count}")
123
+ stmts << contruct_stmt_uri_object("agentn#{w_count}2#{count}", rdf_type_predicate, bf_agent_type_object)
124
+ stmts << contruct_stmt_literal_object("agentn#{w_count}2#{count}", rdfs_label_predicate, artist["name"])
125
+ stmts += build_role_stmts("agentn#{w_count}2#{count}", "role2#{w_count}#{count}", artist["role"])
147
126
  count += 1
148
127
  end
149
128
  stmts # w/out this line, building the graph throws an undefined method `graph_name=' error
@@ -1,16 +1,19 @@
1
1
  require 'rdf'
2
+ require 'rdf/ntriples'
2
3
  module Qa::Authorities
3
4
  class Discogs::GenericAuthority < Base
4
5
  include WebServiceBase
5
6
  include Discogs::DiscogsTranslation
6
7
 
7
8
  class_attribute :discogs_secret, :discogs_key
8
- attr_accessor :primary_artists
9
+ attr_accessor :primary_artists, :selected_format, :work_uri, :instance_uri
9
10
 
10
11
  # @param [String] subauthority to use
11
12
  def initialize(subauthority)
12
13
  @subauthority = subauthority
13
14
  self.primary_artists = []
15
+ self.work_uri = "workn1"
16
+ self.instance_uri = "instn1"
14
17
  end
15
18
 
16
19
  # @param [String] the query
@@ -35,10 +38,14 @@ module Qa::Authorities
35
38
  #
36
39
  # @param [String] the Discogs id of the selected item
37
40
  # @param [Class] QA::TermsController
38
- # @return either json results or jsonld
41
+ # @return results in requested format (supports: json, jsonld, n3, ntriples)
39
42
  def find(id, tc)
40
43
  response = tc.params["subauthority"].include?("all") ? fetch_discogs_results(id) : json(find_url(id, tc.params["subauthority"]))
41
- return build_graph(response) unless tc.params["format"] != "jsonld" || response["message"].present?
44
+ self.selected_format = tc.params["format"]
45
+ return response if response["message"].present?
46
+ return build_graph(response, format: :jsonld) if jsonld?(tc)
47
+ return build_graph(response, format: :n3) if n3?(tc)
48
+ return build_graph(response, format: :ntriples) if ntriples?(tc)
42
49
  response
43
50
  end
44
51
 
@@ -94,46 +101,48 @@ module Qa::Authorities
94
101
 
95
102
  # @param [Hash] the http response from discogs
96
103
  # @example returns parsed discogs data with context
97
- # {
98
- # "uri": "https://api.discogs.com/releases/4212473",
99
- # "id": "4212473",
100
- # "label": "Frank Sinatra And The Modernaires - Sorry / Why Remind Me",
101
- # }
102
- # "context": {
103
- # "Image URL": [
104
- # "https://img.discogs.com/2e-YoNr0dvmMgbzEN0hjHD6X0sU=/fit-in/600x580/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-4212473-1358693671-5430.jpeg.jpg"
105
- # ],
106
- # "Year": [
107
- # "1950"
108
- # ],
109
- # "Record Labels": [
110
- # "Columbia"
111
- # ],
112
- # "Formats": [
113
- # "Shellac",
114
- # "10\"",
115
- # "78 RPM"
116
- # ],
117
- # "Type": [
118
- # "release"
119
- # ]
120
- # }
104
+ # [{
105
+ # "uri": "https://www.discogs.com/Frank-Sinatra-And-The-Modernaires-Sorry-Why-Remind-Me/release/4212473",
106
+ # "id": "4212473",
107
+ # "label": "Frank Sinatra And The Modernaires - Sorry / Why Remind Me",
108
+ # "context": [{
109
+ # "property": "Image URL",
110
+ # "values": ["https://img.discogs.com/1358693671-5430.jpeg.jpg"]
111
+ # }, {
112
+ # "property": "Year",
113
+ # "values": ["1950"]
114
+ # }, {
115
+ # "property": "Record Labels",
116
+ # "values": ["Columbia"]
117
+ # }, {
118
+ # "property": "Formats",
119
+ # "values": ["Shellac", "10\"", "78 RPM"]
120
+ # }, {
121
+ # "property": "Type",
122
+ # "values": ["release"]
123
+ # }]
124
+ # }]
121
125
  def parse_authority_response(response)
122
126
  response['results'].map do |result|
123
- { 'uri' => result['resource_url'].to_s,
127
+ { 'uri' => build_uri(result),
124
128
  'id' => result['id'].to_s,
125
129
  'label' => result['title'].to_s,
126
130
  'context' => assemble_search_context(result) }
127
131
  end
128
132
  end
129
133
 
134
+ # @param [Hash] the results hash from the JSON returned by Discogs
135
+ def build_uri(result)
136
+ result['uri'].present? ? "https://www.discogs.com" + result['uri'].to_s : result['resource_url'].to_s
137
+ end
138
+
130
139
  # @param [Hash] the results hash from the JSON returned by Discogs
131
140
  def assemble_search_context(result)
132
- { "Image URL" => get_context_for_string(result['cover_image']),
133
- "Year" => get_context_for_string(result['year']),
134
- "Record Labels" => get_context_for_array(result['label']),
135
- "Formats" => get_context_for_array(result['format']),
136
- "Type" => get_context_for_string(result['type']) }
141
+ [{ "property" => "Image URL", "values" => get_context_for_string(result['cover_image']) },
142
+ { "property" => "Year", "values" => get_context_for_string(result['year']) },
143
+ { "property" => "Record Labels", "values" => get_context_for_array(result['label']) },
144
+ { "property" => "Formats", "values" => get_context_for_array(result['format']) },
145
+ { "property" => "Type", "values" => get_context_for_string(result['type']) }]
137
146
  end
138
147
 
139
148
  # checks if the param is null, returns appropriate value
@@ -147,5 +156,27 @@ module Qa::Authorities
147
156
  def get_context_for_array(item)
148
157
  item.present? ? item : [""]
149
158
  end
159
+
160
+ def format(tc)
161
+ return 'json' unless tc.params.key?('format')
162
+ return 'json' if tc.params['format'].blank?
163
+ tc.params['format']
164
+ end
165
+
166
+ def jsonld?(tc)
167
+ format(tc).casecmp?('jsonld')
168
+ end
169
+
170
+ def n3?(tc)
171
+ format(tc).casecmp?('n3')
172
+ end
173
+
174
+ def ntriples?(tc)
175
+ format(tc).casecmp?('ntriples')
176
+ end
177
+
178
+ def graph_format?(tc)
179
+ jsonld?(tc) || n3?(tc) || ntriples?(tc)
180
+ end
150
181
  end
151
182
  end