improve caching headers
This commit is contained in:
parent
0ca84da651
commit
6252deadee
|
@ -3,6 +3,16 @@ require 'travis/api/app'
|
||||||
class Travis::Api::App
|
class Travis::Api::App
|
||||||
module Extensions
|
module Extensions
|
||||||
module Scoping
|
module Scoping
|
||||||
|
module Helpers
|
||||||
|
def scope
|
||||||
|
env['travis.scope'].to_sym
|
||||||
|
end
|
||||||
|
|
||||||
|
def public?
|
||||||
|
scope == :public
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.registered(app)
|
def self.registered(app)
|
||||||
app.set default_scope: :public, anonymous_scopes: [:public]
|
app.set default_scope: :public, anonymous_scopes: [:public]
|
||||||
app.helpers(Helpers)
|
app.helpers(Helpers)
|
||||||
|
@ -16,8 +26,9 @@ class Travis::Api::App
|
||||||
headers['X-Accepted-OAuth-Scopes'] = name.to_s
|
headers['X-Accepted-OAuth-Scopes'] = name.to_s
|
||||||
|
|
||||||
if scopes.include? name
|
if scopes.include? name
|
||||||
|
env['travis.scope'] = name
|
||||||
headers['Vary'] = 'Accept'
|
headers['Vary'] = 'Accept'
|
||||||
headers['Vary'] << ', Authorization' if name == :public
|
headers['Vary'] << ', Authorization' unless public?
|
||||||
true
|
true
|
||||||
elsif env['travis.access_token']
|
elsif env['travis.access_token']
|
||||||
halt 403, "insufficient access"
|
halt 403, "insufficient access"
|
||||||
|
|
|
@ -14,12 +14,11 @@ module Travis::Api::App::Responders
|
||||||
private
|
private
|
||||||
|
|
||||||
def cache_control
|
def cache_control
|
||||||
if final?
|
mode = [endpoint.public? ? :public : :private]
|
||||||
endpoint.expires 31536000, :public # 1 year
|
mode << :must_revalidate unless final?
|
||||||
elsif updated_at?
|
endpoint.expires(31536000, *mode) # 1 year
|
||||||
endpoint.cache_control :public, :must_revalidate
|
endpoint.etag resource.cache_key if cache_key?
|
||||||
endpoint.last_modified resource.updated_at
|
endpoint.last_modified resource.updated_at if updated_at?
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def final?
|
def final?
|
||||||
|
@ -30,6 +29,10 @@ module Travis::Api::App::Responders
|
||||||
resource.respond_to?(:updated_at) && resource.updated_at
|
resource.respond_to?(:updated_at) && resource.updated_at
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cache_key?
|
||||||
|
resource.respond_to?(:cache_key) && resource.cache_key
|
||||||
|
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.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user