Sha256: 256dc5abb0c7ee0658ee5b9cb2227d8b8f6d1d8d71d493762c4df92d570ebd6f

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module ActiveESP
  module Providers
    class MailChimp < Base
      include HTTParty
      format :plain

      def endpoint
        "https://#{dc_from_api_key}api.mailchimp.com/1.3"
      end

      def subscribe(subscriber, list)
        call(:list_subscribe, { :id => list.id, :email_address => subscriber.email, :merge_vars => { :FNAME => subscriber.first_name, :LNAME => subscriber.last_name }})
      end

      def lists
        call(:lists)
      end

    private
      def call(method, params = {})
        api_url = endpoint + '/?method=' + method.to_s.camelize(:lower)
        params = api_params(params)
        response = self.class.post(api_url, :body => CGI::escape(params.to_json), :timeout => 30)

        # Some calls (e.g. listSubscribe) return json fragments
        # (e.g. true) so wrap in an array prior to parsing
        response = JSON.parse('['+response.body+']').first

        if @throws_exceptions && response.is_a?(Hash) && response["error"]
          raise "Error from MailChimp API: #{response["error"]} (code #{response["code"]})"
        end

        response
      end

      def api_params(additional_params = {})
        { :apikey => api_key }.merge(additional_params)
      end

      def dc_from_api_key
        (!@api_key.present? || @api_key !~ /-/) ? '' : "#{@api_key.split("-").last}."
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_esp-0.1.0.alpha1 lib/active_esp/providers/mail_chimp.rb