Open donrestarone opened 2 years ago
@Pralish when you get a chance, can you create a unit test for this plugin?
you can copy paste the class here: https://github.com/restarone/violet_rails/blob/master/test/fixtures/external_api_clients.yml
and use the web_mock
gem to mock responses from Mailchimp
credits to @Pralish
class SyncToMailchimp
def initialize(parameters)
@external_api_client = parameters[:external_api_client]
@api_key = @external_api_client.metadata["API_KEY"]
@unsynced_api_resources = @external_api_client.api_namespace.api_resources.where("properties @> ?", {synced_to_mailchimp: false}.to_json)
@mailchimp_uri = "https://#{@external_api_client.metadata['SERVER_PREFIX']}.api.mailchimp.com/3.0/lists/#{@external_api_client.metadata['LIST_ID']}/members?skip_merge_validation=true"
@custom_merge_fields_map = @external_api_client.metadata['CUSTOM_MERGE_FIELDS_MAP'] || {}
@attr_to_exclude = (@external_api_client.metadata['ATTR_TO_EXCLUDE'] || []) + @custom_merge_fields_map.keys + ['synced_to_mailchimp']
@logger_namespace = ApiNamespace.find_by(slug: @external_api_client.metadata["LOGGER_NAMESPACE"]) if @external_api_client.metadata["LOGGER_NAMESPACE"]
end
def start
@unsynced_api_resources.each do |api_resource|
begin
merge_fields = api_resource.properties.except(*@attr_to_exclude).transform_keys(&:upcase).transform_values(&:to_s)
@custom_merge_fields_map.each do |key, value|
merge_fields[value.upcase] = api_resource.properties[key].to_s if value
end
response = HTTParty.post(@mailchimp_uri,
body: {
email_address: api_resource.properties["email"],
status: "subscribed",
merge_fields: merge_fields,
tags: @external_api_client.metadata['TAGS'] || []
}.to_json,
headers: {
'Content-Type': 'application/json',
'Authorization': "Basic #{@api_key}"
}
)
if response.success?
api_resource.properties["synced_to_mailchimp"] = true
api_resource.save
end
@logger_namespace.api_resources.create!(
properties: {
api_resource: api_resource.id,
status: response.success? ? "success" : "error",
response: JSON.parse(response.body),
timestamp: Time.zone.now
}
) if @logger_namespace
rescue StandardError => e
@logger_namespace.api_resources.create!(
properties: {
api_resource: api_resource.id,
status: "error",
response: { detail: e.message},
timestamp: Time.zone.now
}
) if @logger_namespace
end
end
end
end
# at the end of the file we have to implicitly return the class
SyncToMailchimp
email :
synced_to_mailchimp :
API_KEY:
LIST_ID:
SERVER_PREFIX:
CUSTOM_MERGE_FIELDS_MAP
ATTR_TO_EXCLUDE:
TAGS:
LOGGER_NAMESPACE:
Create fields on mailchimp with same name as properties on api namespace. You can create fields with different names from api namespace properties, but you will need to map it correctly using CUSTOM_MERGE_FIELDS_MAP metadata.
credits to @Pralish
Mailchimp Sync V1 (soft deprecated) please use V2 below
The following metadata need to be included for the plugin to work:
API_KEY: mailchimp api key - https://mailchimp.com/help/about-api-keys/
LIST_ID: Audiance ID, - https://mailchimp.com/help/find-audience-id/
SERVER_PREFIX: mailchimp url prefix, eg: us9, us10 - https://mailchimp.com/developer/marketing/guides/quick-start/#make-your-first-api-call
ATTR_TO_EXCLUDE: attributes that you don't want to sync to mailchimp
TAGS: array of tags to assign to contact
LOGGER_NAMESPACE: the slug of the API Namespace you want to use for logging
Note:
merge fields
The field label can be anything but the tags should be exactly same as the keys in api namespace.
Note: The tags are capitalized by mailchimp