Class: MLBStatsAPI::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Conferences, Config, Divisions, Drafts, Games, HomeRunDerby, Leagues, People, Schedules, Seasons, Sports, Standings, Stats, Teams, Venues
Defined in:
lib/mlb_stats_api/client.rb

Constant Summary collapse

DEFAULT_VERSION =
'1'

Constants included from Schedules

Schedules::SCHEDULE_TYPES

Constants included from Leagues

Leagues::LEAGUES

Constants included from Games

Games::POSTGAME_STATUSES, Games::PREGAME_STATUSES

Constants included from Divisions

Divisions::DIVISIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Venues

#venue

Methods included from Teams

#affiliates, #coaches, #roster, #team, #teams

Methods included from Stats

#stats, #stats_leaders

Methods included from Standings

#standings

Methods included from Sports

#sport, #sport_players, #sports

Methods included from Seasons

#season, #seasons

Methods included from Schedules

#schedule

Methods included from People

#person, #person_game_stats

Methods included from Leagues

#all_star_ballot, #all_star_final_vote, #all_star_write_ins, #leagues

Methods included from HomeRunDerby

#home_run_derby, #home_run_derby_bracket, #home_run_derby_pool

Methods included from Games

#boxscore, #color_feed, #content, #context_metrics, #linescore, #live_feed, #live_feed_diff, #live_feed_timestamps, #play_by_play, postgame_status?, pregame_status?, #win_probability

Methods included from Drafts

#draft, #draft_latest, #draft_prospects

Methods included from Divisions

#divisions

Methods included from Config

#baseball_stats, #game_status, #game_types, #languages, #league_leader_types, #metrics, #platforms, #positions, #roster_types, #schedule_event_types, #situation_codes, #standings_types, #stat_groups, #stat_types

Methods included from Conferences

#conference, #conferences

Constructor Details

#initialize(logger: IO::NULL, cache: nil) ⇒ Client

Returns a new instance of Client.



39
40
41
42
43
44
45
46
47
# File 'lib/mlb_stats_api/client.rb', line 39

def initialize(logger: IO::NULL, cache: nil)
  @cache = if cache
             Moneta.new(cache.class.to_s.to_sym, backend: cache)
           else
             Moneta.new(:Null)
           end

  @logger = logger.is_a?(::Logger) ? logger : ::Logger.new(logger)
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



37
38
39
# File 'lib/mlb_stats_api/client.rb', line 37

def cache
  @cache
end

#loggerObject

Returns the value of attribute logger.



37
38
39
# File 'lib/mlb_stats_api/client.rb', line 37

def logger
  @logger
end

Instance Method Details

#get(endpoint, query = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mlb_stats_api/client.rb', line 49

def get(endpoint, query = {})
  url = "/api/v#{query.delete(:version) || DEFAULT_VERSION}#{endpoint}"

  args = normalize_query_args(query)

  @logger.info "Fetching URL: #{url} Query: #{args.inspect}"

  response = self.class.get url, query: args.merge(t: Time.now.to_i)

  raise_exception(response) unless response.code == 200

  response.parsed_response
end

#load(key, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mlb_stats_api/client.rb', line 70

def load(key, options = {})
  value = @cache.load(key)

  return value if value

  value = yield

  @cache.store(key, value, options)

  value
end

#normalize_query_args(query) ⇒ Object



63
64
65
66
67
68
# File 'lib/mlb_stats_api/client.rb', line 63

def normalize_query_args(query)
  query
    .reject { |_, v| v.nil? }
    .map { |key, val| [key, val.is_a?(Array) ? val.join(',') : val] }
    .to_h
end