Class: EasyMailchimp::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_mailchimp/base.rb

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



3
4
5
6
7
8
9
10
# File 'lib/easy_mailchimp/base.rb', line 3

def initialize
  @config = ::EasyMailchimp.config
  @gibbon = ::Gibbon::Request.new({
    api_key: @config.api_key,
    api_endpoint: endpoint,
    timeout: 60
  })
end

Instance Method Details

#create_member(list_id, info = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easy_mailchimp/base.rb', line 12

def create_member(list_id, info = {})
  email = info[:email] || ''
  first_name = info[:first_name] || ''
  last_name = info[:last_name] || ''

  begin
    @gibbon.lists(list_id).members.create({
      body:
      {
        email_address: email,
        status: 'subscribed',
        merge_fields: {FNAME: first_name, LNAME: last_name}
      }
    })
  rescue ::Gibbon::MailChimpError => ex
    Rails.logger.tagged('Mailchimp::Base#create_member') { Rails.logger.debug "ERROR: #{ex.message} - #{ex.raw_body}" }
    raise
  end
end

#get_list_name(list_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/easy_mailchimp/base.rb', line 32

def get_list_name(list_id)
  list_map = {}

  lists = @gibbon.lists.retrieve['lists']
  lists.map { |hsh| list_map[hsh["id"]] = hsh["name"] }

  list_map.select { |k,v| k == list_id }.values.first
rescue => ex
  Rails.logger.tagged('Mailchimp::Base#get_list_name') { Rails.logger.debug "ERROR: #{ex.message}" }
  raise
end