Module: Mud::Utils
- Included in:
- Mud
- Defined in:
- lib/mud/utils.rb
Defined Under Namespace
Classes: LocalsBinding
Constant Summary collapse
- JS_DIRECTORY =
File.(File.join File.dirname(__FILE__), '..', '..', 'js')
- ROOT_DIRECTORY =
File.absolute_path('/')
- HOME_DIRECTORY =
Dir.home
Instance Method Summary collapse
- #compile(src, type = 'simple', out = nil) ⇒ Object
- #render(location, opts = {}) ⇒ Object (also: #cat)
Instance Method Details
#compile(src, type = 'simple', out = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mud/utils.rb', line 48 def compile(src, type = 'simple', out = nil) raise ArgumentError.new("Type must be either 'simple' or 'advanced', was '#{type}'") unless ['simple', 'advanced'].include?(type.to_s) level = "#{type.upcase}_OPTIMIZATIONS" response = Net::HTTP.post_form(URI.parse('http://closure-compiler.appspot.com/compile'), :output_info => 'compiled_code', :compilation_level => level, :warning_level => 'default', :js_code => src) if out File.open(out) { |f| f.write(response.body) } end response.body end |
#render(location, opts = {}) ⇒ Object Also known as: cat
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 |
# File 'lib/mud/utils.rb', line 65 def render(location, opts = {}) if location.is_a?(Hash) opts = location else opts = guess(location).update(opts) end type, path = opts.first content = case type when :erb, :file then basepath = opts[:basepath] || path basepath = basepath.gsub(/^file:\/\//, '') path = opts[:basepath] ? File.join(basepath, path) : basepath File.open(path) { |f| f.read } when :http then basepath = opts[:basepath] || path basepath = "http://#{basepath}" unless basepath.start_with?('http://') path = opts[:basepath] ? URI.join(basepath, path) : basepath response = Net::HTTP.get_response(URI.parse(path)) response.error! unless (200..299).include?(response.code.to_i) response.body else raise ArgumentError.new("Unknown type '#{type}'") end if type == :erb locals = opts[:locals] || {} content = ERB.new(content).result(LocalsBinding.new(locals).binding) end content end |