obf 0.2 → 0.2.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 +4 -4
  2. data/lib/obf/external.rb +89 -79
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f63706c374446dc89b390e990f6fece72dca9d2
4
- data.tar.gz: 4c09d0f858322855b2093e0cfc5d48ba94dba4be
3
+ metadata.gz: a8a99d902b0dc9ed0a272dc723729cb754cf94ca
4
+ data.tar.gz: 0b5daf60c141328401c47b0fc57e940ee4da743b
5
5
  SHA512:
6
- metadata.gz: 17a5e56e9eb06b34ac9c5c57d5343a80afac3863e2f2bf064b0725ca80ec0abc9dfbf3fd974a2fa089c6f6ea258f77ee3ce5fdd73217d187a817320f8fdb4c62
7
- data.tar.gz: 551537ce747367b3b8f4ec516cff4b5fc7287bb0742ee9be490450f5a61221c827c46672b6816a90bb4f5266a663bd5be0e69508d785059ce11ab1685f041247
6
+ metadata.gz: a87fc4b8e6632f2ed4921c9824a8feae1d59f40c749080293d122e09941edd7a53c007e185f0205bd149cdf21a0c00a01c71e30602af55b3c94d956a4ba48d80
7
+ data.tar.gz: 70f62b9f1c68d0377c445bdb875ec45309b5cb3e2136e5d75de1553969f002591805503ba4b47ce5f515097b94246a86650f5ebd04c1e0fd036cecf26b43f12f
data/lib/obf/external.rb CHANGED
@@ -13,8 +13,63 @@ module OBF::External
13
13
  }
14
14
  grid = []
15
15
 
16
- res['images'] = []
17
- (hash['images'] || []).each do |original_image|
16
+ images = []
17
+ sounds = []
18
+
19
+ res['buttons'] = []
20
+ buttons = hash['buttons'] #board.settings['buttons']
21
+ button_count = buttons.length
22
+
23
+ buttons.each_with_index do |original_button, idx|
24
+ button = {
25
+ 'id' => original_button['id'],
26
+ 'label' => original_button['label'],
27
+ 'vocalization' => original_button['vocalization'],
28
+ 'left' => original_button['left'],
29
+ 'top' => original_button['top'],
30
+ 'width' => original_button['width'],
31
+ 'height' => original_button['height'],
32
+ 'border_color' => original_button['border_color'] || "#aaa",
33
+ 'background_color' => original_button['background_color'] || "#fff"
34
+ }
35
+ if original_button['load_board']
36
+ button['load_board'] = {
37
+ 'id' => original_button['load_board']['id'],
38
+ 'url' => original_button['load_board']['url'],
39
+ 'data_url' => original_button['load_board']['data_url']
40
+ }
41
+ if path_hash && path_hash['included_boards'][original_button['load_board']['id']]
42
+ button['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf"
43
+ end
44
+ end
45
+ if original_button['url']
46
+ button['url'] = original_button['url']
47
+ end
48
+ original_button.each do|key, val|
49
+ if key.match(/^ext_/)
50
+ button[key] = val
51
+ end
52
+ end
53
+
54
+ if original_button['image_id'] && hash['images']
55
+ image = hash['images'].detect{|i| i['id'] == original_button['image_id']}
56
+ if image
57
+ images << image
58
+ button['image_id'] = image['id']
59
+ end
60
+ end
61
+ if original_button['sound_id']
62
+ sound = hash['sounds'].detect{|s| s['id'] == original_button['sound_id']}
63
+ if sound
64
+ sounds << sound
65
+ button['sound_id'] = sound['id']
66
+ end
67
+ end
68
+ res['buttons'] << button
69
+ OBF::Utils.update_current_progress(idx.to_f / button_count.to_f)
70
+ end
71
+
72
+ images.each do |original_image|
18
73
  image = {
19
74
  'id' => original_image['id'],
20
75
  'width' => original_image['width'],
@@ -45,8 +100,7 @@ module OBF::External
45
100
  res['images'] << image
46
101
  end
47
102
 
48
- res['sounds'] = []
49
- (hash['sounds'] || []).each do |original_sound|
103
+ sounds.each do |original_sound|
50
104
  sound = {
51
105
  'id' => original_sound['id'],
52
106
  'duration' => original_sound['duration'],
@@ -77,62 +131,7 @@ module OBF::External
77
131
 
78
132
  res['sounds'] << sound
79
133
  end
80
-
81
- res['buttons'] = []
82
- buttons = hash['buttons'] #board.settings['buttons']
83
- button_count = buttons.length
84
-
85
- buttons.each_with_index do |original_button, idx|
86
- button = {
87
- 'id' => original_button['id'],
88
- 'label' => original_button['label'],
89
- 'vocalization' => original_button['vocalization'],
90
- 'left' => original_button['left'],
91
- 'top' => original_button['top'],
92
- 'width' => original_button['width'],
93
- 'height' => original_button['height'],
94
- 'border_color' => original_button['border_color'] || "#aaa",
95
- 'background_color' => original_button['background_color'] || "#fff"
96
- }
97
- if original_button['load_board']
98
- button['load_board'] = {
99
- 'id' => original_button['load_board']['id'],
100
- 'url' => original_button['load_board']['url'],
101
- 'data_url' => original_button['load_board']['data_url']
102
- }
103
- if path_hash && path_hash['included_boards'][original_button['load_board']['id']]
104
- button['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf"
105
- end
106
- end
107
- if original_button['url']
108
- button['url'] = original_button['url']
109
- end
110
- original_button.each do|key, val|
111
- if key.match(/^ext_/)
112
- button[key] = val
113
- end
114
- end
115
- # if original_button['apps']
116
- # button['ext_coughdrop_apps'] = original_button['apps']
117
- # if original_button['apps']['web'] && original_button['apps']['web']['launch_url']
118
- # button['url'] = original_button['apps']['web']['launch_url']
119
- # end
120
- # end
121
- if original_button['image_id'] && hash['images']
122
- image = res['images'].detect{|i| i['id'] == original_button['image_id']}
123
- if image
124
- button['image_id'] = image['id']
125
- end
126
- end
127
- if original_button['sound_id']
128
- sound = res['sounds'].detect{|s| s['id'] == original_button['sound_id']}
129
- if sound
130
- button['sound_id'] = sound['id']
131
- end
132
- end
133
- res['buttons'] << button
134
- OBF::Utils.update_current_progress(idx.to_f / button_count.to_f)
135
- end
134
+
136
135
  res['grid'] = OBF::Utils.parse_grid(hash['grid']) # TODO: more robust parsing here
137
136
  if path_hash
138
137
  zip_path = "board_#{res['id']}.obf"
@@ -167,6 +166,10 @@ module OBF::External
167
166
  item['data_or_url'] = str
168
167
  end
169
168
  item['data_or_url'] ||= item['url']
169
+ if item['path']
170
+ opts[list] ||= {}
171
+ opts[list][item['path']] ||= item
172
+ end
170
173
  end
171
174
  end
172
175
 
@@ -274,31 +277,24 @@ module OBF::External
274
277
 
275
278
  end
276
279
 
277
- def self.to_obz(boards, dest_path, opts)
280
+ def self.to_obz(content, dest_path, opts)
278
281
  paths = {}
282
+ boards = content['boards']
279
283
  root_board = boards[0]
280
284
  OBF::Utils.build_zip(dest_path) do |zipper|
281
285
  paths['zip'] = zipper
282
- # board.track_downstream_boards!
283
286
  paths['included_boards'] = {}
284
287
  boards.each do |b|
285
288
  paths['included_boards'][b['id']] = b
286
289
  end
287
290
  boards.each do |b|
288
291
  b = paths['included_boards'][b['id']]
289
- to_obf(b, nil, paths) if b
292
+ if b
293
+ b['images'] = content['images'] || []
294
+ b['sounds'] = content['sounds'] || []
295
+ to_obf(b, nil, paths)
296
+ end
290
297
  end
291
- # board.settings['downstream_board_ids'].each do |id|
292
- # b = Board.find_by_path(id)
293
- # if b.allows?(opts['user'], 'view')
294
- # paths['included_boards'][id] = b
295
- # end
296
- # end
297
- # to_obf(board, nil, paths)
298
- # board.settings['downstream_board_ids'].each do |id|
299
- # b = paths['included_boards'][id]
300
- # to_obf(b, nil, paths) if b
301
- # end
302
298
  manifest = {
303
299
  'root' => paths['boards'][root_board['id']]['path'],
304
300
  'paths' => {}
@@ -316,7 +312,10 @@ module OBF::External
316
312
  end
317
313
 
318
314
  def self.from_obz(obz_path, opts)
319
- result = []
315
+ boards = []
316
+ images = []
317
+ sounds = []
318
+ obf_opts = {'zipper' => zipper, 'images' => {}, 'sounds' => {}, 'boards' => {}}
320
319
  OBF::Utils.load_zip(obz_path) do |zipper|
321
320
  manifest = JSON.parse(zipper.read('manifest.json'))
322
321
  root = manifest['root']
@@ -324,8 +323,6 @@ module OBF::External
324
323
  board['path'] = root
325
324
  unvisited_boards = [board]
326
325
  visited_boards = []
327
- obf_opts = {'zipper' => zipper, 'images' => {}, 'sounds' => {}, 'boards' => {}}
328
- # obf_opts = {'user' => opts['user'], 'zipper' => 'zipper', 'images' => {}, 'sounds' => {}, 'boards' => {}}
329
326
  while unvisited_boards.length > 0
330
327
  board_object = unvisited_boards.shift
331
328
  board_object['id'] ||= rand(9999).to_s + Time.now.to_i.to_s
@@ -344,10 +341,23 @@ module OBF::External
344
341
  end
345
342
  end
346
343
  visited_boards.each do |board_object|
347
- result << from_obf(board_object, obf_opts)
344
+ res = from_obf(board_object, obf_opts)
345
+ images += res['images'] || []
346
+ sounds += res['sounds'] || []
347
+ boards << res
348
348
  end
349
349
  end
350
- return result
350
+ images.uniq!
351
+ sounds.uniq!
352
+ raise "image ids must be present and unique" unless images.map{|i| i['id'] }.uniq.length == images.length
353
+ raise "sound ids must be present and unique" unless sounds.map{|i| i['id'] }.uniq.length == sounds.length
354
+ # TODO: try to fix the problem where multiple images or sounds have the same id --
355
+ # this involves reaching in and updating image and sound references on generated boards..
356
+ return {
357
+ 'boards' => result,
358
+ 'images' => images,
359
+ 'sounds' => sounds
360
+ }
351
361
  end
352
362
 
353
363
  def self.to_pdf(board, dest_path, opts)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obf
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Whitmer