Class: MLB::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Examples:

connection = MLB::Connection.new

Parameters:

  • open_timeout (Integer) (defaults to: DEFAULT_OPEN_TIMEOUT)

    the connection open timeout in seconds

  • read_timeout (Integer) (defaults to: DEFAULT_READ_TIMEOUT)

    the read timeout in seconds

  • write_timeout (Integer) (defaults to: DEFAULT_WRITE_TIMEOUT)

    the write timeout in seconds

  • debug_output (IO) (defaults to: DEFAULT_DEBUG_OUTPUT)

    the IO object for debug output

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

    the proxy URL



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_outputIO

Returns the IO object for debug output

Examples:

connection.debug_output #=> #<File:/dev/null>

Returns:

  • (IO)

    the IO object for debug output



89
90
91
# File 'lib/mlb/connection.rb', line 89

def debug_output
  @debug_output
end

#open_timeoutInteger

Returns the connection open timeout in seconds

Examples:

connection.open_timeout #=> 60

Returns:

  • (Integer)

    the connection open timeout in seconds



41
42
43
# File 'lib/mlb/connection.rb', line 41

def open_timeout
  @open_timeout
end

#proxy_uriURI::HTTP? (readonly)

Returns the parsed proxy URI

Examples:

connection.proxy_uri #=> #<URI::HTTP http://proxy.example.com:8080>

Returns:

  • (URI::HTTP, nil)

    the parsed proxy URI



113
114
115
# File 'lib/mlb/connection.rb', line 113

def proxy_uri
  @proxy_uri
end

#proxy_urlString?

Returns the proxy URL

Examples:

connection.proxy_url #=> "http://proxy.example.com:8080"

Returns:

  • (String, nil)

    the proxy URL



105
106
107
# File 'lib/mlb/connection.rb', line 105

def proxy_url
  @proxy_url
end

#read_timeoutInteger

Returns the read timeout in seconds

Examples:

connection.read_timeout #=> 60

Returns:

  • (Integer)

    the read timeout in seconds



57
58
59
# File 'lib/mlb/connection.rb', line 57

def read_timeout
  @read_timeout
end

#write_timeoutInteger

Returns the write timeout in seconds

Examples:

connection.write_timeout #=> 60

Returns:

  • (Integer)

    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

Examples:

connection.perform(request: request)

Parameters:

  • request (Net::HTTPRequest)

    the HTTP request to perform

Returns:

  • (Net::HTTPResponse)

    the HTTP response

Raises:



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