Class: Lono::Importer::Service::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/importer/service/coder.rb

Instance Method Summary collapse

Constructor Details

#initialize(template, options = {}) ⇒ Coder

Returns a new instance of Coder.



6
7
8
# File 'lib/lono/importer/service/coder.rb', line 6

def initialize(template, options={})
  @template, @options = template, options
end

Instance Method Details

#translateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lono/importer/service/coder.rb', line 10

def translate
  url = "#{Lono::API}/code"
  http = net_http_client(url)
  req = net_http_request(url,
    template: Base64.encode64(@template), # Base64 JSON for special chars that Rack::LintWrapper cannot process
    lono_version: Lono::VERSION,
    lono_command: lono_command,
  )
  res = http.request(req) # send request

  if res.code == "200"
    data = JSON.load(res.body)
    ruby_code = print(data) # returns data["ruby_code"] / passthrough
    ruby_code
  else
    puts "Error: Unable to convert template to Ruby code."
    puts "The error has been reported."
    puts "Non-successful http response status code: #{res.code}"
    # puts "headers: #{res.each_header.to_h.inspect}"
    exit 1
  end
end