Module: OBF::External
- Defined in:
- lib/obf/external.rb
Class Method Summary collapse
- .from_obf(obf_json_or_path, opts) ⇒ Object
- .from_obz(obz_path, opts) ⇒ Object
- .to_obf(hash, dest_path, path_hash = nil) ⇒ Object
- .to_obz(content, dest_path, opts) ⇒ Object
- .to_pdf(board, dest_path, opts) ⇒ Object
- .to_png(board, dest_path) ⇒ Object
Class Method Details
.from_obf(obf_json_or_path, opts) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/obf/external.rb', line 149 def self.from_obf(obf_json_or_path, opts) obj = obf_json_or_path if obj.is_a?(String) obj = OBF::Utils.parse_obf(File.read(obf_json_or_path)) else obj = OBF::Utils.parse_obf(obf_json_or_path) end ['images', 'sounds'].each do |type| obj[type].each do |item| item['data_or_url'] = item['data'] if !item['data_or_url'] && item['path'] && opts['zipper'] content_type = item['content_type'] data = opts['zipper'].read(item['path']) str = "data:" + content_type str += ";base64," + Base64.strict_encode64(data) record = klass.create(:user => opts['user']) item['data_or_url'] = str end item['data_or_url'] ||= item['url'] if item['path'] opts[list] ||= {} opts[list][item['path']] ||= item end end end obj['license'] = OBF::Utils.parse_license(obj['license']) obj # raise "user required" unless opts['user'] # raise "missing id" unless obj['id'] # # hashes = {} # [['images_hash', ButtonImage], ['sounds_hash', ButtonSound]].each do |list, klass| # obj[list].each do |id, item| # record = nil # unique_id = obj['id'] + "_" + item['id'].to_s # if opts[list] && opts[list][unique_id] # record = klass.find_by_global_id(opts[list][unique_id]) # elsif item['data'] # record = klass.create(:user => opts['user']) # item['ref_url'] = item['data'] # elsif item['path'] && opts['zipper'] # content_type = item['content_type'] # data = opts['zipper'].read(item['path']) # str = "data:" + content_type # str += ";base64," + Base64.strict_encode64(data) # record = klass.create(:user => opts['user']) # item['ref_url'] = str # elsif item['url'] # record = klass.create(:user => opts['user']) # item['ref_url'] = item['url'] # end # if record # item.delete('data') # item.delete('url') # record.process(item) # record.upload_to_remote(item['ref_url']) if item['ref_url'] # opts[list] ||= {} # opts[list][unique_id] = record.global_id # hashes[item['id']] = record.global_id # end # end # end # params = {} # non_user_params = {'user' => opts['user']} # params['name'] = obj['name'] # params['description'] = obj['description_html'] # params['image_url'] = obj['image_url'] # params['license'] = OBF::Utils.parse_license(obj['license']) # params['buttons'] = obj['buttons'].map do |button| # new_button = { # 'id' => button['id'], # 'label' => button['label'], # 'vocalization' => button['vocalization'], # 'left' => button['left'], # 'top' => button['top'], # 'width' => button['width'], # 'height' => button['height'], # 'border_color' => button['border_color'], # 'background_color' => button['background_color'] # } # if button['image_id'] # new_button['image_id'] = hashes[button['image_id']] # end # if button['sound_id'] # new_button['sound_id'] = hashes[button['sound_id']] # end # if button['load_board'] # if opts['boards'] && opts['boards'][button['load_board']['id']] # new_button['load_board'] = opts['boards'][button['load_board']['id']] # else # link = Board.find_by_path(button['load_board']['key'] || button['load_board']['id']) # if link # new_button['load_board'] = { # 'id' => link.global_id, # 'key' => link.key # } # end # end # elsif button['url'] # if button['ext_coughdrop_apps'] # new_button['apps'] = button['ext_coughdrop_apps'] # else # new_button['url'] = button['url'] # end # end # new_button # end # params['grid'] = obj['grid'] # params['public'] = !(obj['settings'] && obj['settings']['private']) # non_user_params[:key] = (obj['settings'] && obj['settings']['key']) # board = nil # if opts['boards'] && opts['boards'][obj['id']] # board = Board.find_by_path(opts['boards'][obj['id']]['id']) || Board.find_by_path(opts['boards'][obj['id']]['key']) # board.process(params, non_user_params) # else # board = Board.process_new(params, non_user_params) # opts['boards'] ||= {} # opts['boards'][obj['id']] = { # 'id' => board.global_id, # 'key' => board.key # } # end # board # end |
.from_obz(obz_path, opts) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/obf/external.rb', line 316 def self.from_obz(obz_path, opts) boards = [] images = [] sounds = [] OBF::Utils.load_zip(obz_path) do |zipper| obf_opts = {'zipper' => zipper, 'images' => {}, 'sounds' => {}, 'boards' => {}} manifest = JSON.parse(zipper.read('manifest.json')) root = manifest['root'] board = OBF::Utils.parse_obf(zipper.read(root)) board['path'] = root unvisited_boards = [board] visited_boards = [] while unvisited_boards.length > 0 board_object = unvisited_boards.shift board_object['id'] ||= rand(9999).to_s + Time.now.to_i.to_s visited_boards << board_object board_object['buttons'].each do || if ['load_board'] all_boards = visited_boards + unvisited_boards if all_boards.none?{|b| b['id'] == ['load_board']['id'] || b['path'] == ['load_board']['path'] } path = ['load_board']['path'] || manifest[['load_board']['id']] b = OBF::Utils.parse_obf(zipper.read(path)) b['path'] = path unvisited_boards << b end end end end visited_boards.each do |board_object| res = from_obf(board_object, obf_opts) images += res['images'] || [] sounds += res['sounds'] || [] boards << res end end images.uniq! sounds.uniq! raise "image ids must be present and unique" unless images.map{|i| i['id'] }.uniq.length == images.length raise "sound ids must be present and unique" unless sounds.map{|i| i['id'] }.uniq.length == sounds.length # TODO: try to fix the problem where multiple images or sounds have the same id -- # this involves reaching in and updating image and sound references on generated boards.. return { 'boards' => boards, 'images' => images, 'sounds' => sounds } end |
.to_obf(hash, dest_path, path_hash = nil) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/obf/external.rb', line 2 def self.to_obf(hash, dest_path, path_hash=nil) res = OBF::Utils.obf_shell res['id'] = hash['id'] res['name'] = hash['name'] res['default_layout'] = hash['default_layout'] || 'landscape' res['url'] = hash['url'] res['data_url'] = hash['data_url'] res['description_html'] = hash['description_html'] res['license'] = OBF::Utils.parse_license(hash['license']) res['settings'] = { 'private' => !!(hash['settings'] && hash['settings']['private']) } grid = [] images = [] sounds = [] res['buttons'] = [] = hash['buttons'] #board.settings['buttons'] = .length .each_with_index do |, idx| = { 'id' => ['id'], 'label' => ['label'], 'vocalization' => ['vocalization'], 'left' => ['left'], 'top' => ['top'], 'width' => ['width'], 'height' => ['height'], 'border_color' => ['border_color'] || "#aaa", 'background_color' => ['background_color'] || "#fff" } if ['load_board'] ['load_board'] = { 'id' => ['load_board']['id'], 'url' => ['load_board']['url'], 'data_url' => ['load_board']['data_url'] } if path_hash && path_hash['included_boards'][['load_board']['id']] ['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf" end end if ['url'] ['url'] = ['url'] end .each do|key, val| if key.match(/^ext_/) [key] = val end end if ['image_id'] && hash['images'] image = hash['images'].detect{|i| i['id'] == ['image_id']} if image images << image ['image_id'] = image['id'] end end if ['sound_id'] sound = hash['sounds'].detect{|s| s['id'] == ['sound_id']} if sound sounds << sound ['sound_id'] = sound['id'] end end res['buttons'] << OBF::Utils.update_current_progress(idx.to_f / .to_f) end images.each do |original_image| image = { 'id' => original_image['id'], 'width' => original_image['width'], 'height' => original_image['height'], 'license' => OBF::Utils.parse_license(original_image['license']), 'url' => original_image['url'], 'data_url' => original_image['data_url'], 'content_type' => original_image['content_type'] } if !path_hash image['data'] = OBF::Utils.image_base64(image['url']) else if path_hash['images'] && path_hash['images'][image['id']] image['path'] = path_hash['images'][image['id']]['path'] else image_fetch = OBF::Utils.image_raw(image['url']) if image_fetch zip_path = "images/image_#{image['id']}#{image_fetch['extension']}" path_hash['images'] ||= {} path_hash['images'][image['id']] = { 'path' => zip_path } path_hash['zip'].add(zip_path, image_fetch['data']) image['path'] = zip_path end end end res['images'] << image end sounds.each do |original_sound| sound = { 'id' => original_sound['id'], 'duration' => original_sound['duration'], 'license' => OBF::Utils.parse_license(original_sound['license']), 'url' => original_sound['url'], 'data_url' => original_sound['data_url'], 'content_type' => original_sound['content_type'] } if !path_hash sound['data'] = OBF::Utils.sound_base64(sound['url']) else if path_hash['sounds'] && path_hash['sounds'][sound['id']] sound['path'] = path_hash['sounds'][sound['id']]['path'] else sound_fetch = OBF::Utils.sound_raw(sound['url']) if sound_fetch zip_path = "sounds/sound_#{sound['id']}#{sound_fetch['extension']}" path_hash['sounds'] ||= {} path_hash['sounds'][sound['id']] = { 'path' => zip_path } path_hash['zip'].add(zip_path, sound_fetch['data']) sound['path'] = zip_path end end sound['path'] = zip_path end res['sounds'] << sound end res['grid'] = OBF::Utils.parse_grid(hash['grid']) # TODO: more robust parsing here if path_hash zip_path = "board_#{res['id']}.obf" path_hash['boards'] ||= {} path_hash['boards'][res['id']] = { 'path' => zip_path } path_hash['zip'].add(zip_path, JSON.pretty_generate(res)) else File.open(dest_path, 'w') {|f| f.write(JSON.pretty_generate(res)) } end return dest_path end |
.to_obz(content, dest_path, opts) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/obf/external.rb', line 280 def self.to_obz(content, dest_path, opts) paths = {} boards = content['boards'] content['images'] ||= boards.map{|b| b['images'] }.flatten.uniq content['sounds'] ||= boards.map{|b| b['sounds'] }.flatten.uniq root_board = boards[0] OBF::Utils.build_zip(dest_path) do |zipper| paths['zip'] = zipper paths['included_boards'] = {} boards.each do |b| paths['included_boards'][b['id']] = b end boards.each do |b| b = paths['included_boards'][b['id']] if b b['images'] = content['images'] || [] b['sounds'] = content['sounds'] || [] to_obf(b, nil, paths) end end manifest = { 'root' => paths['boards'][root_board['id']]['path'], 'paths' => {} } ['images', 'sounds', 'boards'].each do |type| manifest['paths'][type] = {} (paths[type] || {}).each do |id, opts| manifest['paths'][type][id] = opts['path'] end end zipper.add('manifest.json', JSON.pretty_generate(manifest)) end return dest_path end |
.to_pdf(board, dest_path, opts) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/obf/external.rb', line 365 def self.to_pdf(board, dest_path, opts) tmp_path = OBF::Utils.temp_path("stash") if opts['packet'] OBF::Utils.as_progress_percent(0, 0.3) do OBF::External.to_obz(board, tmp_path, opts) end OBF::Utils.as_progress_percent(0.3, 1.0) do OBF::OBZ.to_pdf(tmp_path, dest_path) end else OBF::Utils.as_progress_percent(0, 0.5) do self.to_obf(board, tmp_path) end OBF::Utils.as_progress_percent(0.5, 1.0) do OBF::OBF.to_pdf(tmp_path, dest_path) end end File.unlink(tmp_path) if File.exist?(tmp_path) dest_path end |
.to_png(board, dest_path) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/obf/external.rb', line 386 def self.to_png(board, dest_path) tmp_path = OBF::Utils.temp_path("stash") OBF::Utils.as_progress_percent(0, 0.5) do self.to_pdf(board, tmp_path) end OBF::Utils.as_progress_percent(0.5, 1.0) do OBF::PDF.to_png(tmp_path, dest_path) end File.unlink(tmp_path) if File.exist?(tmp_path) dest_path end |