Class: PredictionIO::Connection

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

Overview

This class handles Connections

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Connection

Creates a connection to the given URI.



9
10
11
12
13
14
15
16
# File 'lib/predictionio/connection.rb', line 9

def initialize(uri)
  @connection = Faraday.new(:url => uri) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    yield faraday if block_given?
  end
  @connection.headers['Content-Type'] = 'application/json; charset=utf-8'
end

Instance Method Details

#delete(request) ⇒ Object

Create a DELETE and return the response.



35
36
37
38
39
# File 'lib/predictionio/connection.rb', line 35

def delete(request)
  @connection.delete request.path do |req|
    req.body = request.params
  end
end

#get(request) ⇒ Object

Create a GET request and return the response.



19
20
21
# File 'lib/predictionio/connection.rb', line 19

def get(request)
  @connection.get request.qpath
end

#post(request) ⇒ Object

Create a POST and return the response.



24
25
26
27
28
29
30
31
32
# File 'lib/predictionio/connection.rb', line 24

def post(request)
  if request.params.is_a?(Hash)
    @connection.post request.path, request.params
  else
    @connection.post request.path do |req|
      req.body = request.params
    end
  end
end