Class: Twingly::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/http.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

DEFAULT_RETRYABLE_EXCEPTIONS =
[
  Faraday::ConnectionFailed,
  Faraday::SSLError,
  Zlib::BufError,
  Zlib::DataError,
].freeze
TIMEOUT_EXCEPTIONS =
[
  Faraday::TimeoutError,
  Net::OpenTimeout,
].freeze
DEFAULT_HTTP_TIMEOUT =
20
DEFAULT_HTTP_OPEN_TIMEOUT =
10
DEFAULT_NUMBER_OF_RETRIES =
0
DEFAULT_RETRY_INTERVAL =
1
DEFAULT_MAX_URL_SIZE_BYTES =
Float::INFINITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:, base_user_agent:) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twingly/http.rb', line 42

def initialize(logger:, base_user_agent:)
  @logger          = logger
  @base_user_agent = base_user_agent
  @request_id      = nil

  @http_timeout      = DEFAULT_HTTP_TIMEOUT
  @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT

  @retryable_exceptions = DEFAULT_RETRYABLE_EXCEPTIONS
  @number_of_retries    = DEFAULT_NUMBER_OF_RETRIES
  @retry_interval       = DEFAULT_RETRY_INTERVAL
  @on_retry_callback    = nil

  @max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
end

Instance Attribute Details

#http_open_timeout=(value) ⇒ Object (writeonly)

Sets the attribute http_open_timeout

Parameters:

  • value

    the value to set the attribute http_open_timeout to.



33
34
35
# File 'lib/twingly/http.rb', line 33

def http_open_timeout=(value)
  @http_open_timeout = value
end

#http_timeout=(value) ⇒ Object (writeonly)

Sets the attribute http_timeout

Parameters:

  • value

    the value to set the attribute http_timeout to.



32
33
34
# File 'lib/twingly/http.rb', line 32

def http_timeout=(value)
  @http_timeout = value
end

#max_url_size_bytes=(value) ⇒ Object (writeonly)

Sets the attribute max_url_size_bytes

Parameters:

  • value

    the value to set the attribute max_url_size_bytes to.



37
38
39
# File 'lib/twingly/http.rb', line 37

def max_url_size_bytes=(value)
  @max_url_size_bytes = value
end

#number_of_retries=(value) ⇒ Object (writeonly)

Sets the attribute number_of_retries

Parameters:

  • value

    the value to set the attribute number_of_retries to.



34
35
36
# File 'lib/twingly/http.rb', line 34

def number_of_retries=(value)
  @number_of_retries = value
end

#on_retry_callback=(value) ⇒ Object (writeonly)

Sets the attribute on_retry_callback

Parameters:

  • value

    the value to set the attribute on_retry_callback to.



36
37
38
# File 'lib/twingly/http.rb', line 36

def on_retry_callback=(value)
  @on_retry_callback = value
end

#request_id=(value) ⇒ Object (writeonly)

Sets the attribute request_id

Parameters:

  • value

    the value to set the attribute request_id to.



38
39
40
# File 'lib/twingly/http.rb', line 38

def request_id=(value)
  @request_id = value
end

#retry_interval=(value) ⇒ Object (writeonly)

Sets the attribute retry_interval

Parameters:

  • value

    the value to set the attribute retry_interval to.



35
36
37
# File 'lib/twingly/http.rb', line 35

def retry_interval=(value)
  @retry_interval = value
end

#retryable_exceptionsObject

Returns the value of attribute retryable_exceptions.



40
41
42
# File 'lib/twingly/http.rb', line 40

def retryable_exceptions
  @retryable_exceptions
end

Instance Method Details

#get(url, params: {}) ⇒ Object



58
59
60
# File 'lib/twingly/http.rb', line 58

def get(url, params: {})
  http_response_for(:get, url: url, params: params)
end

#post(url, body:, headers: {}) ⇒ Object



62
63
64
# File 'lib/twingly/http.rb', line 62

def post(url, body:, headers: {})
  http_response_for(:post, url: url, body: body, headers: headers)
end