Skip to content

Instantly share code, notes, and snippets.

@mloughran
Forked from benpickles/gist:229403
Created October 12, 2010 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloughran/622371 to your computer and use it in GitHub Desktop.
Save mloughran/622371 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
class GoogleClosure
class Error < RuntimeError; end
HOST = URI.parse('http://closure-compiler.appspot.com/compile')
def self.compile(contents)
response = Net::HTTP.post_form(HOST, {
:js_code => contents,
:compilation_level => 'SIMPLE_OPTIMIZATIONS',
:output_format => 'json',
:output_info => 'compiled_code',
})
case response
when Net::HTTPSuccess
info = JSON.parse(response.body)
if info["serverErrors"]
raise Error, "Errors returned from closure-compiler service: #{info["serverErrors"].inspect}"
else
return info["compiledCode"]
end
else
raise Error, "Unexpected response from closure-compiler service: #{response.class}"
end
end
end
p GoogleClosure.compile("var f = function() { var foo = 76; return foo;}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment