Class: IBM::ML::Local

Inherits:
Object
  • Object
show all
Includes:
IBM::ML
Defined in:
lib/ibm/ml/local.rb

Overview

Class for calling Local Machine Learning scoring service

Constant Summary

Constants included from IBM::ML

VERSION

Instance Method Summary collapse

Methods included from IBM::ML

#fetch_token

Constructor Details

#initialize(host, username, password) ⇒ Local

Returns a new instance of Local.



8
9
10
11
12
13
# File 'lib/ibm/ml/local.rb', line 8

def initialize(host, username, password)
  @host = host
  super(username, password)
  @http.use_ssl     = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

Instance Method Details

#score(deployment_id, hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ibm/ml/local.rb', line 15

def score(deployment_id, hash)
  url = URI("https://#{@host}/v2/scoring/online/#{deployment_id}")

  # noinspection RubyStringKeysInHashInspection
  header = {
    'authorization' => "Bearer #{fetch_token}",
    'content-type'  => 'application/json'
  }

  request      = Net::HTTP::Post.new(url, header)
  request.body = { fields: hash.keys, records: [hash.values] }.to_json

  response = @http.request(request)

  begin
    body = JSON.parse(response.read_body)
    body.key?('records') ? body : raise(ScoringError, response.read_body)
  rescue JSON::ParserError
    raise(ScoringError, response.read_body)
  end
end