Module: HTTParty::HTTPCache

Defined in:
lib/httparty/httpcache.rb

Defined Under Namespace

Classes: NoResponseError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
21
22
# File 'lib/httparty/httpcache.rb', line 18

def self.included(base)
  base.class_eval do
    alias_method_chain :perform, :caching
  end
end

Instance Method Details

#perform_with_cachingObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httparty/httpcache.rb', line 24

def perform_with_caching
  if cacheable?
    if response_in_cache?
      log_message("Retrieving response from cache")
      response_from(response_body_from_cache)
    else
      validate
      begin
        httparty_response = timeout(timeout_length) do
          perform_without_caching
        end
        httparty_response.parsed_response
        if httparty_response.response.is_a?(Net::HTTPSuccess)
          log_message("Storing good response in cache")
          store_in_cache(httparty_response.body)
          store_backup(httparty_response.body)
          httparty_response
        else
          retrieve_and_store_backup(httparty_response)
        end
      rescue *exceptions => e
        if exception_callback && exception_callback.respond_to?(:call)
          exception_callback.call(e, redis_key_name, normalized_uri)
        end
        retrieve_and_store_backup
      end
    end
  else
    log_message("Caching off")
    perform_without_caching
  end
end