Class: Clever::Connection

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

Defined Under Namespace

Classes: GatewayTimeoutError

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/clever/connection.rb', line 35

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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clever/connection.rb', line 12

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

  if [502, 504].include?(response.status)
    log_to_sentry(
      'client.app_id' => @client.app_id,
      'connection.body' => body,
      'connection.method' => method,
      'connection.params' => params,
      'connection.path' => path,
      'response.http_status' => response.status,
      'response.raw_body' => response.raw_body
    )
    raise GatewayTimeoutError if response.timed_out?
  end

  response
end

#log(message = '') ⇒ Object



48
49
50
51
52
# File 'lib/clever/connection.rb', line 48

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

  @client.logger.info message
end

#set_token(token) ⇒ Object



31
32
33
# File 'lib/clever/connection.rb', line 31

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