Class: Fickle::Client

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

Constant Summary collapse

USERNAME =
'fickle'
MIME_TYPE =
'application/json'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, password = nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/fickle.rb', line 11

def initialize(host, password = nil)
  @host = host
  @password = password
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/fickle.rb', line 9

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/fickle.rb', line 9

def password
  @password
end

Class Method Details

.connection(url, password = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fickle.rb', line 16

def self.connection(url, password = nil)
  Faraday.new(url) do |builder|
    if !password.nil?
      builder.use Faraday::Request::BasicAuthentication, USERNAME, password
    end

    builder.headers['Content-Type'] = MIME_TYPE
    builder.use Faraday::Response::RaiseError

    builder.adapter Faraday.default_adapter
  end
end

Instance Method Details

#connectionObject



29
30
31
# File 'lib/fickle.rb', line 29

def connection
  @connection ||= self.class.connection(host, password)
end

#fitObject



38
39
40
41
# File 'lib/fickle.rb', line 38

def fit
  post('/fit')
  true
end

#load(dataset) ⇒ Object



33
34
35
36
# File 'lib/fickle.rb', line 33

def load(dataset)
  post('/load', encode(dataset))
  true
end

#predict(samples) ⇒ Object



43
44
45
46
47
48
# File 'lib/fickle.rb', line 43

def predict(samples)
  path = '/predict'

  response = post(path, encode(samples))
  decode(response)
end

#recommend(keys, n = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/fickle.rb', line 50

def recommend(keys, n = nil)
  response = connection.post('/recommend') do |req|
    req.params['n'] = Integer(n) if n
    req.body = encode(keys)
  end
  decode(response)
end