Module: NBA::CLI::Helpers

Included in:
NBA::CLI
Defined in:
lib/nba/cli/helpers.rb

Overview

Helper methods for CLI date parsing and team lookup

Constant Summary collapse

ET_OFFSET_SECONDS =

Eastern Time offset from UTC in seconds

Returns:

  • (Integer)

    offset value in seconds

5 * 60 * 60
CONFERENCE_MAP =

Mapping of conference abbreviations to full names

Returns:

  • (Hash<String, String>)

    conference mapping

{"E" => "East", "W" => "West", nil => "Invalid"}.freeze

Instance Method Summary collapse

Instance Method Details

#eastern_time_dateDate

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.

Returns the current date in Eastern Time

Returns:

  • (Date)

    the current Eastern Time date



43
# File 'lib/nba/cli/helpers.rb', line 43

def eastern_time_date = (Time.now.utc - ET_OFFSET_SECONDS).to_date

#fetch_conference_standingsCollection

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.

Fetches conference-specific standings

Returns:



90
91
92
93
# File 'lib/nba/cli/helpers.rb', line 90

def fetch_conference_standings
  conf = normalize_conference(options.fetch(:conference))
  options[:season] ? Standings.conference(conf, season: options.fetch(:season)) : Standings.conference(conf)
end

#fetch_games(date) ⇒ Collection

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.

Fetches games for a date, using live data for today

Parameters:

  • date (Date)

    the date

Returns:



130
131
132
# File 'lib/nba/cli/helpers.rb', line 130

def fetch_games(date)
  date.eql?(eastern_time_date) ? LiveScoreboard.today : Scoreboard.games(date: date)
end

#fetch_standingsCollection

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.

Fetches standings based on options

Returns:



80
81
82
83
84
# File 'lib/nba/cli/helpers.rb', line 80

def fetch_standings
  return fetch_conference_standings if options[:conference]

  options[:season] ? Standings.all(season: options.fetch(:season)) : Standings.all
end

#fetch_team_roster(team) ⇒ Collection

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.

Fetches roster for a team

Parameters:

  • team (Team)

    the team

Returns:



110
111
112
113
# File 'lib/nba/cli/helpers.rb', line 110

def fetch_team_roster(team)
  season = options[:season]
  season ? Roster.find(team: team, season: season) : Roster.find(team: team)
end

#fetch_team_schedule(team) ⇒ Collection

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.

Fetches schedule for a team

Parameters:

  • team (Team)

    the team

Returns:



100
101
102
103
# File 'lib/nba/cli/helpers.rb', line 100

def fetch_team_schedule(team)
  season = options[:season]
  season ? Schedule.by_team(team: team, season: season) : Schedule.by_team(team: team)
end

#filter_teams(name) ⇒ Collection, Array

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.

Filters teams by name or abbreviation pattern, or returns all teams

Parameters:

  • name (String, nil)

    the team name or abbreviation pattern to filter by

Returns:



60
61
62
63
64
65
# File 'lib/nba/cli/helpers.rb', line 60

def filter_teams(name)
  return Teams.all unless name

  pattern = Regexp.new(name, Regexp::IGNORECASE)
  Teams.all.select { |team| pattern.match?(team.full_name) || pattern.match?(team.abbreviation) }
end

#find_team_by_name(name) ⇒ Team?

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.

Finds a team by name or abbreviation

Parameters:

  • name (String)

    the team name or abbreviation

Returns:

  • (Team, nil)

    the matching team or nil



50
51
52
53
# File 'lib/nba/cli/helpers.rb', line 50

def find_team_by_name(name)
  pattern = Regexp.new(name, Regexp::IGNORECASE)
  Teams.all.find { |t| pattern.match?(t.full_name) || pattern.match?(t.abbreviation) }
end

#normalize_conference(input) ⇒ 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.

Normalizes a conference input to full name

Parameters:

  • input (String)

    the conference input (e.g., “e”, “E”, “East”, “w”, “W”, “West”)

Returns:

  • (String)

    the normalized conference name



72
73
74
# File 'lib/nba/cli/helpers.rb', line 72

def normalize_conference(input)
  CONFERENCE_MAP.fetch(input.upcase[0].to_s, input)
end

#parse_date(date_str) ⇒ Date

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.

Parses a date string into a Date object

Parameters:

  • date_str (String, nil)

    the date string

Returns:

  • (Date)

    the parsed date



18
19
20
21
22
23
24
# File 'lib/nba/cli/helpers.rb', line 18

def parse_date(date_str)
  return eastern_time_date if date_str.nil? || date_str.eql?("today")
  return eastern_time_date - 1 if date_str.eql?("yesterday")
  return eastern_time_date + 1 if date_str.eql?("tomorrow")

  parse_date_string(date_str)
end

#parse_date_string(date_str) ⇒ Date

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.

Parses a YYYYMMDD date string

Parameters:

  • date_str (String)

    the date string in YYYYMMDD format

Returns:

  • (Date)

    the parsed date

Raises:

  • (SystemExit)

    if the date string is invalid



32
33
34
35
36
37
# File 'lib/nba/cli/helpers.rb', line 32

def parse_date_string(date_str)
  Date.strptime(date_str, "%Y%m%d")
rescue Date::Error
  say("Invalid date '#{date_str}'. Use YYYYMMDD format, 'today', 'yesterday', or 'tomorrow'.")
  raise SystemExit
end

#resolve_leader_category(category, category_map) ⇒ 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.

Resolves a category name to a Leaders constant

Parameters:

  • category (String)

    the category name

  • category_map (Hash)

    mapping of category names to constant names

Returns:

  • (String)

    the Leaders constant value



121
122
123
# File 'lib/nba/cli/helpers.rb', line 121

def resolve_leader_category(category, category_map)
  category_map[category.upcase] || Leaders::PTS
end