Module: MLB::Utils

Defined in:
lib/mlb/utils.rb

Overview

Utility methods and constants shared across the MLB gem

Constant Summary collapse

DEFAULT_SPORT_ID =

Default sport ID for MLB (Major League Baseball)

1

Class Method Summary collapse

Class Method Details

.build_query(params) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a URL-encoded query string from parameters

Examples:

Utils.build_query(season: 2024, sportId: 1) #=> "season=2024&sportId=1"

Parameters:

  • params (Hash)

    the parameters to encode

Returns:

  • (String)

    the URL-encoded query string



40
41
42
# File 'lib/mlb/utils.rb', line 40

def build_query(params)
  URI.encode_www_form(params)
end

.current_seasonInteger

Returns the current MLB season year

Examples:

Utils.current_season #=> 2024

Returns:

  • (Integer)

    the current year



16
17
18
# File 'lib/mlb/utils.rb', line 16

def current_season
  Time.now.year
end

.extract_id(object) ⇒ Integer, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts the ID from an object or returns the value as-is

Examples:

With a model object

Utils.extract_id(team) #=> 147

With a raw ID

Utils.extract_id(147) #=> 147

Parameters:

  • object (#id, Integer, String)

    an object with an id method or an ID value

Returns:

  • (Integer, String)

    the extracted ID



29
30
31
# File 'lib/mlb/utils.rb', line 29

def extract_id(object)
  object.respond_to?(:id) ? object.id : object
end