class RemoteResource::StorageEntry
A storage entry closely resembles a network response. This seeks to counteract the impedance mismatch because API responses are done on the resource level and we want to query storages at the attribute level. Headers are also handled on the resource (response) level as well, and thus apply for many attributes.
Attributes
data[R]
headers[R]
Public Class Methods
from_response(response)
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 12 def self.from_response(response) new(response.headers, response.data) end
new(headers, data)
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 18 def initialize(headers, data) @headers = headers.try(:to_hash) || {} @data = data.try(:to_hash) || {} end
Public Instance Methods
cache_control()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 30 def cache_control @cache_control ||= CacheControl.new(headers['cache-control']) end
data?()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 48 def data? !data.empty? end
exists?()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 52 def exists? !headers.empty? || data? end
expired?()
click to toggle source
TODO: Extract this and make it better See: www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4 Cache-Control (http 1.1) overrides Expires header (http: 1.0)
# File lib/remote_resource/storage/storage_entry.rb, line 37 def expired? if cache_control.must_revalidate? true elsif cache_control.max_age expire = DateTime.parse(headers['date']) + cache_control.max_age.seconds Time.now > expire else false end end
headers_for_validation()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 56 def headers_for_validation v_headers = {} v_headers['If-None-Match'] = headers['etag'] if headers['etag'] if headers['last-modified'] v_headers['If-Modified-Since'] = headers['last-modified'] end v_headers end
to_hash()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 23 def to_hash {}.tap do |hash| hash[:data] = @data unless @data.size == 0 hash[:headers] = @headers unless @headers.size == 0 end end
validateable?()
click to toggle source
# File lib/remote_resource/storage/storage_entry.rb, line 65 def validateable? headers.key?('last-modified') || headers.key?('etag') end