class Gibbon::Export

Attributes

api_key[RW]
timeout[RW]
api_key[RW]
timeout[RW]

Public Class Methods

new(api_key: nil, timeout: nil) click to toggle source
# File lib/gibbon/export.rb, line 9
def initialize(api_key: nil, timeout: nil)
  @api_key = api_key || self.class.api_key || ENV['MAILCHIMP_API_KEY']
  @timeout = timeout || self.class.timeout || 600
end

Private Class Methods

method_missing(sym, *args, &block) click to toggle source
# File lib/gibbon/export.rb, line 88
def method_missing(sym, *args, &block)
  new(api_key: self.api_key, timeout: self.timeout).send(sym, *args, &block)
end

Public Instance Methods

campaign_subscriber_activity(params = {}, &block) click to toggle source
# File lib/gibbon/export.rb, line 22
def campaign_subscriber_activity(params = {}, &block)
  call("campaignSubscriberActivity", params, &block)
end
ecomm_orders(params = {}, &block) click to toggle source
# File lib/gibbon/export.rb, line 18
def ecomm_orders(params = {}, &block)
  call("ecommOrders", params, &block)
end
list(params = {}, &block) click to toggle source
# File lib/gibbon/export.rb, line 14
def list(params = {}, &block)
  call("list", params, &block)
end

Protected Instance Methods

call(method, params = {}, &block) click to toggle source
# File lib/gibbon/export.rb, line 32
def call(method, params = {}, &block)
  rows = []

  api_url = export_api_url + method + "/"
  params = params.merge({ apikey: @api_key })
  block = Proc.new { |row| rows << row } unless block_given?

  ensure_api_key(params)

  url = URI.parse(api_url)
  req = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' => 'application/json'})
  req.body = MultiJson.dump(params)
  Net::HTTP.start(url.host, url.port, read_timeout: @timeout, use_ssl: true, ssl_version: :TLSv1_2) do |http|
    # http://stackoverflow.com/questions/29598196/ruby-net-http-read-body-nethttpokread-body-called-twice-ioerror
    http.request req do |response|
      i = -1
      last = ''
      response.read_body do |chunk|
        next if chunk.nil? or chunk.strip.empty?
        infix = "\n" if last[-1, 1]==']'
        lines, last = try_parse_line("#{last}#{infix}#{chunk}")
        lines.each { |line| block.call(line, i += 1) }
      end
      block.call(parse_line(last), i += 1) unless last.nil? or last.empty?
    end
  end
  rows unless block_given?
end
export_api_url() click to toggle source
# File lib/gibbon/export.rb, line 28
def export_api_url
  "https://#{get_data_center_from_api_key(@api_key)}api.mailchimp.com/export/1.0/"
end
parse_line(line) click to toggle source
# File lib/gibbon/export.rb, line 71
def parse_line(line)
  parsed_response = MultiJson.load(line)
rescue MultiJson::ParseError
  return []
end
try_parse_line(res) click to toggle source
# File lib/gibbon/export.rb, line 61
def try_parse_line(res)
  lines = res.split("\n")
  last = lines.pop || ''
  lines.map! { |line| parse_line(line) }
  [lines.compact, last]
rescue MultiJson::ParseError
  [[], last]
end

Private Instance Methods

ensure_api_key(params) click to toggle source
# File lib/gibbon/export.rb, line 79
def ensure_api_key(params)
  unless params[:apikey] && (get_data_center_from_api_key(params[:apikey]) != "")
    raise Gibbon::GibbonError, "You must set an api_key prior to making a call"
  end
end