-
-
Save mloughran/622371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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