Class: Coyote::ClosureCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/coyote/closure_compiler.rb

Instance Method Summary collapse

Constructor Details

#initializeClosureCompiler

Returns a new instance of ClosureCompiler.



7
8
9
# File 'lib/coyote/closure_compiler.rb', line 7

def initialize
  @request = Net::HTTP::Post.new('/compile', 'Content-type' => 'application/x-www-form-urlencoded')
end

Instance Method Details

#compile(content) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coyote/closure_compiler.rb', line 11

def compile(content)
  @content = content
  params = {
    'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
    'output_format' => 'xml',
    'output_info' => 'compiled_code',
    'js_code' => @content
  }

  @request.set_form_data(params)
  begin
    res = Net::HTTP.new('closure-compiler.appspot.com', 80).start {|http| http.request(@request) }
    case res
    when Net::HTTPSuccess
      @doc = REXML::Document.new(res.body)
    end
  rescue REXML::ParseException => msg
    print "Failed: #{msg}\n".red
  rescue
    #these should be caught by external checking
  end

  self
end

#compiled_codeObject



48
49
50
# File 'lib/coyote/closure_compiler.rb', line 48

def compiled_code
  @doc.root.elements['compiledCode'].text
end

#errorsObject



40
41
42
43
44
45
46
# File 'lib/coyote/closure_compiler.rb', line 40

def errors
  unless @doc
    nil
  else
    @doc.root.elements["serverErrors"]
  end
end

#file_too_big?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/coyote/closure_compiler.rb', line 52

def file_too_big?
  @doc && @doc.root && @doc.root.elements["serverErrors/error[@code='8']"] != nil
end

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/coyote/closure_compiler.rb', line 36

def success?
  @doc && @doc.root && @doc.root.elements['serverErrors'].nil?
end