Class: Calpack::FaradayClient

Inherits:
HttpClient show all
Defined in:
lib/calpack/http/faraday_client.rb

Overview

An implementation of HttpClient.

Instance Method Summary collapse

Methods inherited from HttpClient

#delete, #get, #head, #patch, #post, #put

Constructor Details

#initialize(timeout: nil, cache: false, max_retries: nil, retry_interval: nil) ⇒ FaradayClient

The constructor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/calpack/http/faraday_client.rb', line 13

def initialize(timeout: nil, cache: false,
               max_retries: nil, retry_interval: nil)
  @connection = Faraday.new do |faraday|
    faraday.use Faraday::HttpCache, serializer: Marshal if cache
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.ssl[:ca_file] = Certifi.where
    faraday.request :retry, max: max_retries, interval: if max_retries &&
                                                           retry_interval
                                                          retry_interval
                                                        end
    faraday.adapter Faraday.default_adapter
    faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
    faraday.options[:open_timeout] = timeout if timeout
  end
end

Instance Method Details

#convert_response(response) ⇒ Object

Method overridden from HttpClient.



60
61
62
# File 'lib/calpack/http/faraday_client.rb', line 60

def convert_response(response)
  HttpResponse.new(response.status, response.headers, response.body)
end

#execute_as_binary(http_request) ⇒ Object

Method overridden from HttpClient.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/calpack/http/faraday_client.rb', line 46

def execute_as_binary(http_request)
  response = @connection.send(
    http_request.http_method.downcase,
    http_request.query_url
  ) do |request|
    request.headers = http_request.headers
    unless http_request.parameters.empty?
      request.body = http_request.parameters
    end
  end
  convert_response(response)
end

#execute_as_string(http_request) ⇒ Object

Method overridden from HttpClient.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/calpack/http/faraday_client.rb', line 32

def execute_as_string(http_request)
  response = @connection.send(
    http_request.http_method.downcase,
    http_request.query_url
  ) do |request|
    request.headers = http_request.headers
    unless http_request.parameters.empty?
      request.body = http_request.parameters
    end
  end
  convert_response(response)
end