Sha256: ac90423c8f1745ae3a7e768f4f4c6ec9f4fe6cfde961b4e2724e7836ab27e56d

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module MLBStatsAPI
  module Teams
    def team(team_id)
      data = fetch("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: season)
    end

    def coaches(team_id, date: nil)
      date ||= Date.today

      get("/teams/#{team_id}/coaches", date: date.strftime('%m/%d/%Y'))
    end

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

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

    protected

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

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mlb_stats_api-0.1.1 lib/mlb_stats_api/teams.rb
mlb_stats_api-0.1.0 lib/mlb_stats_api/teams.rb