Hello, World… Cup

We’re nearing the end of the World Cup. Even though it’s a little late in the game (no pun intended) with only three matches left, I’ve decided to join in on the World Cup software fun.

An intro to a programming language usually starts off with the traditional “Hello, World!” program. So, in honor of that, I would like to say:

    puts "Hello, World... Cup!"

Today, we’ll be looking into a few tools to help you transform your computer desktop into a World Cup scoreboard. The main tool is the API that my coworker Eric wrote. It’s received a ton of traffic throughout the tournament. At about 15 million API calls, it’s had a lot of traction throughout the community, and it’s what we’ll be using to get the scores on our desktop.

Here is what the final screen can look like:

Screen Shot 2014-07-09 at 10.23.06 AM
Let’s get started.

The first thing you will need is a Ruby installation. Ruby’s bundled with OS X, but if you’re on Windows, you’ll probably need to install it.

Next, you will need to install a tool called GeekTool. This is a fantastic tool for making your desktop your own, and it will be an integral one for getting your scoreboard up and running.

Once you have those two things installed, you’re ready to get to work. I suggest creating a directory to store all of your Geeklets in one place. I’ve named this directory Geeklets.

Let’s change to that directory using the terminal:

    $ cd ~/Geeklets

Let’s add the base of our scoreboard now. Start up GeekTool and you’ll see something like this:

Screen Shot 2014-07-03 at 11.07.28 AM (2)

I created a special group for my World Cup Scoreboard. Feel free to do the same.

Our Geeklet is going to be be dynamic, so we’ll need to use the shell Geeklet. Drag that on the screen like so:

Here, be sure to change the refresh time under the command field. The API updates every minute, so 60s should be a good amount. This tells the Geeklet to run the script every minute to keep it updated.

The next thing we need to do is create a script that GeekTool will use to run our Ruby code. The Ruby code will, in turn, call the APIs at worldcup.sfg.io and return the data to our Geeklet. To do this, create a new text file using your favorite text editor. Be sure to save this in your Geeklets directory as world_cup.rb

Don’t forget to make the Ruby file executable with:

$ chmod a+x ~/Geeklets/world_cup.rb

In your terminal enter in the line:

$ gem install httparty

This gem is what we’ll use to get data from the worldcup.sfg.io website. If you’re using RVM, make sure you install this in the system ruby.

In the world_cup.rb file add these lines:

    #!/usr/bin/env ruby

    require 'json'
    require 'httparty'

In the scoreboard Geeklet’s settings, make sure the command is set to:

    ~/Geeklets/world_cup.rb

Let’s get to the heavy lifting. I’ll post the full code and then explain it after.

#!/usr/bin/env ruby

require 'json'
require 'httparty'

BASE_URL = "http://worldcup.sfg.io/"

def get_json(url)
  response = HTTParty.get(url)
  json = JSON.parse(response.body)

  return json
end

def prepare_matches(matches)
  matches.each do |match|
    home_team = match["home_team"]
    away_team = match["away_team"]

    format_board(home_team, away_team)
  end
end

def format_board(home, away)
  print "#{away["code"]}" + " " * 19 + "#{home["code"]}"
  print "\n" * 2
  print "#{away["goals"]}  -  #{home["goals"]}"
  print "\n" * 2
end

def today
  url = "#{BASE_URL}matches/today"
  matches = get_json(url)

  if matches.count == 0
    puts "No matches today"
  else
    prepare_matches(matches)
  end
end

today

First, we set the BASE_URL as a constant so that we don’t have to type out the full URL every time we need to use it.

In the today method we first set the url that HTTParty will use to get the match data. Then we use the method get_json(url) to get the list of matches that are occurring today. Then, in line 35, we check to see if there were any matches in our list for today. If not, we display a message in the Geeklet letting us know. Otherwise, we use the method prepare_matches(matches) to format the matches into a scoreboard like format.

The format_board method uses each team’s country code, and then adds a little bit of spacing to make the output shown in the geeklet look nice.

The only thing left is to add a title or a logo to the scoreboard. For a title, you can add another shell geeklet and use the echo command directly in the command field of the geeklet like so:

echo 'World Cup'

Joe here at Software for Good gave me the idea to use an Image geeklet with the official World Cup logo.

Here is our final product:

Screen Shot 2014-07-09 at 10.23.06 AM

That’s it! It’s pretty simple, but it can add a little fun to your day. If you’d like to read more about the World Cup API and how it started, head here. There is a lot of data out there, so take this little Ruby script and play with it. Add your own methods and see what you can do. I enjoyed writing this script, and I’m enjoying learning about the World Cup as it continues.