Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Last active December 19, 2015 02:48
Show Gist options
  • Save ashleygwilliams/5885184 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/5885184 to your computer and use it in GitHub Desktop.
exercise for creating resourceful routes in sinatra with datamapper

citibike REST

FIRST, read this. ... if you want to dig deep, this tutorial is also worthwhile.

Following the example laid out in the reading, create a RESTful resource for the citibike stations.

Resources

Instructions

Create a RESTful resource

  1. Create a Datamapper model for a station in a file called station.rb. This model should include all the details that are available in the JSON file. Here is an example of a station in the data:
  {   "id":72,
      "stationName":"W 52 St & 11 Ave",
      "availableDocks":37,
      "totalDocks":39,
      "latitude":40.76727216,
      "longitude":-73.99392888,
      "statusValue":"In Service",
      "statusKey":1,
      "availableBikes":1,
      "stAddress1":"W 52 St & 11 Ave",
      "stAddress2":"",
      "city":"",
      "postalCode":"",
      "location":"",
      "altitude":"",
      "testStation":false,
      "lastCommunicationTime":null,
      "landMark":""
  }
  1. Now, following the example laid out in the reading create a complete set of routes for the station resource. /stations/, /stations/1, /stations/new, /stations/edit/1, /stations/delete/1

JSON -> SQL

  1. Update your app to now pull the data from the API endpoint, http://citibikenyc.com/stations/json, instead of the static JSON file.
  2. Refactor your app to load the data in config.ru like you did during the Playlister lab. To do this, you'll need to create a model to load the data that takes the path as a parameter to its initialize method, and will load the data into the database during it's call method.

If you feel lost, follow the patterns you used here and here.

Views, with Partials!

  1. Build out all the views you need. In the reading the views are written in HAML. You can learn about HAML while you translate it into ERB.

The views in the example use partials. In order to use partials, you'll need to add the following code to the bottom of your app.rb file, inside your Citibike module and App class.

  #app.rb
  helpers do
    def partial(view)
      erb view, :layout => false
    end
  end

A helper is just a function. This function takes a view name (usually a symbol) as a parameter, and returns the rendered erb view. You can call this helper in all of your views, e.g. <%= partial(:index) %>

If you want to learn more about this, read this article, or view this github repo

Maps

  1. Create a map using Leaflet that plots all the citibike locations and add it to your list view.
  2. Create a map on each individual show view that plots the location of the individual station.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment