Module: Chimpactions

Defined in:
lib/railtie.rb,
lib/chimpactions.rb,
lib/chimpactions/list.rb,
lib/chimpactions/setup.rb,
lib/chimpactions/action.rb,
lib/chimpactions/engine.rb,
lib/chimpactions/utility.rb,
lib/chimpactions/version.rb,
lib/chimpactions/notifier.rb,
lib/chimpactions/exception.rb,
lib/chimpactions/subscriber.rb,
lib/generators/chimpactions/install/install_generator.rb,
lib/generators/chimpactions/customize/customize_generator.rb,
lib/generators/chimpactions/migration/migration_generator.rb

Defined Under Namespace

Modules: Generators, Setup, Subscriber, Utility Classes: Action, ArgumentError, AuthError, ConnectionError, Engine, List, ListNotifier, MailChimpError, NotFoundError, Railtie, SetupError

Constant Summary collapse

VERSION =
"0.0.4"
@@observers =
[]
@@merge_map =
Hash.new
@@mailchimp_api_key =
"your_mailchimp_api_key"
@@mailchimp_ses_key =
"your_mailchimp_ses_key"
@@registered_classes =
Array.new
@@default_double_optin =
false
@@default_send_welcome =
true
@@default_email_type =
'html'
@@default_update_existing =
true
@@actions =
Array.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_observer(observer) ⇒ Object

Observer pattern untility.



24
25
26
# File 'lib/chimpactions.rb', line 24

def self.add_observer(observer)
  @@observers << observer
end

.available_lists(force = false) ⇒ Hash

Queries your MailChimp account for available lists. TODO: What if there are an unmanageable (50+) number of lists? Paginate?

Returns:

  • (Hash)

    ChimpActions::List objects available in your account.



149
150
151
152
153
154
155
# File 'lib/chimpactions.rb', line 149

def self.available_lists(force=false)
  if force
    @available_lists = load_lists.map{|raw_list| Chimpactions::List.new(raw_list)}
  else
    @available_lists ||= load_lists.map{|raw_list| Chimpactions::List.new(raw_list)}
  end
end

.change_account(new_api_key) ⇒ Object

# Change the MailChimp used by the system. Notifies all list objects and reloads the available lists from the new account.

Parameters:

  • A (String)

    new API key



101
102
103
104
105
106
# File 'lib/chimpactions.rb', line 101

def self.(new_api_key)
  self.mailchimp_api_key = new_api_key
  self.socket = Gibbon::API.new(self.mailchimp_api_key)
  notify_observers self
  self.available_lists(true)
end

.find_list(params) ⇒ Object

Find a list by any list attribute

Parameters:

  • :attribute (Hash)

    > ‘value’

Returns:

  • Chimpactions::List object



194
195
196
# File 'lib/chimpactions.rb', line 194

def self.find_list(params)
  available_lists.find{|list| list.method(:params[0].to_s).call == params[1]}
end

.for(klass_name) ⇒ Object

Assign a local class to the Chimpactions module.

  • Must respond_to? ‘email’

  • For methods defined in the Chimpactions merge_map

– can to respond_to? each method – if it does not, Chimpactions will not send the merge variable or value.

Parameters:

  • The (Object)

    local object to inherit Chimpactions::Subscriber methods.

Raises:



91
92
93
94
95
96
# File 'lib/chimpactions.rb', line 91

def self.for(klass_name)
  klass =  Kernel.const_get(klass_name.to_s.capitalize).new
  raise Chimpactions::SetupError.new("The #{klass.name} class MUST at least respond to 'email' !") if !klass.respond_to?(:email)
  Kernel.const_get(klass_name.to_s.capitalize).send(:include, Chimpactions::Subscriber)
  @@registered_classes << klass if !@@registered_classes.include? klass
end

.list(list) ⇒ Chimpactions::List

Searches the MailChimp list hash by id, web_id, name

Parameters:

Returns:

Raises:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chimpactions.rb', line 172

def self.list(list)
  case list.class.name
    when "Chimpactions::List"
      #if it's a List, just send it back...
      return_list = [list]
    when "String"
      # The List.name , id, or web_id ?
      return_list = Chimpactions.available_lists.select{|l| l.id == list}
      return_list = Chimpactions.available_lists.select{|l| l.web_id == list.to_i} if return_list.empty?
      return_list = Chimpactions.available_lists.select{|l| l.name == list} if return_list.empty?
    when "Fixnum"
      return_list = Chimpactions.available_lists.select{|l| l.web_id == list.to_i}
    else 
      return_list = []
  end
  raise Chimpactions::NotFoundError.new("Could not locate #{list} in your account. ") if return_list.empty?
  return_list[0]
end

.list_idsHash

Utility for a hash of lists keyed by ID.

Returns:

  • (Hash)

    Lists available in your MailChimp account, keyed by id



206
207
208
# File 'lib/chimpactions.rb', line 206

def self.list_ids
  available_lists.sort{|a,b| a.id <=> b.id}
end

.lists_by_nameHash

Utility for a hash of lists keyed by name in your MailChimp account.

Returns:

  • (Hash)

    Lists available in your MailChimp account, keyed by name.



200
201
202
# File 'lib/chimpactions.rb', line 200

def self.lists_by_name
   available_lists.sort{|a,b| a.name <=> b.name}
end

.load_listsObject

Query the Mailchimp server for lists in this account # @return [Hash] Data for the query



159
160
161
162
163
164
165
166
167
# File 'lib/chimpactions.rb', line 159

def self.load_lists
  # send the query
  response = socket.lists
  if !response['error']
    response['data']
  else
    raise MailChimpError.new("#{response['error']} (#{response['code']})")
  end
end

.notify_observers(mod) ⇒ Object

Observer pattern notify untility.



29
30
31
32
33
# File 'lib/chimpactions.rb', line 29

def self.notify_observers(mod)
  @@observers.each do |o|
    o.update(mod)
  end
end

.registered_classClassName

The registered class

Returns:

  • (ClassName)


110
111
112
# File 'lib/chimpactions.rb', line 110

def self.registered_class
  self.registered_classes[0]
end

.registered_class_nameObject



115
116
117
# File 'lib/chimpactions.rb', line 115

def self.registered_class_name
  self.registered_classes[0].class.name
end

.setup(config) ⇒ Object

Default setup for Chimpactions. Run rails generate chimpactions_install to create a fresh initializer with configuration options.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/chimpactions.rb', line 120

def self.setup(config)
 # puts config
  if config['mailchimp_api_key'] == 'your_mailchimp_api_key' || config['local_model'] == 'YourLocalModel'
    raise Chimpactions::SetupError.new("You must customize initializers/chimpactions.yml.")
  end
 #   klass = Kernel.cont_get(config['local_model'].to_s.capitalize)
#    @@registered_classes << klass if !@@registered_classes.include? klass
  self.for(config['local_model'])
  self.mailchimp_api_key = config['mailchimp_api_key']
  self.merge_map = config['merge_map']
  self.mailchimp_ses_key = config['mailchimp_ses_key']
  self.socket = Gibbon::API.new(self.mailchimp_api_key)
  case config['action_store']
  when :yml
    if !config['actions'].blank?
      config['actions'].each do |action|
        self.actions << Chimpactions::Action.new(action)
      end
    end
  when :active_record
    Chimpaction.all.each do |action|
      self.actions << Chimpactions::Action.new(action)
    end
  end
end

Instance Method Details

#initialize(*atts) ⇒ Object



80
81
82
83
# File 'lib/chimpactions.rb', line 80

def initialize(*atts)
  super(*atts)
  puts "**** CA INITIALIZED! ************"
end