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
5 * 60 * 60
- CONFERENCE_MAP =
Mapping of conference abbreviations to full names
{"E" => "East", "W" => "West", nil => "Invalid"}.freeze
Instance Method Summary collapse
-
#eastern_time_date ⇒ Date
private
Returns the current date in Eastern Time.
-
#fetch_conference_standings ⇒ Collection
private
Fetches conference-specific standings.
-
#fetch_games(date) ⇒ Collection
private
Fetches games for a date, using live data for today.
-
#fetch_standings ⇒ Collection
private
Fetches standings based on options.
-
#fetch_team_roster(team) ⇒ Collection
private
Fetches roster for a team.
-
#fetch_team_schedule(team) ⇒ Collection
private
Fetches schedule for a team.
-
#filter_teams(name) ⇒ Collection, Array
private
Filters teams by name or abbreviation pattern, or returns all teams.
-
#find_team_by_name(name) ⇒ Team?
private
Finds a team by name or abbreviation.
-
#normalize_conference(input) ⇒ String
private
Normalizes a conference input to full name.
-
#parse_date(date_str) ⇒ Date
private
Parses a date string into a Date object.
-
#parse_date_string(date_str) ⇒ Date
private
Parses a YYYYMMDD date string.
-
#resolve_leader_category(category, category_map) ⇒ String
private
Resolves a category name to a Leaders constant.
Instance Method Details
#eastern_time_date ⇒ 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.
Returns the current date in Eastern Time
43 |
# File 'lib/nba/cli/helpers.rb', line 43 def eastern_time_date = (Time.now.utc - ET_OFFSET_SECONDS).to_date |
#fetch_conference_standings ⇒ 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 conference-specific standings
90 91 92 93 |
# File 'lib/nba/cli/helpers.rb', line 90 def fetch_conference_standings conf = normalize_conference(.fetch(:conference)) [:season] ? Standings.conference(conf, season: .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
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_standings ⇒ 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 standings based on options
80 81 82 83 84 |
# File 'lib/nba/cli/helpers.rb', line 80 def fetch_standings return fetch_conference_standings if [:conference] [:season] ? Standings.all(season: .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
110 111 112 113 |
# File 'lib/nba/cli/helpers.rb', line 110 def fetch_team_roster(team) season = [: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
100 101 102 103 |
# File 'lib/nba/cli/helpers.rb', line 100 def fetch_team_schedule(team) season = [: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
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
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
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
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
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
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 |