I enjoy watching baseball. I’d call myself a casual fan, not because I only watch when they’re good, but because I’m not one of those folks who can tell you all the reasons why they are good or what they should do to get even better. I just like watching baseball. It’s a reliable presence with an even tempo and also there’s a lot of statistics.

As the Twins have been leading their division, league and even all of baseball quite frequently this season, my cousin started tweeting whenever they had the best record in baseball. Of course that got tedious so he inquired about automating it. I thought that sounded like a fun project. And if something is worth doing, it’s worth overdoing, so I made a ruby gem that includes the leader executable that’s useful for determining whether or not your favorite team is leading their division, league or all of baseball. It can also report to you which team is leading baseball, a league, or a division as well as print out a nice leaderboard with sort and filtering options.

Some results:

$ leader is minnesota-twins -l && t update "Today is the 27th of June and the Minnesota Twins have the best record in the American League."
Today, the 27th day of June, 2019:
The Minnesota Twins are the leaders of the AL C division.
The Minnesota Twins are the leaders of the AL.
The Minnesota Twins are not the best team in baseball. They are #2.
Tweet posted by @schlazor.

Run `t delete status 1144439948352692224` to delete.

Using it isn’t quite as straightforward as installing an app on your phone, but keep reading and I’ll walk you through it if you’re interested.

First, you’ll need to sign up for the xmlstats API. Click the request link on that page and do the things it asks you to do.

You’ll also need a Twitter account if you want to post updates to Twitter. You’ll also need to create a Twitter application so you can use the command line twitter client, t, but we’re getting ahead of ourselves a bit.

You’re going to need to install ruby if you don’t already have it. If you have a Mac, you might already have it. Try the following in a terminal:

$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

If you got a similar result, you’re good to go. If not, you’ll need a ruby. For Linux and Mac I recommend rbenv. For Windows your easiest path is RubyInstaller. You want the ruby + devkit option.

Okay, now that you’ve got a ruby, let’s install the gems we need:

$ gem install leaderbrag t
...

There’s a good chance that won’t work, though, likely due to you not having write access to wherever your ruby is configured to install gems. Mac/Linux folk have the option of resolving that issue with sudo but a better idea is to change your GEM_HOME environment variable to something in your home directory, like ~/.ruby/gems. Doing that in bash on Mac/Linux means editing ~/.bash_profile and adding a line like:

export GEM_HOME=/Users/$USER/.ruby/gems

That’s on a Mac. Most Linux distros use /home rather than /Users, so take note of that if that’s what you’re using. Next, actually make that directory, then source that file to apply it:

$ mkdir -p ~/.ruby/gems
$ source ~/.bash_profile

Mac and Linux users, you should now be able to get those gems installed. Windows users, you’ll have to edit your environment variable by right clicking on your computer and going to properties and editing a thing with a thing and of course this is how you did it in Windows XP and I have no idea if it’s changed but also I bet RubyInstaller has defaults that make it so you can install gems without issue.

Once the gems are installed, we need to set some more environment variables, so Linux/Mac users will need to edit ~/.bash_profile again (or the appropriate config file for your chosen shell) and add something like this:

export PATH=$PATH:~/.ruby/gems/bin
export XMLSTATS_API_KEY="abc-123-def-456-ghi"
export XMLSTATS_CONTACT_INFO="baseball-lover@example.com"

The first line adds executables provided with gems to your PATH, so adjust it to point to the bin directory inside wherever you decided to install gems to. The second is the API key you got when signing up for xmlstats. The third is your email address and is used so Erik, the guy who runs xmlstats, knows who to contact if you break something. Source your ~/.bash_profile file again to apply the changes to your environment.

Windows users, you’ll need to set environment variables too. That document is for Java and mostly talks about changing the PATH variable but I bet you can figure it out.

OK! Now you can actually use the leader command!

$ leader find
Today, the 27th day of June, 2019:
The Los Angeles Dodgers are the leaders of the NL W division.
The Los Angeles Dodgers are the leaders of the NL.
The Los Angeles Dodgers are the best team in baseball.

What is this nonsense? Why aren’t the Twins leading all of baseball? Well sadly we haven’t been leading all of baseball since the 19th of June. Anyway, check out leaderbrag’s README for more examples and/or consult the extremely adequate help provided with the tool by invoking the -h argument.

Next we have to configure the t application so we can post to Twitter. This is pretty well documented over in their README, except that you’ll have to edit one of the files in the twitter gem until #878 is fixed.

Assuming you get that working, you’re all set. Write yourself a script or just modify my prior example, then call it from cron or at or Task Scheduler or whatever it’s called on Windows and you too can automatically tweet whenever your favorite baseball team is better than some or all of the other teams.

Oh also if you run into rate limit errors, as suggested in the README you can install and run redis and set the XMLSTATS_CACHER environment variable to ‘redis’ to cut down on the number of requests made to xmlstats.