Class: RemoteResource::StorageEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_resource/storage/storage_entry.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, data) ⇒ StorageEntry

Returns a new instance of StorageEntry.



18
19
20
21
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/remote_resource/storage/storage_entry.rb', line 16

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/remote_resource/storage/storage_entry.rb', line 16

def headers
  @headers
end

Class Method Details

.from_response(response) ⇒ Object



12
13
14
# File 'lib/remote_resource/storage/storage_entry.rb', line 12

def self.from_response(response)
  new(response.headers, response.data)
end

Instance Method Details

#cache_controlObject



30
31
32
# File 'lib/remote_resource/storage/storage_entry.rb', line 30

def cache_control
  @cache_control ||= CacheControl.new(headers['cache-control'])
end

#data?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/remote_resource/storage/storage_entry.rb', line 48

def data?
  !data.empty?
end

#exists?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/remote_resource/storage/storage_entry.rb', line 52

def exists?
  !headers.empty? || data?
end

#expired?Boolean

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)

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# 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_validationObject



56
57
58
59
60
61
62
63
# 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_hashObject



23
24
25
26
27
28
# 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?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/remote_resource/storage/storage_entry.rb', line 65

def validateable?
  headers.key?('last-modified') || headers.key?('etag')
end