Class: MailChimpCached

Inherits:
MailChimp show all
Defined in:
lib/mc/mailchimp_cached.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey, options = {}) ⇒ MailChimpCached

Returns a new instance of MailChimpCached.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mc/mailchimp_cached.rb', line 6

def initialize(apikey, options={})
  super(apikey, options)

  # configure filecache
  cache_dir = File.join(File.expand_path(ENV['HOME']), ".mailchimp-cache")

  # expire in one day
  expiry = 60 * 60 * 24

  @cache = FileCache.new(apikey, cache_dir, expiry)
  @skip_cache = options[:skip_cache]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mc/mailchimp_cached.rb', line 25

def method_missing(method_name, *args)
  puts "DEBUG: Calling '#{method_name}(#{args})'..." if @options[:debug]
  cache_key = Digest::SHA1.hexdigest(method_name.to_s + args.to_s)

  if result = @cache.get(cache_key) and not @skip_cache
    puts "DEBUG: USING CACHED RESULT" if @options[:debug]
    return result
  else
    category = method_name.to_s.split('_').first
    method   = method_name.to_s.split('_')[1..-1].join('_')

    throw "error: don't support caching export" if category == "export"
    throw "error: don't support caching send" if method == "send"

    result = @api.send(category).method_missing(method, *args)
    @cache.set(cache_key, result)

    return result
  end
end

Instance Method Details

#cache_value(key, value) ⇒ Object



19
20
21
# File 'lib/mc/mailchimp_cached.rb', line 19

def cache_value(key, value)
  puts "cache returns: #{@cache.set(key, value)}"
end