Module: NBA::Utils

Defined in:
lib/nba/utils.rb

Overview

Utility methods for the NBA gem

Class Method Summary collapse

Class Method Details

.build_query(**params) ⇒ String

Builds a query string from a hash of parameters

Examples:

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

Parameters:

  • params (Hash)

    the parameters to build into a query string

Returns:

  • (String)

    the query string



50
51
52
# File 'lib/nba/utils.rb', line 50

def self.build_query(**params)
  URI.encode_www_form(params.compact)
end

.current_seasonInteger

Returns the current NBA season year

Examples:

NBA::Utils.current_season #=> 2024

Returns:

  • (Integer)

    the current season year



13
14
15
16
17
18
19
# File 'lib/nba/utils.rb', line 13

def self.current_season
  today = Date.today
  # NBA season typically runs from October to June
  # If we're in January-June, we're in the same season (which started previous year)
  # Otherwise (July-December), return current year
  (today.month <= 6) ? today.year - 1 : today.year
end

.extract_id(entity) ⇒ Integer, String

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

Examples:

NBA::Utils.extract_id(player) #=> 2544
NBA::Utils.extract_id(2544) #=> 2544

Parameters:

Returns:

  • (Integer, String)

    the extracted ID



62
63
64
# File 'lib/nba/utils.rb', line 62

def self.extract_id(entity)
  entity.respond_to?(:id) ? entity.id : entity
end

.extract_league_id(league) ⇒ String

Extracts a league ID from a League object or returns the value as-is

Examples:

NBA::Utils.extract_league_id(league) #=> "00"
NBA::Utils.extract_league_id("00") #=> "00"

Parameters:

  • league (League, String)

    the league or ID

Returns:

  • (String)

    the extracted league ID



90
91
92
# File 'lib/nba/utils.rb', line 90

def self.extract_league_id(league)
  league.respond_to?(:id) ? league.id : league
end

.format_season(year) ⇒ String

Formats a season year into the NBA API format (e.g., “2024-25”)

Examples:

NBA::Utils.format_season(2024) #=> "2024-25"

Parameters:

  • year (Integer)

    the season start year

Returns:

  • (String)

    the formatted season string



28
29
30
# File 'lib/nba/utils.rb', line 28

def self.format_season(year)
  "#{year}-#{(year + 1).to_s[-2..]}"
end

.format_season_id(year) ⇒ String

Formats a season year into the NBA SeasonID format (e.g., “22024”)

Examples:

NBA::Utils.format_season_id(2024) #=> "22024"

Parameters:

  • year (Integer)

    the season start year

Returns:

  • (String)

    the formatted season ID string



39
40
41
# File 'lib/nba/utils.rb', line 39

def self.format_season_id(year)
  "2#{year}"
end

.parse_integer(value) ⇒ Integer?

Parses a value as an integer, returning nil for invalid values

Examples:

NBA::Utils.parse_integer("42") #=> 42
NBA::Utils.parse_integer("N/A") #=> nil

Parameters:

  • value (String, Integer, nil)

    the value to parse

Returns:

  • (Integer, nil)

    the parsed integer or nil



74
75
76
77
78
79
80
# File 'lib/nba/utils.rb', line 74

def self.parse_integer(value)
  return if value.to_s.empty?

  Integer(value)
rescue ArgumentError
  nil
end