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, opts) ⇒ Object
Class Method Details
.from_obf(obf_json_or_path, opts) ⇒ Object
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 |
# File 'lib/obf/external.rb', line 163 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 end |
.from_obz(obz_path, opts) ⇒ Object
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 279 280 281 282 283 284 285 286 287 |
# File 'lib/obf/external.rb', line 240 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/obf/external.rb', line 2 def self.to_obf(hash, dest_path, path_hash=nil) if hash['boards'] old_hash = hash hash = old_hash['boards'][0] hash['images'] = old_hash['images'] || [] hash['sounds'] = old_hash['sounds'] || [] path_hash = nil end res = OBF::Utils.obf_shell res['id'] = hash['id'] res['locale'] = hash['locale'] || 'en' res['format'] = 'open-board-0.1' 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']) hash.each do |key, val| if key && key.match(/^ext_/) res[key] = val end end grid = [] images = [] sounds = [] res['buttons'] = [] = hash['buttons'] #board.settings['buttons'] = .length .each_with_index do |, idx| = { 'id' => ['id'], 'label' => ['label'], 'vocalization' => ['vocalization'], 'action' => ['action'], 'left' => ['left'], 'top' => ['top'], 'width' => ['width'], 'height' => ['height'], 'border_color' => OBF::Utils.fix_color(['border_color'] || "#aaa", 'rgb'), 'background_color' => OBF::Utils.fix_color(['background_color'] || "#fff", 'rgb') } 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'] && path_hash['included_boards'][['load_board']['id']] ['load_board']['path'] = "board_#{['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' => original_image['data'], 'data_url' => original_image['data_url'], 'content_type' => original_image['content_type'] } if !path_hash image['data'] = OBF::Utils.image_base64(image['url']) if 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'] || image['data']) 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'] || sound['data']) 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
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 |
# File 'lib/obf/external.rb', line 194 def self.to_obz(content, dest_path, opts) if content['id'] old_content = content content = { 'boards' => [old_content], 'images' => old_content['images'] || [], 'sounds' => old_content['sounds'] || [] } end 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 = { 'format' => 'open-board-0.1', '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
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/obf/external.rb', line 289 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, opts) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/obf/external.rb', line 310 def self.to_png(board, dest_path, opts) tmp_path = OBF::Utils.temp_path("stash") OBF::Utils.as_progress_percent(0, 0.5) do self.to_pdf(board, tmp_path, opts) 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 |