diograph 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/diograph.rb +351 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 434de2c1102fdad09a26f4eb32d9204da021d6fa
4
+ data.tar.gz: d061818771e8df8ba3b6ada88a0426db03bb183e
5
+ SHA512:
6
+ metadata.gz: 57ca10b18b2a791b8e5a535817a850605a65e708354b0cb067d2965fd5a520f235d84d7a2f64588877e14ea18432fbce4029c4a711e48cb782079f7b6e3a6660
7
+ data.tar.gz: 63f7805687d4addddaa049428f1129e5c3942e653cca21dee4761331243115aba016eb6f45c4455fd745c6e51ad5a0119745bdf63cb3f8cfbf5688a6905f24eb
@@ -0,0 +1,351 @@
1
+ module Diograph
2
+
3
+
4
+ class Diograph
5
+
6
+
7
+ ATTRIBUTES = {
8
+ "diories" => "@graph",
9
+ "diory_id" => "id",
10
+ "diory_type" => "type",
11
+ "name" => "name",
12
+ "url" => "www",
13
+ "date" => "date",
14
+ "gps" => "geo",
15
+ "data" => "data",
16
+ "strong_relations" => "view",
17
+ "weak_relations" => "connected",
18
+ "created_at" => "created",
19
+ "updated_at" => "modified"
20
+ # app-related-type?
21
+ # app-name?
22
+ }
23
+
24
+
25
+
26
+ attr_accessor :diories
27
+
28
+ def initialize
29
+ @diories = []
30
+ end
31
+
32
+ def add(diory)
33
+ @diories.push(diory)
34
+ end
35
+
36
+
37
+ # Create from STRING
38
+ def self.create_from_diograph(diograph)
39
+ diograph_obj = Diograph.new
40
+ diograph_hash = DiographHash.new_hash_from_diograph(diograph)
41
+ diograph_hash[Diograph::ATTRIBUTES['diories']].each do |diory_hash|
42
+ diory = Diory.create_from_hash(diory_hash)
43
+ diograph_obj.diories << diory
44
+ end
45
+ diograph_obj
46
+ end
47
+
48
+
49
+ def self.create_from_database
50
+
51
+ diograph = Diograph.new
52
+ diograph.diories = Diory.all
53
+ diograph
54
+
55
+ end
56
+
57
+ def self.new_hash_from_diograph(diograph)
58
+ JSON.parse(diograph)
59
+ end
60
+
61
+ def self.new_hash
62
+ self.empty_static_hash_template # at the bottom of this class
63
+ end
64
+
65
+
66
+ def to_diograph
67
+ diograph_hash = self.to_diograph_hash
68
+ JSON.pretty_generate(diograph_hash)
69
+ end
70
+
71
+ def to_diograph_hash
72
+ diograph_hash = Diograph.new_hash
73
+ @diories.each do |diory|
74
+ diograph_hash[Diograph::ATTRIBUTES['diories']] << diory.to_diograph_hash
75
+ end
76
+ diograph_hash
77
+ end
78
+
79
+ def to_s
80
+ self.to_diograph
81
+ # puts JSON.pretty_generate(self.to_diograph)
82
+ end
83
+
84
+
85
+
86
+ private
87
+
88
+ def self.empty_static_hash_template
89
+
90
+ diograph = {
91
+ "@context" => {
92
+
93
+ "xsd"=>"http://www.w3.org/2001/XMLSchema#",
94
+ "prov"=>"http://www.w3.org/ns/prov#",
95
+ "dct"=>"http://purl.org/dc/terms/",
96
+ "css"=>"http://www.w3.org/wiki/CSS/Properties/",
97
+ "diograph"=>"http://diograph.org/",
98
+ "diory"=>"http://diograph.org/diory/",
99
+
100
+ "id"=>"@id",
101
+ "type"=>"@type",
102
+ "name"=>"dct:title",
103
+
104
+ "style" => "diory:style",
105
+ "background-color" => "css:background-color",
106
+ "background-image" => "css:background-image",
107
+
108
+ "modified" => {
109
+ "@id" => "dct:modified",
110
+ "@type" => "xsd:dateTime"
111
+ },
112
+ "created" => {
113
+ "@id" => "dct:created",
114
+ "@type" => "xsd:dateTime"
115
+ },
116
+ "data" => "diory:data",
117
+
118
+ "IRI" => {
119
+ "@id" => "dct:abstract",
120
+ "@type" => "@id"
121
+ },
122
+ "www" => {
123
+ "@id" => "dct:source",
124
+ "@type" => "xsd:anyURI"
125
+ },
126
+ "view" => {
127
+ "@id" => "diory:view",
128
+ "@container" => "@list"
129
+ },
130
+ "connected" => {
131
+ "@id" => "diory:connected",
132
+ "@container" => "@list"
133
+ },
134
+
135
+ "ratio" => "diory:ratio",
136
+ "position" => "diory:position",
137
+ "top" => "css:top",
138
+ "left" => "css:left",
139
+ "height" => "css:height",
140
+ "padding-bottom" => "css:padding-bottom",
141
+ "width" => "css:width",
142
+
143
+ "app" => "dct:agent",
144
+ "icon" => "http://purl.org/dc/dcmitype/Image",
145
+
146
+ "owl-time" => "http://www.w3.org/2006/time#",
147
+ "datetime" => "owl-time:inXSDDateTime",
148
+ "dateStart" => {
149
+ "@id" => "owl-time:hasBeginning",
150
+ "@type" => "datetime"
151
+ },
152
+ "dateStop" => {
153
+ "@id" => "owl-time:hasEnding",
154
+ "@type" => "datetime"
155
+ },
156
+ "duration" => "owl-time:duration",
157
+
158
+ "geo" => "http://ld.geojson.org/vocab#",
159
+ "geometry" => "geo:geometry",
160
+ "coordinates" => {
161
+ "@id" => "geo:coordinates",
162
+ "@container" => "@list"
163
+ },
164
+ "Polygon" => "geo:Polygon",
165
+ "Point" => "geo:Point",
166
+
167
+ "dct" => "http://purl.org/dc/terms/",
168
+ "distance" => "dct:SizeOrDuration",
169
+
170
+ "note" => "http://purl.org/dc/dcmitype/Text",
171
+ "photo" => "http://purl.org/dc/dcmitype/image"
172
+
173
+ },
174
+
175
+ "@graph" => []
176
+ }
177
+
178
+ end
179
+
180
+
181
+ end
182
+
183
+
184
+
185
+ class Diory
186
+
187
+ attr_accessor :app_id, :app_related_type, :app_name, :name, :url, :diory_id, :date, :geo, :data, :type, :thumbnail, :created_at, :updated_at, :strong_relations, :weak_relations
188
+
189
+
190
+ def initialize
191
+
192
+ @app_id = nil
193
+ @app_related_type = nil
194
+ @app_name = nil
195
+
196
+ @name = nil
197
+ @url = nil
198
+ @diory_id = nil
199
+ @date = nil
200
+ @geo = nil
201
+ @data = {}
202
+ @type = "diory"
203
+ @thumbnail = nil
204
+
205
+ @created_at = nil
206
+ @updated_at = nil
207
+
208
+ @strong_relations = []
209
+ @weak_relations = []
210
+
211
+ end
212
+
213
+
214
+ def self.generate_id(app_id, app_related_type, name)
215
+ # Cut down too long names
216
+ if name.size > 25
217
+ name = name[0..24]
218
+ end
219
+ # Encode name suitable for an URL
220
+ name = URI.escape(name, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
221
+ # Compose the id
222
+ return app_id + app_related_type + '/' + name
223
+
224
+ end
225
+
226
+ def relate(diory)
227
+ self.strong_relations.push(diory)
228
+ diory.weak_relations.push(self)
229
+ end
230
+
231
+
232
+ # Create Diory itself
233
+ def self.create_from_hash(diory_hash)
234
+
235
+ # Create new or load existing diory
236
+ diory = Diory.new
237
+ diory.load_attributes_from_hash(diory_hash)
238
+
239
+ # Relations or no relations?
240
+ relations = diory_hash["#{Diograph::ATTRIBUTES['strong_relations']}"].blank? ? false : true
241
+
242
+ # Save and update relations
243
+ if relations
244
+ # Go through relations in JSON-LD
245
+ diory_hash["#{Diograph::ATTRIBUTES['strong_relations']}"].each do |related_hash|
246
+ # Create diory from JSON-LD
247
+ related = Diory.create_from_hash(related_hash)
248
+ # Add all the relations for freshly created diory
249
+ if Diory.where(diory_id: related.diory_id).blank?
250
+ diory.strong_relations << related
251
+ end
252
+ end
253
+ end
254
+
255
+ # Return saved diory
256
+ diory
257
+ end
258
+
259
+
260
+ # Fills in all the attributes of Diory from JSON-LD
261
+ def load_attributes_from_hash(diory_hash)
262
+ # Load Diory basic attributes from JSONLD
263
+ @diory_id = diory_hash[Diograph::ATTRIBUTES["diory_id"]]
264
+ @diory_type = diory_hash[Diograph::ATTRIBUTES["diory_type"]]
265
+ @name = diory_hash[Diograph::ATTRIBUTES["name"]]
266
+ @url = diory_hash[Diograph::ATTRIBUTES["url"]]
267
+ @date = diory_hash[Diograph::ATTRIBUTES["date"]]
268
+ @gps = diory_hash[Diograph::ATTRIBUTES["gps"]]
269
+
270
+ # Define which are basic attributes
271
+ diograph_attr = [Diograph::ATTRIBUTES["diory_id"], Diograph::ATTRIBUTES["diory_type"], Diograph::ATTRIBUTES["name"], Diograph::ATTRIBUTES["url"], Diograph::ATTRIBUTES["date"], Diograph::ATTRIBUTES["gps"], Diograph::ATTRIBUTES["strong_relations"]]
272
+
273
+ # Extract all the other attributes to data
274
+ # Sorts out the basic attributes from diory_hash
275
+ diory_hash.select do |key, value|
276
+ if !diograph_attr.include?(key)
277
+ @data[key] = value
278
+ end
279
+ end
280
+
281
+ end
282
+
283
+
284
+ # Get a Diograph hash of the Diory and its related diories
285
+ def to_diograph_hash(include_relations=true)
286
+
287
+ diory_hash = {}
288
+ diory_hash["#{Diograph::ATTRIBUTES['diory_id']}"] = @diory_id
289
+ diory_hash["#{Diograph::ATTRIBUTES['diory_type']}"] = @diory_type
290
+ diory_hash["#{Diograph::ATTRIBUTES['name']}"] = @name
291
+ if !@url.blank?
292
+ diory_hash["#{Diograph::ATTRIBUTES['url']}"] = @url
293
+ end
294
+ if !@date.blank?
295
+ diory_hash["#{Diograph::ATTRIBUTES['date']}"] = @date
296
+ end
297
+ if !@gps.blank?
298
+ diory_hash["#{Diograph::ATTRIBUTES['gps']}"] = @gps
299
+ end
300
+
301
+ diory_hash["style"] = {"background-color" => "rgb(84,97,112)"}
302
+ diory_hash["created"] = @created_at
303
+ diory_hash["modified"] = @updated_at
304
+ diory_hash["IRI"] = nil
305
+
306
+ if !@data.blank?
307
+ diory_hash.merge!(DiographHash.new_hash_from_diograph(@data))
308
+ end
309
+
310
+ diory_hash["#{Diograph::ATTRIBUTES['strong_relations']}"] = []
311
+
312
+ if include_relations
313
+
314
+ if !@strong_relations.blank?
315
+ @strong_relations.each do |strong_relation|
316
+ diory_hash["#{Diograph::ATTRIBUTES['strong_relations']}"] << to_related_hash(strong_relation.diory_id, @strong_relations.index(strong_relation) + 1)
317
+ end
318
+ end
319
+
320
+ if !@weak_relations.blank?
321
+ @weak_relations.each do |weak_relation|
322
+ diory_hash["#{Diograph::ATTRIBUTES['strong_relations']}"] << to_related_hash(weak_relation.diory_id, @weak_relations.index(weak_relation) + 1)
323
+ end
324
+ end
325
+
326
+ end
327
+
328
+ diory_hash
329
+
330
+ end
331
+
332
+
333
+ def to_related_hash(diory_id, order)
334
+ related_hash = {}
335
+ related_hash["diory"] = { Diograph::ATTRIBUTES["diory_id"] => diory_id }
336
+ related_hash["order"] = order
337
+ related_hash["position"] = {"width" => "25%", "padding-bottom" => "15%"}
338
+ related_hash
339
+ end
340
+
341
+
342
+ def to_diograph
343
+ Diograph.to_diograph(self.to_diograph_hash)
344
+ end
345
+
346
+
347
+ end
348
+
349
+
350
+
351
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diograph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jouni Alanen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Libraries for dealing with diographs and diories in Personal Web applications
14
+ email: jouni.alanen@iki.fi
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/diograph.rb
20
+ homepage: http://personalweb.me
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.2.2
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Diograph and diory libraries for Personal Web
43
+ test_files: []