Module: JSCompiler
- Defined in:
- lib/jsc.rb,
lib/jsc/parser.rb,
lib/jsc/closure_compiler.rb
Defined Under Namespace
Modules: Parser
Constant Summary collapse
- VERSION =
:stopdoc:
'0.2.3'
Class Attribute Summary collapse
-
.file ⇒ Object
Returns the value of attribute file.
-
.format_type ⇒ Object
Returns the value of attribute format_type.
-
.level ⇒ Object
Returns the value of attribute level.
-
.op ⇒ Object
Returns the value of attribute op.
Class Method Summary collapse
-
.cleancode(arg, file, level, type) ⇒ Object
Compiles a file or a piece of code and returns parsed output if no errors or warnings are found.
-
.compile(arg, is_file, op, level, type) ⇒ Object
Compiles a file or a piece of code and returns parsed output.
-
.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, type) ⇒ Object
Compiles a file or a piece of code for errors or warnings.
-
.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 Attribute Details
.file ⇒ Object
Returns the value of attribute file.
28 29 30 |
# File 'lib/jsc/closure_compiler.rb', line 28 def file @file end |
.format_type ⇒ Object
Returns the value of attribute format_type.
28 29 30 |
# File 'lib/jsc/closure_compiler.rb', line 28 def format_type @format_type end |
.level ⇒ Object
Returns the value of attribute level.
28 29 30 |
# File 'lib/jsc/closure_compiler.rb', line 28 def level @level end |
.op ⇒ Object
Returns the value of attribute op.
28 29 30 |
# File 'lib/jsc/closure_compiler.rb', line 28 def op @op end |
Class Method Details
.cleancode(arg, file, level, type) ⇒ 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
111 112 113 114 115 116 |
# File 'lib/jsc/closure_compiler.rb', line 111 def cleancode(arg, file, level, type) ['errors', 'warnings', 'compiled_code'].each do |x| str = JSCompiler.compile(arg, file, x, level, type) return str unless str.eql?("No " + x) end end |
.compile(arg, is_file, op, level, type) ⇒ 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
-
type: the type of parsing requested
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/jsc/closure_compiler.rb', line 74 def compile(arg, is_file, op, level, type) self.op = op.blank? ? DEFAULT_SERVICE : op self.level = level.blank? ? DEFAULT_LEVEL : level self.format_type = type.blank? ? DEFAULT_TYPE : type self.file = "" value = true begin if is_file js_code = read_file(arg) self.file = arg else js_code = arg end resp, data = post_to_cc(create_json_request(js_code)) if $debug puts "#DEBUG data \n" p data puts "\n" end rescue StandardError => e return e end parse_json_output(data) 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
36 37 38 39 40 41 42 43 |
# File 'lib/jsc/closure_compiler.rb', line 36 def create_json_request(code) parameters = { "code" => code, "level" => self.level, "format" => "json", "info" => self.op } end |
.create_statistics_output(result) ⇒ Object
Parses and returns the result JSON server response
Accepted parameters:
-
result: the already parsed JSON server response
211 212 213 214 215 216 217 218 219 |
# File 'lib/jsc/closure_compiler.rb', line 211 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, type) ⇒ Object
Compiles a file or a piece of code for errors or warnings. Returns nothing 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
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/jsc/closure_compiler.rb', line 127 def full_compile(arg, file, level, type) errors_log = compile(arg, file, "errors", level, type) str = "" if errors_log.eql?("No errors") warnings_log = compile(arg, file, "warnings", level, type) str = warnings_log unless warnings_log.eql?("No warnings") else str = errors_log end str 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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/jsc/closure_compiler.rb', line 162 def parse_json_output(response) out = "" parsed_response = ActiveSupport::JSON.decode(response) if parsed_response.has_key?("serverErrors") result = parsed_response['serverErrors'] = "Server Error: #{result[0]['error']} \n" << "Error Code: #{result[0]['code']}" return .red # return red, bold, error_message, reset end case self.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[self.op] # call parser # return parse(result) JSCompiler::Parser.parse(result) rescue StandardError => e out = e out << "Error parsing JSON output...Check it." 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jsc/closure_compiler.rb', line 47 def post_to_cc(data) post_args = { 'js_code' => data["code"], 'compilation_level' => data["level"], 'output_format' => data["format"], 'output_info' => data["info"] } if $debug puts "#DEBUG post_args \n" p post_args puts "\n" end # 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
198 199 200 201 202 203 204 205 |
# File 'lib/jsc/closure_compiler.rb', line 198 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 |