secret
Last active

  • Download Gist
handler.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class ProvisionAPIHandler < Sinatra::Base
post "/heroku/resources" do
require_valid_credentials!
provision_request.process(json_data, self)
end
 
def provisioned_okay results
content_type :json
body render_to_json(results)
end
 
def request_nonsensical reason
content_type :json
status 400
body render_to_json({error: reason})
end
 
def service_unavaliable
content_type :json
status 503
body render_to_json({error: HELPFUL_ERROR})
end
 
private
def provision_request
@provision_request ||= ProvisionRequest.new(...)
end
 
def json_data
@json_data ||= JSON.parse(request.body.read)
end
end
interactor.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class ProvisionRequest
def process request, responder
when_not_semantically_valid request[:kind], request[:amount] do |error|
responder.request_nonsensical error
return
end
begin
prov = domain_model.provision_service! request[:kind], request[:amount]
rescue SomeKindOfError => e
responder.service_unavaliable
else
responder.provisioned_okay {resource_id: prov.id, locator: prov.location}
end
end
 
# ...
end
strawman.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Strawman < Sinatra::base
post "/foo" do
begin
data = validate params
create_from! data
redirect_to "/foo/#{data[:id]}"
rescue InvalidData => e
render :some_form, locals: { errors: e.errors }
rescue SemanticError => e
render :some_form, locals: { alert: e.description }
rescue ServiceUnavaliable => e
render :sorry_please_come_back_later
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.