Class: Clever::Connection

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

Constant Summary collapse

OPEN_TIMEOUT =
60
TIMEOUT =
120

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Connection

Returns a new instance of Connection.



8
9
10
# File 'lib/clever/connection.rb', line 8

def initialize(client)
  @client = client
end

Instance Method Details

#connectionObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clever/connection.rb', line 20

def connection
  return @connection if @connection

  @connection = Faraday.new(@client.api_url) do |connection|
    connection.request :json
    connection.response :logger, @client.logger if @client.logger
    connection.response :json, content_type: /\bjson$/
    connection.adapter Faraday.default_adapter
  end
  @connection.basic_auth(@client.vendor_key, @client.vendor_secret)
  @connection
end

#execute(path, method = :get, params = nil, body = nil) ⇒ Object



12
13
14
# File 'lib/clever/connection.rb', line 12

def execute(path, method = :get, params = nil, body = nil)
  Response.new(raw_request(path, method, params, body))
end

#log(message = '') ⇒ Object



33
34
35
36
37
# File 'lib/clever/connection.rb', line 33

def log(message = '')
  return unless @client.logger

  @client.logger.info message
end

#set_token(token) ⇒ Object



16
17
18
# File 'lib/clever/connection.rb', line 16

def set_token(token)
  connection.authorization :Bearer, token
end