Module: NBA::Utils
- Defined in:
- lib/nba/utils.rb
Overview
Utility methods for the NBA gem
Class Method Summary collapse
-
.build_query(**params) ⇒ String
Builds a query string from a hash of parameters.
-
.current_season ⇒ Integer
Returns the current NBA season year.
-
.extract_id(entity) ⇒ Integer, String
Extracts an ID from an entity object or returns the value as-is.
-
.extract_league_id(league) ⇒ String
Extracts a league ID from a League object or returns the value as-is.
-
.format_season(year) ⇒ String
Formats a season year into the NBA API format (e.g., “2024-25”).
-
.format_season_id(year) ⇒ String
Formats a season year into the NBA SeasonID format (e.g., “22024”).
-
.parse_integer(value) ⇒ Integer?
Parses a value as an integer, returning nil for invalid values.
Class Method Details
.build_query(**params) ⇒ String
Builds a query string from a hash of parameters
50 51 52 |
# File 'lib/nba/utils.rb', line 50 def self.build_query(**params) URI.encode_www_form(params.compact) end |
.current_season ⇒ Integer
Returns the current NBA 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
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
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”)
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”)
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
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 |