Skip to content

Instantly share code, notes, and snippets.

@renatosousafilho
Created August 28, 2012 14:54
Show Gist options
  • Save renatosousafilho/3498729 to your computer and use it in GitHub Desktop.
Save renatosousafilho/3498729 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'active_record'
require 'json'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:database => 'autofood_web_development',
:username => 'root',
:password => 'root'
)
class Category < ActiveRecord::Base
end
class Product < ActiveRecord::Base
end
class Service < ActiveRecord::Base
end
class Request < ActiveRecord::Base
end
get '/categories' do
@categories = Category.all
@categories.to_json
end
get '/categories/:id' do
content_type :json
@categories = Category.find(params[:id])
@categories.to_json
end
get '/categories/:id/products' do
@products = Product.where('category_id=?',params[:id])
@products.to_json
end
get '/services' do
Service.all.to_json
end
get '/services/:id' do
@service = Service.find(params[:id])
@service.to_json
end
get '/services/:id/requests' do
@requests = Request.where('service_id=?',params[:id])
@requests.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment