Module: Castle::Core::GetConnection

Defined in:
lib/castle/core/get_connection.rb

Overview

this module returns a new configured Net::HTTP object

Constant Summary collapse

HTTPS_SCHEME =
'https'

Class Method Summary collapse

Class Method Details

.call(config = nil) ⇒ Net::HTTP

Parameters:

Returns:

  • (Net::HTTP)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/castle/core/get_connection.rb', line 12

def call(config = nil)
  config ||= Castle.config
  http = Net::HTTP.new(config.base_url.host, config.base_url.port)
  # `request_timeout` is in milliseconds for historical reasons; both
  # Net::HTTP timeouts take seconds.
  timeout_seconds = config.request_timeout / 1000.0
  http.open_timeout = timeout_seconds
  http.read_timeout = timeout_seconds

  if config.base_url.scheme == HTTPS_SCHEME
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  http
end