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.



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

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

Instance Attribute Details

#hamlObject

images, stylesheets and javascripts as file path arrays haml file as string, fullpath to main.haml thumbnail as fullpath to image, will be renamed to default.png



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

def haml
  @haml
end

#imagesObject

images, stylesheets and javascripts as file path arrays haml file as string, fullpath to main.haml thumbnail as fullpath to image, will be renamed to default.png



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

def images
  @images
end

#javascriptsObject

images, stylesheets and javascripts as file path arrays haml file as string, fullpath to main.haml thumbnail as fullpath to image, will be renamed to default.png



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

def javascripts
  @javascripts
end

#stylesheetsObject

images, stylesheets and javascripts as file path arrays haml file as string, fullpath to main.haml thumbnail as fullpath to image, will be renamed to default.png



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

def stylesheets
  @stylesheets
end

#thumbnailObject

images, stylesheets and javascripts as file path arrays haml file as string, fullpath to main.haml thumbnail as fullpath to image, will be renamed to default.png



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

def thumbnail
  @thumbnail
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.



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

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 ) }

	# copy thumbnail to ./default.png
	FileUtils.cp("#{Rails.root}/public#{thumbnail.split('?')[0]}", "#{path}/default.png") if thumbnail

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