Class: Yhat

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

Instance Method Summary collapse

Constructor Details

#initialize(username, apikey, uri = 'api.yhathq.com') ⇒ Yhat

Class that can be used to access the Yhat API



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yhat.rb', line 9

def initialize(username, apikey, uri = 'api.yhathq.com')
  @username = username
  @apikey = apikey
  @base_uri = uri

  if @base_uri == 'api.yhathq.com'
    @is_enterprise = false
  else
    @is_enterprise = true
  end

end

Instance Method Details

#predict(modelname, data) ⇒ Hash

Make a prediction by calling Yhat via HTTP. You should pass both the name of the model you want to use to make a prediction as well as a JSON object (or a Hash) that contains the data requried to make a prediction.

Parameters:

  • modelname (String)
  • data (Hash)

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yhat.rb', line 30

def predict(modelname, data)
  uri = URI::parse(@base_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  endpoint = "/" + @username + "/models/" + modelname + "/"
  request = Net::HTTP::Post.new(endpoint)
  request.add_field('Content-Type', 'application/json')
  request.basic_auth(@username, @apikey)
  request.body = data.to_json
  response = http.request(request)
  data = response.body
  JSON.parse(data)
end