Skip to content

Instantly share code, notes, and snippets.

@col
Last active August 29, 2015 14:09
Show Gist options
  • Save col/6ab59285615b1991a0f9 to your computer and use it in GitHub Desktop.
Save col/6ab59285615b1991a0f9 to your computer and use it in GitHub Desktop.
Notes for creating a JSON API using Sinatra, SQL, DataMapper, Hal and hosted on Heroku.

Lightweight Ruby JSON API

Step 4 - Add a SQL data store

Firstly, lets add the heroku-postgresql addon to our project.

heroku addons:add heroku-postgresql

Add Postgres to the Gemfile for Production

gem 'data_mapper'
group :production do
 gem 'pg'
 gem 'dm-postgres-adapter'
end

and SQLite for Development

group :development do
 gem 'sqlite3'
 gem 'dm-sqlite-adapter'
end

and install

Did you know? You type bundle rather then bundle install as it will run the install task by default.

bundle

This will fail in you don't have Postgres installed. Install it with:

brew install postgresql
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

I also recommend installing a Postgres GUI such as pgAdmin

http://www.pgadmin.org/download/macosx.php

Add this just under the require statements in the cocktail_api.rb file.

require 'data_mapper'

class Drink
  include DataMapper::Resource
  property :id, 	Serial
  property :name,   String
end

configure do
  DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{Dir.pwd}/development.sqlite3"))    
  DataMapper.auto_upgrade!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment