Module: JSCompiler
- Defined in:
- lib/jsc.rb,
lib/jsc/closure_compiler.rb
Constant Summary collapse
- VERSION =
:stopdoc:
'0.2.1'
Class Method Summary collapse
-
.compile(arg, is_file, op, level) ⇒ Object
Compiles a file or a piece of code and returns parsed output.
-
.compile_dir(dir, op, level) ⇒ Object
Calls compile method for every file in dir directory.
-
.create_json_request(code) ⇒ Object
Creates the JSON hash for the request and returns the hash to send along with the request.
-
.create_statistics_output(result) ⇒ Object
Parses and returns the result JSON server response.
-
.full_compile(arg, file, level) ⇒ Object
Compiles a file or a piece of code and returns parsed output if no errors or warnings are found.
-
.libpath(*args) ⇒ Object
Returns the library path for the module.
-
.parse_json_output(response) ⇒ Object
Parses and returns JSON server response.
-
.path(*args) ⇒ Object
Returns the lpath for the module.
-
.post_to_cc(data) ⇒ Object
Sends the JSON request data hash to Google service and returns its response.
-
.read_file(file_name) ⇒ Object
Reads file and returns its content.
-
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in.
-
.version ⇒ Object
Returns the version string for the library.
Class Method Details
.compile(arg, is_file, op, level) ⇒ Object
Compiles a file or a piece of code and returns parsed output
Accepted parameters:
-
arg: the code or the file path to compile
-
is_file: 0 => arg is code
1 => arg is a file path -
op: output_info parameter
-
level: compilation_level parameter
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jsc/closure_compiler.rb', line 55 def compile(arg, is_file, op, level) @op = op.blank? ? DEFAULT_SERVICE : op @level = level.blank? ? DEFAULT_LEVEL : level value = true begin if is_file js_code = read_file(arg) else js_code = arg end resp, data = post_to_cc(create_json_request(js_code)) rescue StandardError => e return e end parse_json_output(data) end |
.compile_dir(dir, op, level) ⇒ Object
Calls compile method for every file in dir directory
Accepted parameters:
-
dir: the directory
-
op: output_info parameter
-
level: compilation_level parameter
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jsc/closure_compiler.rb', line 95 def compile_dir(dir, op, level) out = "" Dir.entries(dir).each do |file| if File.extname(file) == ".js" out << "Statistics for file #{file}...\n" out << compile(file, true, op, level) + "\n***************\n" end end return out end |
.create_json_request(code) ⇒ Object
Creates the JSON hash for the request and returns the hash to send along with the request
Accepted parameters:
-
code: json_code parameter
-
level: compilation_level parameter
25 26 27 28 29 30 31 32 |
# File 'lib/jsc/closure_compiler.rb', line 25 def create_json_request(code) parameters = { "code" => code, "level" => @level, "format" => "json", "info" => @op } end |
.create_statistics_output(result) ⇒ Object
Parses and returns the result JSON server response
Accepted parameters:
-
result: the already parsed JSON server response
166 167 168 169 170 171 172 173 174 |
# File 'lib/jsc/closure_compiler.rb', line 166 def create_statistics_output(result) size_improvement = result['originalSize'] - result['compressedSize'] size_gzip_improvement = result['originalGzipSize'] - result['compressedGzipSize'] rate_improvement = (size_improvement * 100)/result['originalSize'] rate_gzip_improvement = (size_gzip_improvement * 100)/result['originalGzipSize'] out = "Original Size: #{result['originalSize']} bytes (#{result['originalGzipSize']} bytes gzipped) \n" out << "Compiled Size: #{result['compressedSize']} bytes (#{result['compressedGzipSize']} bytes gzipped) \n" out << "\t Saved #{rate_improvement}% off the original size (#{rate_gzip_improvement}% off the gzipped size)" end |
.full_compile(arg, file, level) ⇒ Object
Compiles a file or a piece of code and returns parsed output if no errors or warnings are found
Accepted parameters:
-
arg: the code or the file path to compile
-
file: 0 => arg is code
1 => arg is a file path -
level: compilation_level parameter
82 83 84 85 86 87 |
# File 'lib/jsc/closure_compiler.rb', line 82 def full_compile(arg, file, level) ['errors', 'warnings','compiled_code'].each do |x| str = JSCompiler.compile(arg, file, x, level) return str unless str.eql?("No " + x) end end |
.libpath(*args) ⇒ Object
Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.
19 20 21 |
# File 'lib/jsc.rb', line 19 def self.libpath( *args ) args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten) end |
.parse_json_output(response) ⇒ Object
Parses and returns JSON server response
Accepted parameters:
-
response: the server response
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/jsc/closure_compiler.rb', line 110 def parse_json_output(response) out = "" parsed_response = ActiveSupport::JSON.decode(response) if parsed_response.has_key?("serverErrors") result = parsed_response['serverErrors'] return "Server Error: #{result[0]['error']} - Error Code: #{result[0]['code']}" end case @op when "compiled_code" out = parsed_response['compiledCode'] when "statistics" result = parsed_response['statistics'] out = create_statistics_output(result) else "errors" #case for errors or warnings begin result = parsed_response[@op] unless result.nil? num = result.size out << "You've got #{result.size} #{@op}\n" i = 0 result.each do || i += 1 out << "\n#{@op.singularize.capitalize} n.#{i}\n" out << "\t#{['type']}: " + [@op.singularize] + " at line #{['lineno']} character #{['charno']}\n" out << "\t" + ['line'] + "\n" unless ['line'].nil? end return out else return "No #{@op}" end rescue out = "Error parsing JSON output...Check your output" end end end |
.path(*args) ⇒ Object
Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.
27 28 29 |
# File 'lib/jsc.rb', line 27 def self.path( *args ) args.empty? ? PATH : ::File.join(PATH, args.flatten) end |
.post_to_cc(data) ⇒ Object
Sends the JSON request data hash to Google service and returns its response
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jsc/closure_compiler.rb', line 36 def post_to_cc(data) post_args = { 'js_code' => data["code"], 'compilation_level' => data["level"], 'output_format' => data["format"], 'output_info' => data["info"] } # send the request resp, data = Net::HTTP.post_form(URI.parse(GOOGLE_SERVICE_ADDRESS), post_args) end |
.read_file(file_name) ⇒ Object
Reads file and returns its content
Accepted parameters:
-
file_name: the absolute path to the file
153 154 155 156 157 158 159 160 |
# File 'lib/jsc/closure_compiler.rb', line 153 def read_file(file_name) begin content = File.open(file_name).read return content rescue raise end end |
.require_all_libs_relative_to(fname, dir = nil) ⇒ Object
Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.
36 37 38 39 40 41 42 |
# File 'lib/jsc.rb', line 36 def self.require_all_libs_relative_to( fname, dir = nil ) dir ||= ::File.basename(fname, '.*') search_me = ::File.( ::File.join(::File.dirname(fname), dir, '**', '*.rb')) Dir.glob(search_me).sort.each {|rb| require rb} end |
.version ⇒ Object
Returns the version string for the library.
11 12 13 |
# File 'lib/jsc.rb', line 11 def self.version VERSION end |