|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Multiwoven |
| 4 | + module Integrations |
| 5 | + module Destination |
| 6 | + module Mailchimp |
| 7 | + include Multiwoven::Integrations::Core |
| 8 | + |
| 9 | + API_VERSION = "3.0" |
| 10 | + |
| 11 | + class Client < DestinationConnector |
| 12 | + prepend Multiwoven::Integrations::Core::RateLimiter |
| 13 | + |
| 14 | + def check_connection(connection_config) |
| 15 | + connection_config = connection_config.with_indifferent_access |
| 16 | + initialize_client(connection_config) |
| 17 | + authenticate_client |
| 18 | + success_status |
| 19 | + rescue StandardError => e |
| 20 | + failure_status(e) |
| 21 | + end |
| 22 | + |
| 23 | + def discover(_connection_config = nil) |
| 24 | + catalog = build_catalog(load_catalog) |
| 25 | + catalog.to_multiwoven_message |
| 26 | + rescue StandardError => e |
| 27 | + handle_exception(e, { |
| 28 | + context: "MAILCHIMP:DISCOVER:EXCEPTION", |
| 29 | + type: "error" |
| 30 | + }) |
| 31 | + end |
| 32 | + |
| 33 | + def write(sync_config, records, _action = "create") |
| 34 | + @sync_config = sync_config |
| 35 | + initialize_client(sync_config.destination.connection_specification) |
| 36 | + process_records(records, sync_config.stream) |
| 37 | + rescue StandardError => e |
| 38 | + handle_exception(e, { |
| 39 | + context: "MAILCHIMP:WRITE:EXCEPTION", |
| 40 | + type: "error", |
| 41 | + sync_id: @sync_config.sync_id, |
| 42 | + sync_run_id: @sync_config.sync_run_id |
| 43 | + }) |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + def initialize_client(config) |
| 49 | + config = config.with_indifferent_access |
| 50 | + @client = MailchimpMarketing::Client.new |
| 51 | + @client.set_config({ |
| 52 | + api_key: config[:api_key], |
| 53 | + server: config[:api_key].split("-").last |
| 54 | + }) |
| 55 | + @list_id = config[:list_id] |
| 56 | + @email_template_id = config[:email_template_id] || "" |
| 57 | + end |
| 58 | + |
| 59 | + def process_records(records, stream) |
| 60 | + log_message_array = [] |
| 61 | + write_success = 0 |
| 62 | + write_failure = 0 |
| 63 | + properties = stream.json_schema[:properties] |
| 64 | + |
| 65 | + records.each do |record_object| |
| 66 | + record = extract_data(record_object, properties) |
| 67 | + args = [stream.name, "Id", record] |
| 68 | + begin |
| 69 | + response = send_to_mailchimp(record, stream.name) |
| 70 | + write_success += 1 |
| 71 | + log_message_array << log_request_response("info", args, response) |
| 72 | + rescue StandardError => e |
| 73 | + handle_exception(e, { |
| 74 | + context: "MAILCHIMP:WRITE:EXCEPTION", |
| 75 | + type: "error", |
| 76 | + sync_id: @sync_config.sync_id, |
| 77 | + sync_run_id: @sync_config.sync_run_id |
| 78 | + }) |
| 79 | + write_failure += 1 |
| 80 | + log_message_array << log_request_response("error", args, e.message) |
| 81 | + end |
| 82 | + end |
| 83 | + tracking_message(write_success, write_failure, log_message_array) |
| 84 | + end |
| 85 | + |
| 86 | + def send_to_mailchimp(record, stream_name) |
| 87 | + case stream_name |
| 88 | + when "Audience" |
| 89 | + @client.lists.set_list_member(@list_id, Digest::MD5.hexdigest(record[:email].downcase), { |
| 90 | + email_address: record[:email], |
| 91 | + status_if_new: "subscribed", |
| 92 | + merge_fields: { |
| 93 | + FNAME: record[:first_name], |
| 94 | + LNAME: record[:last_name] |
| 95 | + } |
| 96 | + }) |
| 97 | + when "Tags" |
| 98 | + @client.lists.update_list_member_tags(@list_id, Digest::MD5.hexdigest(record[:email].downcase), { |
| 99 | + tags: record[:tags].map { |tag| { name: tag, status: "active" } } |
| 100 | + }) |
| 101 | + when "Campaigns" |
| 102 | + campaign = @client.campaigns.create({ |
| 103 | + type: "regular", |
| 104 | + recipients: { list_id: @list_id }, |
| 105 | + settings: { |
| 106 | + subject_line: record[:subject], |
| 107 | + from_name: record[:from_name], |
| 108 | + reply_to: record[:reply_to] |
| 109 | + } |
| 110 | + }) |
| 111 | + if @email_template_id |
| 112 | + @client.campaigns.set_content(campaign["id"], { |
| 113 | + template: { id: @email_template_id } |
| 114 | + }) |
| 115 | + else |
| 116 | + @client.campaigns.set_content(campaign["id"], { |
| 117 | + plain_text: record[:content] |
| 118 | + }) |
| 119 | + end |
| 120 | + @client.campaigns.send(campaign["id"]) |
| 121 | + else |
| 122 | + raise "Unsupported stream type: #{stream_name}" |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + def authenticate_client |
| 127 | + @client.lists.get_all_lists |
| 128 | + end |
| 129 | + |
| 130 | + def load_catalog |
| 131 | + read_json(CATALOG_SPEC_PATH) |
| 132 | + end |
| 133 | + |
| 134 | + def log_debug(message) |
| 135 | + Multiwoven::Integrations::Service.logger.debug(message) |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments