Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active October 2, 2018 02:21
Show Gist options
  • Save harrisonmalone/1565adcb9e1b7e6ca57e17f0be2b39ff to your computer and use it in GitHub Desktop.
Save harrisonmalone/1565adcb9e1b7e6ca57e17f0be2b39ff to your computer and use it in GitHub Desktop.

Strava API

This challenge is similar to what we did the other day with Stripe - using an API to extract data that we can then store in some way.

Today we'll be using the Strava API. They have decent documentation if you're interested.

What we want to use in this case is the List Athlete Activities GET request. This will return the activities of one specific Strava user. It needs an access_token to actually work which in this case you can use my own.

If you enter in this url into your web browser it will return a big JSON file while contains all of my running data. Notice the the id number and the access_token in the url. I recommend you open JSON files in Firefox as it has a built in JSON reader.

https://www.strava.com/api/v3/athletes/24750713/activities?access_token=a8791b703420070c51af9fe792196bb8d2422b47

So now we have the JSON we need to access it in a Ruby file. Touch a file called strava_api.rb in a directory and save the JSON url as a string in that file.

We can then use NET:HTTP and JSON which are built in ruby modules to create a data structure from the JSON in the url. Google how to do this if you get stuck.

require 'json'
require 'net/http'

Once this is working I then want you to do a few things:

  1. Write a function to create an array of arrays(runs) which contain date, name, distance, moving_time, average_heartrate and max_heartrate
  2. Write a function to make the date something more readable (using the date class strftime)
  3. Now you have an array of arrays(runs) I want you to push each run as a row into a csv

Beast mode

Instead of storing these runs in a csv I want you to store them in a database. Create a db file and use db.execute("SQL") to insert the rows into your database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment