Browse files

add restaurant model

  • Loading branch information...
1 parent d89b0ee commit 4610966a41ea2af752cbe504cf9ac8666ffcbf68 @brentvatne committed Jun 3, 2012
Showing with 50 additions and 3 deletions.
  1. +4 −0 app/controllers/api_controller.rb
  2. +10 −0 app/models/restaurant.rb
  3. +30 −0 spec/api_spec.rb
  4. +6 −3 spec/spec_helper.rb
View
4 app/controllers/api_controller.rb
@@ -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
View
10 app/models/restaurant.rb
@@ -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
View
30 spec/api_spec.rb
@@ -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
View
9 spec/spec_helper.rb
@@ -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

0 comments on commit 4610966

Please sign in to comment.