Class: Sorare::Rewards::GameWeek

Inherits:
Object
  • Object
show all
Defined in:
lib/sorare/rewards/game_week.rb

Overview

GameWeek stores the data of a game week

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, hash: nil, salt: nil) ⇒ GameWeek

Returns a new instance of GameWeek.



13
14
15
16
17
# File 'lib/sorare/rewards/game_week.rb', line 13

def initialize(data: nil, hash: nil, salt: nil)
  @uri = URI.parse("#{Sorare::Rewards.configuration.gateway}#{hash}")
  @data = data
  @salt = salt
end

Instance Attribute Details

#saltObject (readonly)

Returns the value of attribute salt.



11
12
13
# File 'lib/sorare/rewards/game_week.rb', line 11

def salt
  @salt
end

Instance Method Details

#configObject



29
30
31
# File 'lib/sorare/rewards/game_week.rb', line 29

def config
  @config ||= GameWeekConfig.new(data['config'])
end

#dataObject



25
26
27
# File 'lib/sorare/rewards/game_week.rb', line 25

def data
  @data ||= JSON.parse(@uri.open.read)
end

#each_leagueObject



40
41
42
43
44
45
46
# File 'lib/sorare/rewards/game_week.rb', line 40

def each_league
  (leagues || {}).each_key do |league_name|
    return to_enum(:each_league) unless block_given?

    yield(league(league_name))
  end
end

#inspectObject



60
61
62
# File 'lib/sorare/rewards/game_week.rb', line 60

def inspect
  format('#<%<class>s %<data>s>', { class: self.class.name, data: { loaded: loaded?, hash: hash, salt: salt } })
end

#league(name) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/sorare/rewards/game_week.rb', line 48

def league(name)
  raise 'Unknown league' unless (leagues || {})[name]

  League.new(
    game_week: self, name: name, config: leagues[name], supply: supply[leagues.dig(name, 'pool_supply')]
  )
end

#loaded?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sorare/rewards/game_week.rb', line 64

def loaded?
  data.present?
end

#player(slug) ⇒ Object



56
57
58
# File 'lib/sorare/rewards/game_week.rb', line 56

def player(slug)
  Player.new(slug, self)
end

#supply_for(league, rarity: nil, player: nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/sorare/rewards/game_week.rb', line 33

def supply_for(league, rarity: nil, player: nil)
  return (supply.dig(league, rarity, player) || 0) if rarity && player
  return (supply.dig(league, rarity) || {}) if rarity

  supply[league] || {}
end