Class: SmashRuby::Tournament
- Inherits:
-
Object
- Object
- SmashRuby::Tournament
- Defined in:
- lib/smash_ruby/tournament.rb
Constant Summary collapse
- BASE_URL =
'https://api.smash.gg/tournament'.freeze
- M =
Dry::Monads
Instance Attribute Summary collapse
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#entrants ⇒ Object
readonly
Returns the value of attribute entrants.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sets ⇒ Object
readonly
Returns the value of attribute sets.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#venue_address ⇒ Object
readonly
Returns the value of attribute venue_address.
-
#venue_name ⇒ Object
readonly
Returns the value of attribute venue_name.
Class Method Summary collapse
Instance Method Summary collapse
- #available_phases ⇒ Object
- #fetch_complete_results_for_event(event) ⇒ Object
- #fetch_phase_results(event, phase) ⇒ Object
- #get_player_results(url) ⇒ Object
-
#initialize(attributes) ⇒ Tournament
constructor
A new instance of Tournament.
- #melee_results ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Tournament
Returns a new instance of Tournament.
17 18 19 20 21 22 23 24 25 |
# File 'lib/smash_ruby/tournament.rb', line 17 def initialize(attributes) @id = attributes.dig('id') @name = attributes.dig('name') @slug = attributes.dig('slug') @start_date = Time.at(attributes.dig('startAt')) @end_date = Time.at(attributes.dig('endAt')) @venue_name = attributes.dig('venueName') @venue_address = attributes.dig('venueAddress') end |
Instance Attribute Details
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def end_date @end_date end |
#entrants ⇒ Object (readonly)
Returns the value of attribute entrants.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def entrants @entrants end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def name @name end |
#sets ⇒ Object (readonly)
Returns the value of attribute sets.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def sets @sets end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def slug @slug end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def start_date @start_date end |
#venue_address ⇒ Object (readonly)
Returns the value of attribute venue_address.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def venue_address @venue_address end |
#venue_name ⇒ Object (readonly)
Returns the value of attribute venue_name.
15 16 17 |
# File 'lib/smash_ruby/tournament.rb', line 15 def venue_name @venue_name end |
Class Method Details
.find(slug) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/smash_ruby/tournament.rb', line 35 def self.find(slug) url = "#{BASE_URL}/#{slug}" response = SmashRuby::Request.get(url, slug, 'tournament') response.fmap do |result| new(result.dig('entities', 'tournament').merge('slug' => slug)) end.value end |
Instance Method Details
#available_phases ⇒ Object
31 32 33 |
# File 'lib/smash_ruby/tournament.rb', line 31 def available_phases @available_phases ||= fetch_phases end |
#fetch_complete_results_for_event(event) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/smash_ruby/tournament.rb', line 82 def fetch_complete_results_for_event(event) players = [] available_phases.dig(event.to_sym).each do |phase| players << fetch_phase_results(event.to_sym, phase) end results = dedupe_and_combine_players(players) build_sets(results) results.values.sort_by(&:placement) end |
#fetch_phase_results(event, phase) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/smash_ruby/tournament.rb', line 44 def fetch_phase_results(event, phase) get_id(event, phase.name).fmap do |id| players = [] if phase.pools.empty? url = "#{BASE_URL}/#{slug}/event/#{event}/phase_groups?expand[]=results&filter={\"phaseId\": \"#{id}\"}&getSingleBracket=true" players << get_player_results(url) else phase.pools.each do |pool| url = "#{BASE_URL}/#{slug}/event/#{event}/phase_groups?expand[]=results&filter={\"phaseId\": \"#{id}\", \"id\": \"#{pool.id}\"}&getSingleBracket=true" players << get_player_results(url) end end players.flatten end.value end |
#get_player_results(url) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/smash_ruby/tournament.rb', line 61 def get_player_results(url) response = SmashRuby::Request.get(url, slug, 'tournament') response.fmap do |result| data = result.dig('items', 'entities') standings = data.dig('standings') entrants = data.dig('entrants') players = [] entrants.each do |e| standings.each do |s| if s.dig('id').include? e.dig('id').to_s players << SmashRuby::Player.new(s.merge(e)) end end end players end.value end |
#melee_results ⇒ Object
27 28 29 |
# File 'lib/smash_ruby/tournament.rb', line 27 def melee_results @melee_singles_results ||= fetch_singles end |