Quick Ruby Script to the Google Closure API
Closure is a Google tool for intelligently optimising - minifying - your Javascript. There’s a command-line tool, an online version and a REST API.
Here’s a quick Ruby script for accessing the REST API:
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 'httparty' | |
class GoogleClosure | |
include HTTParty | |
base_uri 'closure-compiler.appspot.com' | |
def self.compile(path) | |
contents = File.read(path) | |
params = { | |
:js_code => contents, | |
:compilation_level => 'SIMPLE_OPTIMIZATIONS', | |
:output_format => 'text', | |
:output_info => 'compiled_code', | |
} | |
post('/compile', | |
:body => params, | |
:headers => { | |
'Content-type' => 'application/x-www-form-urlencoded' | |
} | |
) | |
end | |
end | |
File.open('public/javascripts/bookmarklet.min.js', 'w') do |f| | |
f.write GoogleClosure.compile('public/javascripts/bookmarklet.js') | |
end |