Class: X::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/x/connection.rb

Overview

Manages HTTP connections to the X API

Constant Summary collapse

DEFAULT_HOST =

Default host for the X API

"api.twitter.com".freeze
DEFAULT_PORT =

Default port for HTTPS connections

443
DEFAULT_OPEN_TIMEOUT =

Default timeout for opening connections in seconds

60
DEFAULT_READ_TIMEOUT =

Default timeout for reading responses in seconds

60
DEFAULT_WRITE_TIMEOUT =

Default timeout for writing requests in seconds

60
NETWORK_ERRORS =

Network errors that should be wrapped in NetworkError

[
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Net::OpenTimeout,
  Net::ReadTimeout,
  OpenSSL::SSL::SSLError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: nil, proxy_url: nil) ⇒ Connection

Initialize a new connection

Examples:

Create a connection with default settings

connection = X::Connection.new

Create a connection with custom timeouts

connection = X::Connection.new(open_timeout: 30, read_timeout: 30)

Parameters:

  • open_timeout (Integer) (defaults to: DEFAULT_OPEN_TIMEOUT)

    the timeout for opening connections in seconds

  • read_timeout (Integer) (defaults to: DEFAULT_READ_TIMEOUT)

    the timeout for reading responses in seconds

  • write_timeout (Integer) (defaults to: DEFAULT_WRITE_TIMEOUT)

    the timeout for writing requests in seconds

  • debug_output (IO) (defaults to: nil)

    the IO object for debug output

  • proxy_url (String, nil) (defaults to: nil)

    the proxy URL for requests



92
93
94
95
96
97
98
99
# File 'lib/x/connection.rb', line 92

def initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT,
  write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: nil, proxy_url: nil)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @write_timeout = write_timeout
  @debug_output = debug_output
  self.proxy_url = proxy_url unless proxy_url.nil?
end

Instance Attribute Details

#debug_outputIO

The IO object for debug output

Examples:

Get or set the debug output

connection.debug_output = $stderr

Returns:

  • (IO)

    the IO object for debug output



58
59
60
# File 'lib/x/connection.rb', line 58

def debug_output
  @debug_output
end

#open_timeoutInteger

The timeout for opening connections in seconds

Examples:

Get or set the open timeout

connection.open_timeout = 30

Returns:

  • (Integer)

    the timeout for opening connections in seconds



37
38
39
# File 'lib/x/connection.rb', line 37

def open_timeout
  @open_timeout
end

#proxy_uriURI? (readonly)

The parsed proxy URI

Examples:

Get the proxy URI

connection.proxy_uri

Returns:

  • (URI, nil)

    the parsed proxy URI



72
73
74
# File 'lib/x/connection.rb', line 72

def proxy_uri
  @proxy_uri
end

#proxy_urlString?

The proxy URL for requests

Examples:

Get the proxy URL

connection.proxy_url

Returns:

  • (String, nil)

    the proxy URL for requests



65
66
67
# File 'lib/x/connection.rb', line 65

def proxy_url
  @proxy_url
end

#read_timeoutInteger

The timeout for reading responses in seconds

Examples:

Get or set the read timeout

connection.read_timeout = 30

Returns:

  • (Integer)

    the timeout for reading responses in seconds



44
45
46
# File 'lib/x/connection.rb', line 44

def read_timeout
  @read_timeout
end

#write_timeoutInteger

The timeout for writing requests in seconds

Examples:

Get or set the write timeout

connection.write_timeout = 30

Returns:

  • (Integer)

    the timeout for writing requests in seconds



51
52
53
# File 'lib/x/connection.rb', line 51

def write_timeout
  @write_timeout
end

Instance Method Details

#perform(request:) ⇒ Net::HTTPResponse

Perform an HTTP request

Examples:

Perform a request

response = connection.perform(request: request)

Parameters:

  • request (Net::HTTPRequest)

    the HTTP request to perform

Returns:

  • (Net::HTTPResponse)

    the HTTP response

Raises:



109
110
111
112
113
114
115
116
117
# File 'lib/x/connection.rb', line 109

def perform(request:)
  host = request.uri.host || DEFAULT_HOST
  port = request.uri.port || DEFAULT_PORT
  http_client = build_http_client(host, port)
  http_client.use_ssl = request.uri.scheme.eql?("https")
  http_client.request(request)
rescue *NETWORK_ERRORS => e
  raise NetworkError, "Network error: #{e}"
end

#perform_stream(request:) {|Net::HTTPResponse| ... } ⇒ void

This method returns an undefined value.

Perform a streaming HTTP request

Examples:

Perform a streaming request

connection.perform_stream(request: request) { |response| response.read_body { |chunk| } }

Parameters:

  • request (Net::HTTPRequest)

    the HTTP request to perform

Yields:

  • (Net::HTTPResponse)

    the HTTP response for streaming

Raises:



128
129
130
131
132
133
134
135
136
# File 'lib/x/connection.rb', line 128

def perform_stream(request:, &)
  host = request.uri.host || DEFAULT_HOST
  port = request.uri.port || DEFAULT_PORT
  http_client = build_http_client(host, port)
  http_client.use_ssl = request.uri.scheme.eql?("https")
  http_client.request(request, &)
rescue *NETWORK_ERRORS => e
  raise NetworkError, "Network error: #{e}"
end