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



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

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

Instance Method Details

#perform_with_cachingObject



23
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
# File 'lib/httparty/httpcache.rb', line 23

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
        retrieve_and_store_backup
      end
    end
  else
    log_message("Caching off")
    perform_without_caching
  end
end