rdfobjects 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,216 @@
1
+ require 'uri'
2
+ require 'builder'
3
+ require 'date'
4
+ require 'curies'
5
+
6
+ module RDFObject
7
+ class Resource < OpenStruct
8
+ class << self
9
+ attr_reader :instances
10
+
11
+ def instances
12
+ @instances ||= {}
13
+ @instances
14
+ end
15
+
16
+ def reset!
17
+ @instances = {}
18
+ end
19
+
20
+ def register(resource)
21
+ instances
22
+ @instances[resource.uri] = resource
23
+ end
24
+
25
+ def remove(resource)
26
+ instances
27
+ @instances.delete(resource.uri)
28
+ end
29
+
30
+ def exists?(uri)
31
+ instances
32
+ if @instances.has_key?(uri)
33
+ true
34
+ else
35
+ false
36
+ end
37
+ end
38
+ end
39
+
40
+ def initialize(uri)
41
+ if uri.could_be_a_safe_curie?
42
+ uri = Curie.parse uri
43
+ end
44
+ super(:uri=>uri)
45
+ self.class.register(self)
46
+ end
47
+
48
+ def assert(predicate, object)
49
+ curied_predicate = case
50
+ when predicate.could_be_a_safe_curie? then Curie.new_from_curie(predicate)
51
+ when Curie.curie_from_uri(predicate) then Curie.curie_from_uri(predicate)
52
+ else Curie.create_from_uri(predicate)
53
+ end
54
+ self.register_vocabulary(curied_predicate.prefix)
55
+ pred_attr = self.send(curied_predicate.prefix.to_sym)
56
+ return if assertion_exists?(predicate, object)
57
+ if pred_attr[curied_predicate.reference]
58
+ unless pred_attr[curied_predicate.reference].is_a?(Array)
59
+ pred_attr[curied_predicate.reference] = [pred_attr[curied_predicate.reference]]
60
+ end
61
+ pred_attr[curied_predicate.reference] << object
62
+ else
63
+ pred_attr[curied_predicate.reference] = object
64
+ end
65
+ end
66
+
67
+ def assertion_exists?(predicate, object)
68
+ return false unless self[predicate]
69
+ if self[predicate].is_a?(Array)
70
+ return true if self[predicate].index(object)
71
+ else
72
+ return true if self[predicate] == object
73
+ end
74
+ return false
75
+ end
76
+
77
+ def [](uri)
78
+ curie = case
79
+ when uri.could_be_a_safe_curie? then Curie.new_from_curie(uri)
80
+ when Curie.curie_from_uri(uri) then Curie.curie_from_uri(uri)
81
+ else
82
+ return nil
83
+ end
84
+ vocab = self.send(curie.prefix.to_sym)
85
+ return nil unless vocab
86
+ return vocab if curie.reference.empty?
87
+ return vocab[curie.reference]
88
+ end
89
+
90
+ def prefix_for(uri)
91
+ Curie.prefix_for(uri)
92
+ end
93
+
94
+ def register_vocabulary(name)
95
+ return if self.respond_to?(name.to_sym)
96
+ self.new_ostruct_member(name)
97
+ self.send("#{name}=".to_sym, {})
98
+ end
99
+
100
+ def relate(predicate, resource)
101
+ unless resource.is_a?(self.class)
102
+ resource = self.class.new(resource)
103
+ end
104
+ self.assert(predicate, resource)
105
+ end
106
+
107
+ def describe
108
+ rdf = HTTPClient.fetch(self.uri)
109
+ Parser.parse(rdf)
110
+ end
111
+
112
+ def empty_graph?
113
+ Curie.get_mappings.each do | prefix, uri |
114
+ return false if self.respond_to?(prefix.to_sym)
115
+ end
116
+ return true
117
+ end
118
+
119
+ def to_xml
120
+ doc = Builder::XmlMarkup.new
121
+ xmlns = {}
122
+ i = 1
123
+ @namespaces.each do | ns |
124
+ next if ns == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
125
+ xmlns["xmlns:n#{i}"] = ns
126
+ i += 1
127
+ end
128
+ doc.rdf :Description,xmlns.merge({:about=>uri}) do | rdf |
129
+ self.instance_variables.each do | ivar |
130
+ next unless ivar =~ /^@n[0-9]*_/
131
+ prefix, tag = ivar.split('_',2)
132
+ attrs = {}
133
+ curr_attr = self.instance_variable_get("#{ivar}")
134
+ prefix.sub!(/^@/,'')
135
+ prefix = 'rdf' if prefix == 'n0'
136
+ unless curr_attr.is_a?(Array)
137
+ curr_attr = [curr_attr]
138
+ end
139
+ curr_attr.each do | val |
140
+ if val.is_a?(RDFResource)
141
+ attrs['rdf:resource'] = val.uri
142
+ end
143
+ if @modifiers[val.object_id]
144
+ if @modifiers[val.object_id][:language]
145
+ attrs['xml:lang'] = @modifiers[val.object_id][:language]
146
+ end
147
+ if @modifiers[val.object_id][:type]
148
+ attrs['rdf:datatype'] = @modifiers[val.object_id][:type]
149
+ end
150
+ end
151
+ unless attrs['rdf:resource']
152
+ rdf.tag!("#{prefix}:#{tag}", attrs, val)
153
+ else
154
+ rdf.tag!("#{prefix}:#{tag}", attrs)
155
+ end
156
+ end
157
+ end
158
+ end
159
+ doc.target!
160
+ end
161
+
162
+ def to_rss
163
+ doc = Builder::XmlMarkup.new
164
+ xmlns = {}
165
+ i = 1
166
+ @namespaces.each do | ns |
167
+ next if ns == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
168
+ xmlns["xmlns:n#{i}"] = ns
169
+ i += 1
170
+ end
171
+ xmlns["xmlns:rss"] = "http://purl.org/rss/1.0/"
172
+ doc.rdf :RDF, xmlns do | rdf |
173
+ rdf.item :about=>uri do | item |
174
+ self.instance_variables.each do | ivar |
175
+ next unless ivar =~ /^@n[0-9]*_/
176
+ prefix, tag = ivar.split('_',2)
177
+ attrs = {}
178
+ curr_attr = self.instance_variable_get("#{ivar}")
179
+ prefix.sub!(/^@/,'')
180
+ prefix = 'rdf' if prefix == 'n0'
181
+ unless curr_attr.is_a?(Array)
182
+ curr_attr = [curr_attr]
183
+ end
184
+ curr_attr.each do | val |
185
+ if val.is_a?(RDFResource)
186
+ attrs['rdf:resource'] = val.uri
187
+ end
188
+ if @modifiers[val.object_id]
189
+ if @modifiers[val.object_id][:language]
190
+ attrs['xml:lang'] = @modifiers[val.object_id][:language]
191
+ end
192
+ if @modifiers[val.object_id][:type]
193
+ attrs['rdf:datatype'] = @modifiers[val.object_id][:type]
194
+ end
195
+ end
196
+ unless attrs['rdf:resource']
197
+ item.tag!("#{prefix}:#{tag}", attrs, val)
198
+ else
199
+ item.tag!("#{prefix}:#{tag}", attrs)
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ doc.target!
206
+ end
207
+ def self.new(uri)
208
+ if self.exists?(uri)
209
+ return self.instances[uri]
210
+ end
211
+ super(uri)
212
+ end
213
+
214
+
215
+ end
216
+ end
@@ -0,0 +1,7 @@
1
+ require 'nokogiri'
2
+ module RDFObject
3
+ class XMLSerializer
4
+ def self.write(resources)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module RDFObject
2
+ require 'rubygems'
3
+ require 'ostruct'
4
+ require 'curies'
5
+ require File.dirname(__FILE__) + '/rdf_objects/parsers'
6
+ require File.dirname(__FILE__) + '/rdf_objects/rdf_resource'
7
+ require File.dirname(__FILE__) + '/rdf_objects/curies'
8
+ require File.dirname(__FILE__) + '/rdf_objects/data_types'
9
+ require File.dirname(__FILE__) + '/rdf_objects/http_client'
10
+ Curie.remove_prefixes!(:http)
11
+ end