Sha256: 9d8e8f300ce6efb8004a98edb5f751c4a6ee16b5cc479ef7559752ea857b6c15

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module MLBStatsAPI
  module Teams
    def team(team_id)
      data = load("mlb_stats_api:teams:#{team_id}") do
        get("/teams/#{team_id}", hydrate: 'venue(timezone)').dig('teams', 0)
      end

      MLBStatsAPI::Team.new data
    end

    def teams(*team_ids)
      teams = ids = []

      team_ids.each do |team_id|
        value = @cache.load("mlb_stats_api:teams:#{team_id}")

        if value
          teams << MLBStatsAPI::Team.new(value)
        else
          ids << team_id
        end
      end

      teams.concat load_teams_by_id(ids)
    end

    def affiliates(team_id, season: nil) = get("/teams/#{team_id}/affiliates", season:)

    def coaches(team_id, date: nil) = get("/teams/#{team_id}/coaches", date: (date || Date.today).strftime('%m/%d/%Y'))

    # def leaders(team_id) = get("/teams/#{team_id}/leaders")

    def roster(team_id, type:, date: nil) = get("/teams/#{team_id}/roster/#{type}", date: date.strftime('%m/%d/%Y'))

    protected

    def load_teams_by_id(ids)
      return [] if ids.none?

      get('/teams', teamId: ids.join(','), hydrate: 'venue(timezone)')['teams'].map do |data|
        @cache.store("mlb_stats_api:teams:#{data['id']}", data)

        MLBStatsAPI::Team.new(data)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mlb_stats_api-0.4.0 lib/mlb_stats_api/teams.rb