rdf-serializers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6db3307aae5e975df5c2a5d3dde85f6354d0b788
4
+ data.tar.gz: b1ed8984ee9a8c054140d311e46e730d26f7bd10
5
+ SHA512:
6
+ metadata.gz: 5e1af6b5e5c7e641516b61cabbf8ced44fa2f85ee4e3954bf9e6d8819621c6716cbdba4efd8cdd86c77c0f4882be3c7dad149f713c49cfe193afdd689a17ee57
7
+ data.tar.gz: 52ef788b59d23e5fef8bab0cce2a4874d956f89b77115d3e8bf67d6885884cfe14a5ab21b6a158d19b7b6c09a8da1adfdc6f465f3ee8eab422901987da8427ba
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class RDF
6
+ class Relationship < JsonApi::Relationship
7
+ def triples
8
+ return [] if subject.blank? || predicate.blank? || data.empty?
9
+ data.map do |iri|
10
+ raise "#{iri} is not a RDF::URI but a #{iri.class}" unless iri.is_a?(::RDF::URI)
11
+ [subject, predicate, iri]
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def data
18
+ @data ||=
19
+ if association.collection?
20
+ iri_for_many(association).compact
21
+ else
22
+ [iri_for_one(association)].compact
23
+ end
24
+ end
25
+
26
+ def iri_for_many(association)
27
+ collection_serializer = association.lazy_association.serializer
28
+ if collection_serializer.respond_to?(:each)
29
+ collection_serializer.map do |serializer|
30
+ serializer.read_attribute_for_serialization(:iri)
31
+ end
32
+ else
33
+ []
34
+ end
35
+ end
36
+
37
+ def iri_for_one(association)
38
+ if belongs_to_id_on_self?(association)
39
+ parent_serializer.read_attribute_for_serialization(:iri)
40
+ else
41
+ serializer = association.lazy_association.serializer
42
+ serializer.read_attribute_for_serialization(:iri) if serializer && association.object
43
+ end
44
+ end
45
+
46
+ def predicate
47
+ @predicate ||= association.reflection.options[:predicate]
48
+ end
49
+
50
+ def subject
51
+ @subject ||= parent_serializer.read_attribute_for_serialization(:iri)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class RDF < Base
6
+ extend ActiveSupport::Autoload
7
+ autoload :Relationship
8
+
9
+ delegate :object, to: :serializer
10
+ delegate :dump, to: :graph
11
+
12
+ def initialize(serializer, options = {})
13
+ super
14
+ @include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
15
+ @fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
16
+ @resource_identifiers = Set.new
17
+ end
18
+
19
+ protected
20
+
21
+ attr_reader :fieldset
22
+
23
+ private
24
+
25
+ def add_attribute(data, iri, value)
26
+ predicate = data.options[:predicate]
27
+ return unless predicate
28
+ value = value.respond_to?(:each) ? value : [value]
29
+ value.compact.map { |v| add_triple(iri, predicate, v) }
30
+ end
31
+
32
+ def add_triple(subject, predicate, object)
33
+ obj =
34
+ case object
35
+ when ::RDF::Resource, ::RDF::Literal
36
+ object
37
+ when ActiveSupport::TimeWithZone
38
+ ::RDF::Literal(object.to_datetime)
39
+ else
40
+ ::RDF::Literal(object)
41
+ end
42
+ @graph << [::RDF::URI(subject), ::RDF::URI(predicate), obj]
43
+ end
44
+
45
+ def attributes_for(serializer, fields)
46
+ serializer.class._attributes_data.map do |key, data|
47
+ next if data.excluded?(serializer)
48
+ next unless fields.nil? || fields.include?(key)
49
+ add_attribute(data, serializer.read_attribute_for_serialization(:iri), serializer.attributes[key])
50
+ end
51
+ end
52
+
53
+ def graph
54
+ return @graph if @graph.present?
55
+ @graph = ::RDF::Graph.new
56
+
57
+ serializers.each { |serializer| process_resource(serializer, @include_directive) }
58
+ serializers.each { |serializer| process_relationships(serializer, @include_directive) }
59
+ instance_options[:meta]&.each { |meta| add_triple(*meta) }
60
+
61
+ @graph
62
+ end
63
+
64
+ def process_relationship(serializer, include_slice)
65
+ if serializer.respond_to?(:each)
66
+ serializer.each { |s| process_relationship(s, include_slice) }
67
+ return
68
+ end
69
+ return unless serializer&.object
70
+ return unless process_resource(serializer, include_slice)
71
+ process_relationships(serializer, include_slice)
72
+ end
73
+
74
+ def process_relationships(serializer, include_slice)
75
+ serializer.associations(include_slice).each do |association|
76
+ process_relationship(association.lazy_association.serializer, include_slice[association.key])
77
+ end
78
+ end
79
+
80
+ def process_resource(serializer, include_slice = {})
81
+ return unless serializer.respond_to?(:iri) || serializer.object.respond_to?(:iri)
82
+ return false unless @resource_identifiers.add?(serializer.read_attribute_for_serialization(:iri))
83
+ resource_object_for(serializer, include_slice)
84
+ true
85
+ end
86
+
87
+ def relationships_for(serializer, requested_associations, include_slice)
88
+ include_directive = JSONAPI::IncludeDirective.new(
89
+ requested_associations,
90
+ allow_wildcard: true
91
+ )
92
+ serializer.associations(include_directive, include_slice).each do |association|
93
+ Relationship.new(serializer, instance_options, association).triples.each do |triple|
94
+ @graph << triple
95
+ end
96
+ end
97
+ end
98
+
99
+ def resource_object_for(serializer, include_slice = {})
100
+ type = type_for(serializer, instance_options).to_s
101
+ serializer.fetch(self) do
102
+ break nil if serializer.read_attribute_for_serialization(:iri).nil?
103
+ requested_fields = fieldset&.fields_for(type)
104
+ attributes_for(serializer, requested_fields)
105
+ end
106
+ requested_associations = fieldset.fields_for(type) || '*'
107
+ relationships_for(serializer, requested_associations, include_slice)
108
+ end
109
+
110
+ def serializers
111
+ serializer.respond_to?(:each) ? serializer : [serializer]
112
+ end
113
+
114
+ def type_for(serializer, instance_options)
115
+ JsonApi::ResourceIdentifier.new(serializer, instance_options).as_json[:type]
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Registers the N-Triples renderer
4
+ #
5
+ # And then in controllers, use `render nt: model`.
6
+ #
7
+ # For example, in a controller action, we can:
8
+ # respond_to do |format|
9
+ # format.nt { render nt: model }
10
+ # end
11
+
12
+ require 'active_model_serializers/adapter/rdf.rb'
13
+
14
+ module RDFSerializers
15
+ module Renderers
16
+ def self.register(symbols, opts = {})
17
+ symbols = [symbols] unless symbols.respond_to?(:each)
18
+ symbols.each do |symbol|
19
+ format = RDF::Format.for(symbol)
20
+ raise "#{symbol} if not a known rdf format" if format.nil?
21
+ Mime::Type.register format.content_type.first, format.file_extension.first
22
+ add_renderer(format, opts)
23
+ end
24
+ end
25
+
26
+ def self.add_renderer(format, opts = {})
27
+ ActionController::Renderers.add format.file_extension.first do |resource, options|
28
+ self.content_type = format.content_type.first
29
+ get_serializer(resource, options.merge(adapter: :rdf)).adapter.dump(format.symbols.first, opts)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RDFSerializers
4
+ module Version
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model_serializers/adapter/rdf'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdf-serializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Dingemans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '6'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.2.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '6'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdf
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.2'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.2'
61
+ description:
62
+ email: arthur@argu.co
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - lib/active_model_serializers/adapter/rdf.rb
68
+ - lib/active_model_serializers/adapter/rdf/relationship.rb
69
+ - lib/rdf_serializers.rb
70
+ - lib/rdf_serializers/renderers.rb
71
+ - lib/rdf_serializers/version.rb
72
+ homepage: http://rubygems.org/gems/rdf_serializers
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.6.11
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Adds RDF serialization, like n-triples or turtle, to active model serializers
95
+ test_files: []