Class: MLB::Connection

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

Constant Summary collapse

DEFAULT_HOST =
"statsapi.mlb.com".freeze
DEFAULT_PORT =
443
DEFAULT_OPEN_TIMEOUT =

seconds

60
DEFAULT_READ_TIMEOUT =

seconds

60
DEFAULT_WRITE_TIMEOUT =

seconds

60
DEFAULT_DEBUG_OUTPUT =
File.open(File::NULL, "w")
NETWORK_ERRORS =
[
  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

Returns a new instance of Connection.



33
34
35
36
37
38
39
40
# File 'lib/mlb/connection.rb', line 33

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 unless proxy_url.nil?
end

Instance Attribute Details

#debug_outputObject

Returns the value of attribute debug_output.



25
26
27
# File 'lib/mlb/connection.rb', line 25

def debug_output
  @debug_output
end

#open_timeoutObject

Returns the value of attribute open_timeout.



25
26
27
# File 'lib/mlb/connection.rb', line 25

def open_timeout
  @open_timeout
end

#proxy_uriObject (readonly)

Returns the value of attribute proxy_uri.



26
27
28
# File 'lib/mlb/connection.rb', line 26

def proxy_uri
  @proxy_uri
end

#proxy_urlObject

Returns the value of attribute proxy_url.



26
27
28
# File 'lib/mlb/connection.rb', line 26

def proxy_url
  @proxy_url
end

#read_timeoutObject

Returns the value of attribute read_timeout.



25
26
27
# File 'lib/mlb/connection.rb', line 25

def read_timeout
  @read_timeout
end

#write_timeoutObject

Returns the value of attribute write_timeout.



25
26
27
# File 'lib/mlb/connection.rb', line 25

def write_timeout
  @write_timeout
end

Instance Method Details

#perform(request:) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/mlb/connection.rb', line 42

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