Awarding a Discourse badge from external data

Just for fun, every time I go to the gym I would like to award myself a workout badge. This is the final post in this little series, please checkout the previous posts for some more details.

In this post I’ll be creating a ruby file containing the code to check my online Gold’s Gym account, and code to talk to my Discourse instance and award myself a badge if I went to the gym that day.

Then I’ll create a cron job on my server to run daily that will call my ruby script.

One issue I realized when setting up the cron job is that my server is in UTC time, but I want the script to run at the end of the day Mountain Time. At first I thought I would have to handle this in the cron job somehow, but actually I can specify the date I pass to Gold’s Gym in ruby based on a specific timezone using the tzinfo gem.

@tz = TZInfo::Timezone.get('US/Mountain')

The golds check-in ruby script

For the golds check-in ruby script I setup two classes, GoldsCheckin and DiscourseBadge. At the bottom of the script I run golds_checkin.fetch_checkins and if I have a checkin for that day I assign myself a badge with discourse.grant_workout_badge:

require 'yaml'
require 'net/http'
require 'http-cookie'
require 'cgi'
require 'json'
require 'tzinfo'
require 'discourse_api'

class GoldsCheckin
  def initialize
    @config = YAML.load_file('config.yml')
    @jar =
    @tz = TZInfo::Timezone.get('US/Mountain')

  def login
    uri = URI('')

    Net::HTTP.start(, uri.port, use_ssl: true) do |http|
      req = uri
      req.form_data = { "j_username": @config['username'], "j_password": @config['password'] }
      res = http.request req
      res.get_fields('Set-Cookie').each do |value|
        @jar.parse(value, req.uri)

  def fetch_checkins
    today =
    low_date = CGI::escape(today.strftime("%m/%d/%Y"))
    high_date = CGI::escape(today.strftime("%m/%d/%Y"))
    uri = URI("{low_date}&highDate=#{high_date}")

    Net::HTTP.start(, uri.port, use_ssl: true) do |http|
      req =
      req['Cookie'] = HTTP::Cookie.cookie_value(@jar.cookies(uri))
      res = http.request req


class DiscourseBadge
  def initialize
    @config = YAML.load_file('discourse_config.yml')
    @client =['host'])
    @client.api_key = @config['api_key']
    @client.api_username = @config['api_username']

  def grant_workout_badge
    badge = {
      badge_id: 104,
      username: 'blake'

golds_checkin =
discourse =

response = golds_checkin.fetch_checkins

json = JSON.parse(response)
if json.count > 0

Create a script to call the ruby app

Now that we have our little ruby app let’s create a bash script that we can easily call from our cron job that will call our ruby app.

Create a new file in /usr/bin and call it workout with the following contents:


cd /golds_checkin && ruby app.rb

Now make sure our bash script is executable with:

chmod +x /usr/bin/workout

Create a cron job:

Now to finish it all off let’s create a cron job that will call our bash script. To create/edit a cron job run crontab -e:

0 5 * * * /usr/bin/workout

This should run our script once per day at 5 AM UTC, which will actually be 11:00 PM MST (the previous day). Usually I don’t go to gym that late, but this will capture if I ever go to gym in the evening after work.