class Lono::Pro::Importer::Service::Coder

Constants

LONO_API

Public Class Methods

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

Public Instance Methods

translate() click to toggle source
# File lib/lono/pro/importer/service/coder.rb, line 12
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_pro_version: Lono::Pro::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 "errored"
    puts "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/pro/importer/service/coder.rb, line 83
def lono_command
  "#{$0} #{ARGV.join(' ')}"
end
net_http_client(url) click to toggle source
# File lib/lono/pro/importer/service/coder.rb, line 67
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/pro/importer/service/coder.rb, line 75
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