Skip to content

Commit

Permalink
add restaurant model
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvatne committed Jun 4, 2012
1 parent d89b0ee commit 4610966
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require_relative '../models/user'
require_relative '../models/restaurant'

module LunchZone
class App < Sinatra::Application
get '/api/restaurants' do
Restaurant.all.map(&:attributes).to_json
# [ { :name => 'restaurant name', :id => 1232 }, { ... } ]
end

Expand Down
10 changes: 10 additions & 0 deletions app/models/restaurant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative "../../config/data_mapper"

module LunchZone
class Restaurant
include DataMapper::Resource

property :id, Serial
property :name, String, :unique => true
end
end
30 changes: 30 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'json'
require 'spec_helper'
require_relative '../app/app'
require_relative '../app/controllers/api_controller'
require 'rack/test'

module LunchZone
describe 'service' do
include Rack::Test::Methods

def app
App
end

describe 'GET /api/restaurants' do
before do
Restaurant.create(:name => 'QQ Sushi')
Restaurant.create(:name => 'Memphis Grill')
end

it 'gets all restaurants' do
get '/api/restaurants'
last_response.should be_ok
attributes = JSON.parse(last_response.body)
first_restaurant = attributes.first
first_restaurant['name'].should == 'QQ Sushi'
end
end
end
end
9 changes: 6 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
APP_ENV = 'test'

require 'rack/test'
require 'rspec'
require 'sinatra/base'
require_relative '../config/data_mapper'

module LunchZone
class App < Sinatra::Application
set :environment, :test
end
end

RSpec.configure do |config|
config.before(:each) { DataMapper.auto_migrate! }
config.mock_with :mocha
Expand Down

0 comments on commit 4610966

Please sign in to comment.