Class: Devise::Models::Mailchimp::MailchimpListApiMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/devise_mailchimp/mailchimp_list_api_mapper.rb

Defined Under Namespace

Classes: ListLookupError

Constant Summary collapse

LIST_CACHE_KEY =
"devise_mailchimp/lists"

Instance Method Summary collapse

Constructor Details

#initialize(api_key, double_opt_in) ⇒ MailchimpListApiMapper

craete a new ApiMapper with the provided API key



10
11
12
13
# File 'lib/devise_mailchimp/mailchimp_list_api_mapper.rb', line 10

def initialize(api_key, double_opt_in)
  @api_key = api_key
  @double_opt_in = double_opt_in
end

Instance Method Details

#name_to_id(list_name) ⇒ Object

looks the name up in the cache. if it doesn’t find it, looks it up using the api and saves it to the cache



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/devise_mailchimp/mailchimp_list_api_mapper.rb', line 16

def name_to_id(list_name)
  load_cached_lists
  if @lists.has_key?(list_name)
    return @lists[list_name]
  else
    list_id = hominid.find_list_id_by_name(list_name)
    if list_id.nil?
      raise ListLookupError
    else
      @lists[list_name] = list_id
      save_cached_lists
      return @lists[list_name]
    end
  end
end

#subscribe_to_lists(list_names, email) ⇒ Object

subscribes the user to the named mailing list(s). list_names can be the name of one list, or an array of several.

NOTE: Do not use this method unless the user has opted in.



36
37
38
39
40
41
42
# File 'lib/devise_mailchimp/mailchimp_list_api_mapper.rb', line 36

def subscribe_to_lists(list_names, email)
  list_names = [list_names] unless list_names.is_a?(Array)
  list_names.each do |list_name|
    list_id = name_to_id(list_name)
    hominid.list_subscribe(list_id, email, {}, 'html', @double_opt_in, true, true, false)
  end
end

#unsubscribe_from_lists(list_names, email) ⇒ Object

unsubscribe the user from the named mailing list(s). list_names can be the name of one list, or an array of several.



46
47
48
49
50
51
52
53
# File 'lib/devise_mailchimp/mailchimp_list_api_mapper.rb', line 46

def unsubscribe_from_lists(list_names, email)
  list_names = [list_names] unless list_names.is_a?(Array)
  list_names.each do |list_name|
    list_id = name_to_id(list_name)
    hominid.list_unsubscribe(list_id, email, false, false, false)
    # don't delete, send goodbye, or send notification
  end
end