travis-api/lib/travis/api/app/responders/service.rb
2012-10-09 20:51:09 +02:00

33 lines
832 B
Ruby

module Travis::Api::App::Responders
class Service < Base
def apply?
resource.respond_to?(:run)
end
def apply
# TODO add caching headers depending on the resource
data = result
halt 404 if data.nil?
flash.concat(data.messages) if resource.respond_to?(:messages)
data
end
private
# Services potentially return all sorts of things
# If it's a string, true or false we'll wrap it into a hash.
# If it's an active record instance or scope we just pass it on
# so it can be processed by the json responder.
# If it's nil we also pass it but immediately yield not_found.
def result
case result = resource.run
when String, true, false
{ result: result }
else
result
end
end
end
end