Class: MLB::Connection
- Inherits:
-
Object
- Object
- MLB::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/mlb/connection.rb
Overview
Manages HTTP connections to the MLB Stats API
Constant Summary collapse
- DEFAULT_HOST =
Default host for API requests
"statsapi.mlb.com".freeze
- DEFAULT_PORT =
Default port for HTTPS connections
443- DEFAULT_OPEN_TIMEOUT =
Default connection open timeout in seconds
60- DEFAULT_READ_TIMEOUT =
Default read timeout in seconds
60- DEFAULT_WRITE_TIMEOUT =
Default write timeout in seconds
60- DEFAULT_DEBUG_OUTPUT =
Default debug output destination
File.open(File::NULL, "w")
- HTTPS_SCHEME =
HTTPS scheme identifier
"https".freeze
- NETWORK_ERRORS =
Network errors that trigger a NetworkError exception
[ Errno::ECONNREFUSED, Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError ].freeze
Instance Attribute Summary collapse
-
#debug_output ⇒ IO
Returns the IO object for debug output.
-
#open_timeout ⇒ Integer
Returns the connection open timeout in seconds.
-
#proxy_uri ⇒ URI::HTTP?
readonly
Returns the parsed proxy URI.
-
#proxy_url ⇒ String?
Returns the proxy URL.
-
#read_timeout ⇒ Integer
Returns the read timeout in seconds.
-
#write_timeout ⇒ Integer
Returns the write timeout in seconds.
Instance Method Summary collapse
-
#initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: DEFAULT_DEBUG_OUTPUT, proxy_url: nil) ⇒ Connection
constructor
Initializes a new Connection instance.
-
#perform(request:) ⇒ Net::HTTPResponse
Performs an HTTP request.
Constructor Details
#initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: DEFAULT_DEBUG_OUTPUT, proxy_url: nil) ⇒ Connection
Initializes a new Connection instance
130 131 132 133 134 135 136 137 |
# File 'lib/mlb/connection.rb', line 130 def initialize(open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, debug_output: DEFAULT_DEBUG_OUTPUT, proxy_url: nil) @open_timeout = open_timeout @read_timeout = read_timeout @write_timeout = write_timeout @debug_output = debug_output self.proxy_url = proxy_url if proxy_url end |
Instance Attribute Details
#debug_output ⇒ IO
Returns the IO object for debug output
89 90 91 |
# File 'lib/mlb/connection.rb', line 89 def debug_output @debug_output end |
#open_timeout ⇒ Integer
Returns the connection open timeout in seconds
41 42 43 |
# File 'lib/mlb/connection.rb', line 41 def open_timeout @open_timeout end |
#proxy_uri ⇒ URI::HTTP? (readonly)
Returns the parsed proxy URI
113 114 115 |
# File 'lib/mlb/connection.rb', line 113 def proxy_uri @proxy_uri end |
#proxy_url ⇒ String?
Returns the proxy URL
105 106 107 |
# File 'lib/mlb/connection.rb', line 105 def proxy_url @proxy_url end |
#read_timeout ⇒ Integer
Returns the read timeout in seconds
57 58 59 |
# File 'lib/mlb/connection.rb', line 57 def read_timeout @read_timeout end |
#write_timeout ⇒ Integer
Returns the write timeout in seconds
73 74 75 |
# File 'lib/mlb/connection.rb', line 73 def write_timeout @write_timeout end |
Instance Method Details
#perform(request:) ⇒ Net::HTTPResponse
Performs an HTTP request
147 148 149 150 151 152 153 154 |
# File 'lib/mlb/connection.rb', line 147 def perform(request:) uri = request.uri http_client = build_http_client(uri.host || DEFAULT_HOST, uri.port || DEFAULT_PORT) http_client.use_ssl = uri.scheme.eql?(HTTPS_SCHEME) http_client.request(request) rescue *NETWORK_ERRORS => e raise NetworkError, "Network error: #{e}" end |