class Crm::Core::ConnectionManager

Constants

DEFAULT_TIMEOUT
SOCKET_ERRORS

Attributes

ca_file[R]
cert_store[R]
uri[R]

Public Class Methods

new(uri) click to toggle source
# File lib/crm/core/connection_manager.rb, line 22
def initialize(uri)
  @uri = uri
  @ca_file = File.expand_path('../../../../config/ca-bundle.crt', __FILE__)
  @cert_store = OpenSSL::X509::Store.new.tap do |store|
    store.set_default_paths
    store.add_file(@ca_file)
  end
  @connection = nil
end

Public Instance Methods

request(request, timeout=DEFAULT_TIMEOUT) click to toggle source
# File lib/crm/core/connection_manager.rb, line 32
def request(request, timeout=DEFAULT_TIMEOUT)
  request['User-Agent'] = user_agent
  ensure_started(timeout)

  begin
    @connection.request(request)
  rescue *SOCKET_ERRORS => e
    ensure_finished
    raise Errors::NetworkError.new(e.message, e)
  end
end

Private Instance Methods

configure_timeout(connection, timeout) click to toggle source
# File lib/crm/core/connection_manager.rb, line 83
def configure_timeout(connection, timeout)
  connection.open_timeout = timeout
  connection.read_timeout = timeout
  connection.ssl_timeout = timeout
end
ensure_finished() click to toggle source
# File lib/crm/core/connection_manager.rb, line 67
def ensure_finished
  @connection.finish if @connection && @connection.started?
  @connection = nil
end
ensure_started(timeout) click to toggle source
# File lib/crm/core/connection_manager.rb, line 46
def ensure_started(timeout)
  if @connection && @connection.started?
    configure_timeout(@connection, timeout)
  else
    conn = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == 'https'
      conn.use_ssl = true
      conn.verify_mode = OpenSSL::SSL::VERIFY_PEER
      conn.cert_store = @cert_store
    end
    configure_timeout(conn, timeout)
    retry_twice_on_socket_error do |attempt|
      ActiveSupport::Notifications.instrument("establish_connection.crm") do |msg|
        msg[:attempt] = attempt
        conn.start
      end
    end
    @connection = conn
  end
end
retry_twice_on_socket_error() { |attempt| ... } click to toggle source
# File lib/crm/core/connection_manager.rb, line 72
def retry_twice_on_socket_error
  attempt = 1
  begin
    yield attempt
  rescue *SOCKET_ERRORS => e
    raise Errors::NetworkError.new(e.message, e) if attempt > 2
    attempt += 1
    retry
  end
end
user_agent() click to toggle source
# File lib/crm/core/connection_manager.rb, line 89
def user_agent
  @user_agent ||= (
    gem_info = Gem.loaded_specs["infopark_webcrm_sdk"]
    if gem_info
      "#{gem_info.name}-#{gem_info.version}"
    end
  )
end