Module: HTTParty::Sober::ClassMethods

Defined in:
lib/httparty/sober.rb

Constant Summary collapse

@@cache_store =
nil
@@cache_options =
{}

Instance Method Summary collapse

Instance Method Details

#cache(cache_options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/httparty/sober.rb', line 12

def cache(cache_options={})
  @@cache_store = cache_options.delete(:store)
  APICache.store = @@cache_store if @@cache_store
  @@cache_options = cache_options
end

#cache!(cache_options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/httparty/sober.rb', line 18

def cache!(cache_options={})
  cache(cache_options)
  self.instance_eval do
    alias :get_without_caching :get
    alias :get :get_with_caching
  end
end

#get_with_caching(path, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/httparty/sober.rb', line 26

def get_with_caching(path, options={})
  cache_options = (@@cache_options || {}).merge(options[:cache] || {})
  query = options[:query] ||= {}
  key = Digest::MD5.hexdigest("#{path.to_s}-#{query.collect{|e| e.join("=")}.join("&")}")
  APICache.get(key,cache_options) do 
    perform_request(Net::HTTP::Get, path, options)
  end
end