Class: MLB::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mlb/request_builder.rb

Overview

Builds HTTP requests for the MLB Stats API

Constant Summary collapse

DEFAULT_HEADERS =

Default HTTP headers for API requests

{
  "Content-Type" => "application/json; charset=utf-8",
  "User-Agent" => "MLB-Client/#{VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PLATFORM})"
}.freeze
HTTP_METHODS =

Mapping of HTTP method symbols to Net::HTTP request classes

{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  put: Net::HTTP::Put,
  delete: Net::HTTP::Delete
}.freeze

Instance Method Summary collapse

Instance Method Details

#build(http_method:, uri:, body: nil, headers: {}) ⇒ Net::HTTPRequest

Builds an HTTP request

Examples:

builder.build(http_method: :get, uri: URI("https://example.com"))

Parameters:

  • http_method (Symbol)

    the HTTP method (:get, :post, :put, :delete)

  • uri (URI::Generic)

    the request URI

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

    the request body

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

    additional HTTP headers

Returns:

  • (Net::HTTPRequest)

    the built HTTP request



31
32
33
34
35
# File 'lib/mlb/request_builder.rb', line 31

def build(http_method:, uri:, body: nil, headers: {})
  request = create_request(http_method:, uri:, body:)
  add_headers(request:, headers:)
  request
end