Class: X::Connection
- Inherits:
-
Object
- Object
- X::Connection
- 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
-
#debug_output ⇒ IO
The IO object for debug output.
-
#open_timeout ⇒ Integer
The timeout for opening connections in seconds.
-
#proxy_uri ⇒ URI?
readonly
The parsed proxy URI.
-
#proxy_url ⇒ String?
The proxy URL for requests.
-
#read_timeout ⇒ Integer
The timeout for reading responses in seconds.
-
#write_timeout ⇒ Integer
The timeout for writing requests in seconds.
Instance Method Summary collapse
-
#initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: nil, proxy_url: nil) ⇒ Connection
constructor
Initialize a new connection.
-
#perform(request:) ⇒ Net::HTTPResponse
Perform an HTTP request.
-
#perform_stream(request:) {|Net::HTTPResponse| ... } ⇒ void
Perform a streaming HTTP request.
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
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_output ⇒ IO
The IO object for debug output
58 59 60 |
# File 'lib/x/connection.rb', line 58 def debug_output @debug_output end |
#open_timeout ⇒ 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_uri ⇒ URI? (readonly)
The parsed proxy URI
72 73 74 |
# File 'lib/x/connection.rb', line 72 def proxy_uri @proxy_uri end |
#proxy_url ⇒ String?
The proxy URL for requests
65 66 67 |
# File 'lib/x/connection.rb', line 65 def proxy_url @proxy_url end |
#read_timeout ⇒ 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_timeout ⇒ 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
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
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 |