Skip to content

Instantly share code, notes, and snippets.

@bomatson
Last active August 29, 2015 14:03
Show Gist options
  • Save bomatson/bf0473a1c69ccb5ea903 to your computer and use it in GitHub Desktop.
Save bomatson/bf0473a1c69ccb5ea903 to your computer and use it in GitHub Desktop.
require 'httparty'
response= HTTParty.get('http://worldcup.sfg.io/matches')
# I'd recommend creating a little more spacing when declaring your variables
# it'll help for reading & editing the code (see below)
array = response.parsed_response
stadiums_hash = {}
winners_hash = {}
# I'd also suggest indenting the way we do in class, especially when you are looping through an array:
# array.each do |match|
# match_number = match['match_number']
# end
# I know it may seem silly, but it will really help you down the line
# when you are reviewing previous code samples
# (as well as other developer code samples)
array.each do |match|
matchnumber=match['match_number']
homecountry=match['home_team']['country']
awaycountry=match['away_team']['country']
puts "Match #{matchnumber}: #{homecountry} vs #{awaycountry}"
winner = match['winner']
# awesome! there's another way to handle checking for the presence of Ruby objects:
# In Ruby, objects that are defined but have no value are nil
# Nil is a special object, not just 'empty'
# As a side-effect, there are helpers in ruby for checking (one of which you used)
# Here is another way of doing it, where you check for Draw first,
# then the presence of the object, and in any other case it will be nil
# (via the else statement)
#if match['winner'] == 'Draw'
# puts "Draw"
#elsif match['winner']
# puts "#{match['winner']}"
#else
# puts "Not Played"
#end
#Assuming that the winner is either nil, draw, or a team is
#fine for this assignment (which is why your way is totally cool)
#but if the winner was something other than those three (like Forfeit),
#it would group it with the 'winner' column by default, which we wouldnt want
if winner == nil
puts " Not played"
elsif winner == 'Draw'
puts " Draw"
elsif puts "#{winner}"
end
stadium = match ['location']
unless stadiums_hash.has_key?(stadium)
stadiums_hash[stadium]=[]
end
stadiums_hash[stadium]<< matchnumber
unless winners_hash.has_key?(winner)
winners_hash[winner] = 0
end
winners_hash [winner] += 1
# great stuff!!
end
winners_hash.delete('Draw')
winners_hash.delete(nil)
puts "\n\nMatch Numbers by Stadium"
puts"===========================\n"
stadiums_hash.each do |key, value|
puts "#{key}: #{value}"
end
puts "\n\nTeams With at Least One Win"
puts"===========================\n"
winners_hash.each do |key, value|
puts "#{key}: #{value} win(s)"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment