add http headers for services that respond to caching info

This commit is contained in:
Sven Fuchs 2012-10-11 05:04:27 +02:00
parent 02c503ae19
commit e6b44ff1eb

View File

@ -5,20 +5,37 @@ module Travis::Api::App::Responders
end end
def apply def apply
# TODO add caching headers depending on the resource cache_control
data = result result = normalize(resource.run)
flash.concat(data.messages) if data && data.respond_to?(:messages) flash.concat(resource.messages) if result && resource.respond_to?(:messages)
data result
end end
private private
def cache_control
if final?
endpoint.expires 31536000, :public # 1 year
elsif updated_at?
endpoint.cache_control :public, :must_revalidate
endpoint.last_modified resource.updated_at
end
end
def final?
resource.respond_to?(:final?) && resource.final?
end
def updated_at?
resource.respond_to?(:updated_at) && resource.updated_at
end
# Services potentially return all sorts of things # 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 a string, true or false we'll wrap it into a hash.
# If it's an active record or scope we just pass so it can be processed by the json responder. # If it's an active record or scope we just pass so it can be processed by the json responder.
# If it's nil we also pass it but yield not_found. # If it's nil we also pass it but yield not_found.
def result def normalize(result)
case result = resource.run case result
when String, true, false when String, true, false
{ result: result } { result: result }
else else
@ -27,4 +44,3 @@ module Travis::Api::App::Responders
end end
end end
end end