require 'json' module RestAssured module DoubleRoutes def self.included(router) router.helpers do def verbs RestAssured::Models::Double::VERBS end end router.get '/' do redirect to('/doubles') end router.get '/doubles' do @doubles = Models::Double.all haml :'doubles/index' end router.get '/doubles/new' do @double = Models::Double.new haml :'doubles/new' end router.get '/doubles/:id.json' do |id| begin double = Models::Double.find(id) body double.to_json(:include => :requests) rescue ActiveRecord::RecordNotFound status 404 end end router.post /^\/doubles(\.json)?$/ do begin data = request.body.read d = JSON.parse(data)['double'] rescue d = params['double'] || { :fullpath => params['fullpath'], :content => params['content'], :description => params['description'], :verb => params['verb'], :status => params['status'] } d.merge!(:response_headers => params['response_headers']) if params['response_headers'] end @double = Models::Double.create(d) if browser? if @double.errors.blank? flash[:notice] = "Double created" redirect '/doubles' else flash.now[:error] = "Crumps! " + @double.errors.full_messages.join("; ") haml :'doubles/new' end else if @double.errors.present? status 422 body @double.errors.to_json else body @double.to_json end end end router.get %r{/doubles/(\d+)/edit} do |id| @double = Models::Double.find(id) haml :'doubles/edit' end router.put %r{/doubles/(\d+)} do |id| @double = Models::Double.find(id) if request.xhr? if params['active'] @double.active = params['active'] @double.save 'Changed' end elsif params['double'] @double.update_attributes(params['double']) if @double.save flash[:notice] = 'Double updated' redirect '/doubles' else flash[:error] = 'Crumps! ' + @double.errors.full_messages.join("\n") haml :'doubles/edit' end end end router.delete %r{/doubles/(\d+)} do |id| if Models::Double.destroy(id) flash[:notice] = 'Double deleted' redirect '/doubles' end end router.delete '/doubles/all' do status Models::Double.delete_all ? 200 : 500 end end end end