class Lono::Importer::Service::Coder

Public Class Methods

new(template, options={}) click to toggle source
# File lib/lono/importer/service/coder.rb, line 6
def initialize(template, options={})
  @template, @options = template, options
end

Public Instance Methods

translate() click to toggle source
# 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

Private Instance Methods

lono_command() click to toggle source
# File lib/lono/importer/service/coder.rb, line 78
def lono_command
  "#{$0} #{ARGV.join(' ')}"
end
net_http_client(url) click to toggle source
# File lib/lono/importer/service/coder.rb, line 62
def net_http_client(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = http.read_timeout = 30
  http.use_ssl = true if uri.scheme == 'https'
  http
end
net_http_request(url, data) click to toggle source
# File lib/lono/importer/service/coder.rb, line 70
def net_http_request(url, data)
  req = Net::HTTP::Post.new(url) # url includes query string and uri.path does not, must used url
  text = JSON.dump(data)
  req.body = text
  req.content_length = text.bytesize
  req
end
print(data) click to toggle source