Class: Fickle::Client
- Inherits:
-
Object
- Object
- Fickle::Client
- Defined in:
- lib/fickle.rb
Constant Summary collapse
- USERNAME =
'fickle'- MIME_TYPE =
'application/json'
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
Class Method Summary collapse
Instance Method Summary collapse
- #connection ⇒ Object
- #fit ⇒ Object
-
#initialize(host, password = nil) ⇒ Client
constructor
A new instance of Client.
- #load(dataset) ⇒ Object
- #predict(samples) ⇒ Object
- #recommend(keys, n = nil) ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/fickle.rb', line 9 def host @host end |
#password ⇒ Object (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
#connection ⇒ Object
29 30 31 |
# File 'lib/fickle.rb', line 29 def connection @connection ||= self.class.connection(host, password) end |
#fit ⇒ Object
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 |