This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment