Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created July 13, 2012 03:35
Show Gist options
  • Save abevoelker/3102551 to your computer and use it in GitHub Desktop.
Save abevoelker/3102551 to your computer and use it in GitHub Desktop.
require 'sinatra'
require './models'
get '/customers' do
Customer.all.to_json
end
get '/customer/:cust_num' do |cust_num|
@customer = Customer.get(cust_num)
if @customer
@customer.to_json
else
not_found 'unknown customer'
end
end
post '/customer' do
next_id = Customer.last.cust_num + 1
@customer = Customer.create(params.merge(:cust_num => next_id))
@customer.to_json
end
put '/customer/:cust_num' do |cust_num|
@customer = Customer.get(cust_num)
if @customer
@customer.destroy && Customer.create(request.params.merge({'cust_num' => cust_num}))
@customer.to_json
else
not_found 'unknown customer'
end
end
patch '/customer/:cust_num' do |cust_num|
@customer = Customer.get(cust_num)
if @customer
@customer.attributes = request.params.reject{|k,v| k == 'cust_num'}
@customer.save
@customer.to_json
else
not_found 'unknown customer'
end
end
delete '/customer/:cust_num' do |cust_num|
@customer = Customer.get(cust_num)
if @customer
@customer.destroy
else
not_found 'unknown customer'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment