API
Libraries & Example Code
We'll be adding more code examples in the future and libraries as people release them. If you've built a client in another language, please let us know so others can benefit too.
Libraries
If you've built a client in another language, please let us know so we can link to it here!
Ruby
This ruby client supports most endpoints in the API. Please see the examples below for how to use it.
git clone git@github.com:dailymile/dailymile-ruby.git
A Ruby OAuth Example App
We've built a small example Sinatra app to show you how getting an access token using OAuth works. It uses the dailymile-ruby client and is an example of consuming the API using the authorization code flow, which is typical for a Web app. You can find the project on github here:
Using the Ruby Client
Before you get started, make sure you're familiar with the documentation. If you want to access any of the resources that require authorization, you'll need to register your app in order to use OAuth.
Accessing Public Resources
For resources that aren't protected, you can use the Ruby client to send HTTP GET to them or use the special entries method.
# get the representation for user with username ben Dailymile::Client.get '/people/ben' # get most recent public entries Dailymile::Client.entries # get the 2nd page of entries near latitude 37.77916, longitude -122.420049) Dailymile::Client.entries :nearby, 37.77916, -122.420049, :page => 2 # get the 2nd page of ben's stream Dailymile::Client.entries 'ben', :page => 2
Accessing Protected Resources
In order to access protected resources on behalf of a user, they'll have to authorize an access token for us. (You can read more about this in the Authentication section of the docs.) Make sure you've registered an app and have your Client ID, Client Secret, and Callback URL handy. We built a simple example Sinatra app to show off how to get this access token.
require 'dailymile'
# first set your app's client credentials
Dailymile::Client.set_client_credentials <Client ID>, <Client Secret>
authorize_url = Dailymile::Client.oauth_client.web_server.authorize_url(
:redirect_uri => <Callback URL>, :response_type => 'code')
# ... redirect authorizing user to the authorize_url ...
# in your Callback URL, you'll want this code:
Dailymile::Client.set_client_credentials <Client ID>, <Client Secret>
access_token = Dailymile::Client.oauth_client.web_server.get_access_token(
params[:code], :redirect_uri => <Callback URL>,
:grant_type => 'authorization_code')
puts "token: #{access_token.token}" # we've got our token!
Now that we have an access token for a user, we'll want to store it. We'll use this access token and create a client to access protected resources on their behalf. The client object will allow you to send HTTP requests to the API with these methods client.get, client.post, client.put or client.delete passing the path and any parameters. Here's how we use it:
require 'dailymile'
Dailymile::Client.set_client_credentials <Client ID>, <Client Secret>
client = Dailymile::Client.new(<Access Token>)
# the first step is to get the user who authorized the token
user = client.get '/people/me'
# we can get their friends
friends = client.get '/people/me/friends'
# get their "You and Friends" stream
entries = client.get '/entries/friends'
# let's pretend we just finished a 5k, and PR'd with a time of 22'30"
entry = client.post('/entries', {
:message => "Race went great. I set a PR!",
:workout => {
:type => 'running', :duration => 1350,
:distance => { :value => 5, :units => 'kilometers' }
}
})
entry_id = entry['id']
# whoops, we forgot about our celebratory donut. let's add a comment
client.post "/entries/#{entry_id}/comments",
:body => "Oh ya, enjoyed a chocolate donut afterwards :)"
# the entry will be in our feed, but we can view it to verify that it's correct
entry = client.get "/entries/#{entry_id}"
puts entry.inspect
# because this is just test data, we need a way to remove an entry
client.delete "/entries/#{entry_id}"
We'll be adding more example code in the future!
