Class: ActiveResource::Connection

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
lib/active_resource/conditional_get/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/active_resource/conditional_get/connection.rb', line 7

def cache
  @cache
end

Instance Method Details

#get_with_conditional_get(path, headers = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_resource/conditional_get/connection.rb', line 9

def get_with_conditional_get(path, headers = {})
  return get_without_conditional_get(path, headers) unless cache
  begin
    cached_response = cache.read(cache_key(path))
    if cached_response
      headers.update('If-None-Match' => cached_response['eTag']) if cached_response['eTag']
      headers.update('If-Modified-Since' => cached_response['Cache-Control']) if cached_response['Cache-Control']
    end

    response = get_without_conditional_get(path, headers)
    
    return cached_response if (response.try(:code).to_i == 304) && cached_response
    
    if (response['Cache-Control'] =~ /public/) && (response['eTag'] || response['Last-Modified'])
      if response['Cache-Control'] =~ /max-age=(\d+)/ && $1.to_i > 0
        cache.write(cache_key(path), response, :expires_in => $1.to_i)
      end
    end
    return response
  rescue ActiveResource::TimeoutError => e
    cached_response || raise(e)
  end
end