Class: Effective::MailchimpApi

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/mailchimp_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ MailchimpApi

Returns a new instance of MailchimpApi.



13
14
15
16
17
18
19
20
21
22
# File 'app/models/effective/mailchimp_api.rb', line 13

def initialize(api_key:)
  @api_key = api_key
  @server = api_key.to_s.split('-').last

  raise('expected an api key') unless @api_key.present?
  raise('expected an api key') unless @server.present?

  @client = ::MailchimpMarketing::Client.new()
  @client.set_config(api_key: @api_key, server: @server)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'app/models/effective/mailchimp_api.rb', line 9

def api_key
  @api_key
end

#clientObject

Returns the value of attribute client.



11
12
13
# File 'app/models/effective/mailchimp_api.rb', line 11

def client
  @client
end

#serverObject

Returns the value of attribute server.



10
11
12
# File 'app/models/effective/mailchimp_api.rb', line 10

def server
  @server
end

Instance Method Details

#add_merge_field(id, name:, type: :text) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/effective/mailchimp_api.rb', line 108

def add_merge_field(id, name:, type: :text)
  raise("invalid mailchimp merge key: #{name}. Must be 10 or fewer characters") if name.to_s.length > 10

  return if sandbox_mode?
  Rails.logger.info "[effective_mailchimp] Add List Merge Field #{name}" if debug?

  payload = { name: name.to_s.titleize, tag: name.to_s, type: type }

  begin
    client.lists.add_list_merge_field(id.try(:mailchimp_id) || id, payload)
  rescue MailchimpMarketing::ApiError => e
    EffectiveLogger.error(e.message, details: name.to_s) if defined?(EffectiveLogger)
    false
  end
end

#admin_urlObject



32
33
34
# File 'app/models/effective/mailchimp_api.rb', line 32

def admin_url
  "https://#{server}.admin.mailchimp.com"
end

#audience_urlObject



36
37
38
# File 'app/models/effective/mailchimp_api.rb', line 36

def audience_url
  "https://#{server}.admin.mailchimp.com/audience/"
end

#campaigns_urlObject



48
49
50
# File 'app/models/effective/mailchimp_api.rb', line 48

def campaigns_url
  "https://#{server}.admin.mailchimp.com/campaigns/"
end

#categories(list_id) ⇒ Object



75
76
77
78
79
80
# File 'app/models/effective/mailchimp_api.rb', line 75

def categories(list_id)
  Rails.logger.info "[effective_mailchimp] Index Interest Categories" if debug?

  response = client.lists.get_list_interest_categories(list_id.try(:mailchimp_id) || list_id)
  Array(response['categories']) - [nil, '', {}]
end

#contacts_urlObject



44
45
46
# File 'app/models/effective/mailchimp_api.rb', line 44

def contacts_url
  "https://#{server}.admin.mailchimp.com/audience/contacts"
end

#debug?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/effective/mailchimp_api.rb', line 24

def debug?
  Rails.env.development?
end

#groups_urlObject



40
41
42
# File 'app/models/effective/mailchimp_api.rb', line 40

def groups_url
  "https://#{server}.admin.mailchimp.com/audience/groups/"
end

#interests(list_id, category_id) ⇒ Object



82
83
84
85
86
87
# File 'app/models/effective/mailchimp_api.rb', line 82

def interests(list_id, category_id)
  Rails.logger.info "[effective_mailchimp] Index Interest Category Interests" if debug?

  response = client.lists.list_interest_category_interests(list_id, category_id)
  Array(response['interests']) - [nil, '', {}]
end

#list(id) ⇒ Object



69
70
71
72
73
# File 'app/models/effective/mailchimp_api.rb', line 69

def list(id)
  Rails.logger.info "[effective_mailchimp] Get List" if debug?

  client.lists.get_list(id.try(:mailchimp_id) || id)
end

#list_member(id, email) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/effective/mailchimp_api.rb', line 89

def list_member(id, email)
  raise('expected an email') unless email.to_s.include?('@')

  Rails.logger.info "[effective_mailchimp] Get List Member" if debug?

  begin
    client.lists.get_list_member(id.try(:mailchimp_id) || id, email)
  rescue MailchimpMarketing::ApiError => e
    {}
  end
end

#list_member_add(member) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/effective/mailchimp_api.rb', line 124

def list_member_add(member)
  raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)

  return if sandbox_mode?
  Rails.logger.info "[effective_mailchimp] Add List Member" if debug?

  # See if they exist somehow
  existing = list_member(member.mailchimp_list, member.user.email)

  if existing.present?
    member.assign_attributes(mailchimp_id: existing['id'])
    return list_member_update(member)
  end

  # Actually add
  payload = list_member_payload(member)
  client.lists.add_list_member(member.mailchimp_list.mailchimp_id, payload)
end

#list_member_payload(member) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/effective/mailchimp_api.rb', line 153

def list_member_payload(member)
  raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)

  merge_fields = member.user.mailchimp_merge_fields
  raise('expected user mailchimp_merge_fields to be a Hash') unless merge_fields.kind_of?(Hash)

  payload = {
    email_address: member.user.email,
    status: (member.subscribed ? 'subscribed' : 'unsubscribed'),
    merge_fields: merge_fields.delete_if { |k, v| v.blank? },
    interests: member.interests_hash.presence
  }.compact
end

#list_member_update(member) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'app/models/effective/mailchimp_api.rb', line 143

def list_member_update(member)
  raise('expected an Effective::MailchimpListMember') unless member.kind_of?(Effective::MailchimpListMember)

  return if sandbox_mode?
  Rails.logger.info "[effective_mailchimp] Update List Member" if debug?

  payload = list_member_payload(member)
  client.lists.update_list_member(member.mailchimp_list.mailchimp_id, member.email, payload)
end

#list_merge_fields(id) ⇒ Object



101
102
103
104
105
106
# File 'app/models/effective/mailchimp_api.rb', line 101

def list_merge_fields(id)
  Rails.logger.info "[effective_mailchimp] Get List Merge Fields" if debug?

  response = client.lists.get_list_merge_fields(id.try(:mailchimp_id) || id, count: 100)
  Array(response['merge_fields']) - [nil, '', ' ', {}]
end

#listsObject

Returns an Array of Lists, which are each Hash Like this [{ …}, { … }]



62
63
64
65
66
67
# File 'app/models/effective/mailchimp_api.rb', line 62

def lists
  Rails.logger.info "[effective_mailchimp] Index Lists" if debug?

  response = client.lists.get_all_lists(count: 250)
  Array(response['lists']) - [nil, '', {}]
end

#pingObject



56
57
58
# File 'app/models/effective/mailchimp_api.rb', line 56

def ping
  client.ping.get()
end

#public_urlObject



52
53
54
# File 'app/models/effective/mailchimp_api.rb', line 52

def public_url
  "https://mailchimp.com"
end

#sandbox_mode?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/effective/mailchimp_api.rb', line 28

def sandbox_mode?
  EffectiveMailchimp.sandbox_mode?
end