Class: WidgetBundle

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/app/models/widget_bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ WidgetBundle

Returns a new instance of WidgetBundle.



22
23
24
25
26
# File 'lib/app/models/widget_bundle.rb', line 22

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Instance Attribute Details

#hamlObject

images, stylesheets and javascripts as file paths haml file as string, fullpath to main.haml



19
20
21
# File 'lib/app/models/widget_bundle.rb', line 19

def haml
  @haml
end

#imagesObject

images, stylesheets and javascripts as file paths haml file as string, fullpath to main.haml



19
20
21
# File 'lib/app/models/widget_bundle.rb', line 19

def images
  @images
end

#javascriptsObject

images, stylesheets and javascripts as file paths haml file as string, fullpath to main.haml



19
20
21
# File 'lib/app/models/widget_bundle.rb', line 19

def javascripts
  @javascripts
end

#stylesheetsObject

images, stylesheets and javascripts as file paths haml file as string, fullpath to main.haml



19
20
21
# File 'lib/app/models/widget_bundle.rb', line 19

def stylesheets
  @stylesheets
end

Instance Method Details

#package(display_name, bundle_identifier, bundle_name, root_path = Dir.tmpdir) ⇒ Object

Package the widget contents into a folder for download root_path: specify path to use, default is Rails app tmpdir. Implemented this for testing.



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
# File 'lib/app/models/widget_bundle.rb', line 30

def package(display_name, bundle_identifier, bundle_name, root_path = Dir.tmpdir)

  return false unless valid?

  path = File.expand_path "#{root_path}/#{Time.now.to_i}#{rand(1000)}.wdgt/"
  FileUtils.mkdir_p path

  # make requred directories
  images_dir = path
  css_js_dir = FileUtils.mkdir("#{path}/assets/")[0]

  # copy images
  #  TODO: tried doing this all with FileUtils.cp_r but couldn't get it to work properly, had to use this block instead
  if images
    images.each do |i|
      file = "#{Rails.root}/public#{i}".split("?")[0]
      dir = "#{images_dir}/#{File.dirname(i)}".gsub('//', '/')
      FileUtils.mkdir_p dir
      FileUtils.cp file, dir
    end
  end

  # minify stylesheets
  minify_stylesheets(stylesheets, css_js_dir)

  # minify javascripts
  minify_javascripts(javascripts, css_js_dir)

  # create Info.plist
  File.open("#{path}/Info.plist", "w") { |file| file.write(plist_content(display_name, bundle_identifier, bundle_name)) }

  # create main.html
  # compiled_haml = `haml #{haml}`
  File.open("#{path}/main.html", "w") { |file| file.write( haml ) }

  # compress the *.wdgt folder and return the full path to the zip archive
  compress(path) 
end