Sha256: 48cde5f1f6fb25e65c7b2b7a4dd2b6dfe0323a1898e22aaf96fd6be620789523

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module Gibbon
  class Export < APICategory

    def initialize(api_key = nil, default_params = {})
      @api_key = api_key
      @default_params = default_params

      set_instance_defaults
    end

    protected

    def export_api_url
      "http://#{get_data_center_from_api_key}api.mailchimp.com/export/1.0/"
    end

    def call(method, params = {})
      api_url = export_api_url + method + "/"
      params = @default_params.merge(params).merge({:apikey => @api_key})
      response = self.class.post(api_url, :body => MultiJson.dump(params), :timeout => @timeout)

      lines = response.body.lines
      if @throws_exceptions
        first_line = MultiJson.load(lines.first) if lines.first

        if should_raise_for_response?(first_line)
          error = MailChimpError.new("MailChimp Export API Error: #{first_line["error"]} (code #{first_line["code"]})")
          error.code = first_line["code"]
          raise error
        end
      end

      lines
    end

    def set_instance_defaults
      super
      @api_key = self.class.api_key if @api_key.nil?
      @timeout = self.class.timeout if @timeout.nil?
    end

    def method_missing(method, *args)
      # To support underscores, we camelize the method name

      # Thanks for the camelize gsub, Rails
      method = method.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }

      # We need to downcase the first letter of every API method
      # and MailChimp has a few of API methods that end in "AIM," which
      # must be upcased (See "Campaign Report Data Methods" in their API docs).
      method = method[0].chr.downcase + method[1..-1].gsub(/aim$/i, 'AIM')

      call(method, *args)
    end

    class << self
      attr_accessor :api_key, :timeout, :throws_exceptions

      def method_missing(sym, *args, &block)
        new(self.api_key, {:timeout => self.timeout, :throws_exceptions => self.throws_exceptions}).send(sym, *args, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gibbon-1.0.4 lib/gibbon/export.rb