module GcfRuby

Main class for prediction

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/gcf_ruby.rb, line 71
def self.configure(&block)
  self.configuration ||= Configuration.new
  yield configuration
end
predict(model_name, params) click to toggle source
# File lib/gcf_ruby.rb, line 16
def predict(model_name, params)
  post(model_name, formatted_params(params))
end
predict_async(model_name, params, webhook_url) click to toggle source
# File lib/gcf_ruby.rb, line 20
def predict_async(model_name, params, webhook_url)
  post(model_name, formatted_params(params), webhook_url)
end

Private Class Methods

formatted_params(params) click to toggle source
# File lib/gcf_ruby.rb, line 66
def formatted_params(params)
  { instances: params }
end
post(model_name, params, url = nil) click to toggle source
# File lib/gcf_ruby.rb, line 26
def post(model_name, params, url = nil)
  uri = URI.parse(GcfRuby.configuration.api_url)
  query_params = { model: model_name }
  query_params[:url] = url if url
  uri.query = URI.encode_www_form(query_params)

  req = Net::HTTP::Post.new(uri)
  req['Authorization'] = "Bearer #{GcfRuby.configuration.api_bearer}"
  req['Content-Type'] = 'application/json'

  req.body =  params.to_json
  generate_https = Net::HTTP.new(uri.host, uri.port)
  generate_https.use_ssl = true

  res = generate_https.request(req)
  response = JSON.parse(res.body)

  case res.code
  when '400'
    raise FailedValidationError, "Wrong inputs #{params} in model #{model_name}"
  when '403'
    raise AuthentificationError, 'Authorization error'
  when '404'
    raise WrongModelName, "Wrong model name #{model_name}"
  when '405'
    raise AsyncError, "#{model_name} should be called in an asynchronous manner"
  when '500'
    raise InternalServerError, 'Internal server error in api'
  when '200'
    # it's sync
    response
  when '202'
    # it's ansync
    true
  else
    raise UnhandledError, 'Cannot process error code'
    false
  end
end