Class: MLB::Client

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

Overview

HTTP client for making requests to the MLB Stats API

Constant Summary collapse

DEFAULT_BASE_URL =

Default base URL for the MLB Stats API

"https://statsapi.mlb.com/api/v1/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DEFAULT_BASE_URL, open_timeout: Connection::DEFAULT_OPEN_TIMEOUT, read_timeout: Connection::DEFAULT_READ_TIMEOUT, write_timeout: Connection::DEFAULT_WRITE_TIMEOUT, debug_output: Connection::DEFAULT_DEBUG_OUTPUT, proxy_url: nil, max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS) ⇒ Client

Initializes a new Client instance

Examples:

client = MLB::Client.new

Parameters:

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    the base URL for API requests

  • open_timeout (Integer) (defaults to: Connection::DEFAULT_OPEN_TIMEOUT)

    the connection open timeout in seconds

  • read_timeout (Integer) (defaults to: Connection::DEFAULT_READ_TIMEOUT)

    the read timeout in seconds

  • write_timeout (Integer) (defaults to: Connection::DEFAULT_WRITE_TIMEOUT)

    the write timeout in seconds

  • debug_output (IO) (defaults to: Connection::DEFAULT_DEBUG_OUTPUT)

    the IO object for debug output

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

    the proxy URL

  • max_redirects (Integer) (defaults to: RedirectHandler::DEFAULT_MAX_REDIRECTS)

    the maximum number of redirects to follow



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mlb/client.rb', line 48

def initialize(base_url: DEFAULT_BASE_URL,
  open_timeout: Connection::DEFAULT_OPEN_TIMEOUT,
  read_timeout: Connection::DEFAULT_READ_TIMEOUT,
  write_timeout: Connection::DEFAULT_WRITE_TIMEOUT,
  debug_output: Connection::DEFAULT_DEBUG_OUTPUT,
  proxy_url: nil,
  max_redirects: RedirectHandler::DEFAULT_MAX_REDIRECTS)
  @base_url = base_url
  @connection = Connection.new(open_timeout:, read_timeout:, write_timeout:, debug_output:, proxy_url:)
  @request_builder = RequestBuilder.new
  @redirect_handler = RedirectHandler.new(connection: @connection, request_builder: @request_builder, max_redirects:)
  @error_handler = ErrorHandler.new
end

Instance Attribute Details

#base_urlString

Returns the base URL for API requests

Examples:

client.base_url #=> "https://statsapi.mlb.com/api/v1/"

Returns:

  • (String)

    the base URL for API requests



21
22
23
# File 'lib/mlb/client.rb', line 21

def base_url
  @base_url
end

Instance Method Details

#delete(endpoint, headers: {}) ⇒ String?

Performs a DELETE request

Examples:

client.delete("endpoint")

Parameters:

  • endpoint (String)

    the API endpoint

  • headers (Hash) (defaults to: {})

    optional HTTP headers

Returns:

  • (String, nil)

    the response body



108
109
110
# File 'lib/mlb/client.rb', line 108

def delete(endpoint, headers: {})
  execute_request(:delete, endpoint, headers:)
end

#get(endpoint, headers: {}) ⇒ String?

Performs a GET request

Examples:

client.get("teams")

Parameters:

  • endpoint (String)

    the API endpoint

  • headers (Hash) (defaults to: {})

    optional HTTP headers

Returns:

  • (String, nil)

    the response body



70
71
72
# File 'lib/mlb/client.rb', line 70

def get(endpoint, headers: {})
  execute_request(:get, endpoint, headers:)
end

#post(endpoint, body = nil, headers: {}) ⇒ String?

Performs a POST request

Examples:

client.post("endpoint", '{"key": "value"}')

Parameters:

  • endpoint (String)

    the API endpoint

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

    the request body

  • headers (Hash) (defaults to: {})

    optional HTTP headers

Returns:

  • (String, nil)

    the response body



83
84
85
# File 'lib/mlb/client.rb', line 83

def post(endpoint, body = nil, headers: {})
  execute_request(:post, endpoint, body:, headers:)
end

#put(endpoint, body = nil, headers: {}) ⇒ String?

Performs a PUT request

Examples:

client.put("endpoint", '{"key": "value"}')

Parameters:

  • endpoint (String)

    the API endpoint

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

    the request body

  • headers (Hash) (defaults to: {})

    optional HTTP headers

Returns:

  • (String, nil)

    the response body



96
97
98
# File 'lib/mlb/client.rb', line 96

def put(endpoint, body = nil, headers: {})
  execute_request(:put, endpoint, body:, headers:)
end