Class: MessageMediaLookups::FaradayClient

Inherits:
HttpClient
  • Object
show all
Defined in:
lib/message_media_lookups/http/faraday_client.rb

Overview

An implementation of HttpClient.

Instance Method Summary collapse

Methods inherited from HttpClient

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

Constructor Details

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

The constructor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/message_media_lookups/http/faraday_client.rb', line 8

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.request :multipart
    faraday.request :url_encoded
    faraday.ssl[:ca_file] = Certifi.where
    faraday.adapter Faraday.default_adapter
    faraday.options[:params_encoder] = Faraday::FlatParamsEncoder
    faraday.options[:open_timeout] = timeout if timeout
    faraday.request :retry, max: max_retries, interval: if max_retries &&
                                                           retry_interval
                                                          retry_interval
                                                        end
  end
end

Instance Method Details

#convert_response(response) ⇒ Object

Method overridden from HttpClient.



50
51
52
# File 'lib/message_media_lookups/http/faraday_client.rb', line 50

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

#execute_as_binary(http_request) ⇒ Object

Method overridden from HttpClient.



38
39
40
41
42
43
44
45
46
47
# File 'lib/message_media_lookups/http/faraday_client.rb', line 38

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
    request.body = http_request.parameters
  end
  convert_response(response)
end

#execute_as_string(http_request) ⇒ Object

Method overridden from HttpClient.



26
27
28
29
30
31
32
33
34
35
# File 'lib/message_media_lookups/http/faraday_client.rb', line 26

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
    request.body = http_request.parameters
  end
  convert_response(response)
end