Sha256: 2b506966208c24d91720a420f1507eff74918446a280c53c0df1927bdc6d1177

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'active_support'
require 'httparty'
require 'json'
require 'cgi'

class Gibbon
  include HTTParty
  format :plain
  default_timeout 30

  attr_accessor :api_key, :timeout

  def initialize(api_key = nil, extra_params = {})
    @api_key = api_key || ENV['MC_API_KEY'] || self.class.api_key
    @default_params = {:apikey => @api_key}.merge(extra_params)
  end

  def api_key=(value)
    @api_key = value
    @default_params = @default_params.merge({:apikey => @api_key})
  end

  def base_api_url
    dc = @api_key.blank? ? '' : "#{@api_key.split("-").last}."
    "https://#{dc}api.mailchimp.com/1.3/?method="
  end

  def call(method, params = {})
    url = base_api_url + method
    params = @default_params.merge(params)
    response = self.class.post(url, :body => CGI::escape(params.to_json), :timeout => @timeout)

    begin
      response = ActiveSupport::JSON.decode(response.body)
    rescue
      response = response.body
    end
    response
  end

  def method_missing(method, *args)
    method = method.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } #Thanks for the gsub, Rails
    method = method[0].chr.downcase + method[1..-1].gsub(/aim$/i, 'AIM')
    args = {} unless args.length > 0
    args = args[0] if (args.class.to_s == "Array")
    call(method, args)
  end

  class << self
    attr_accessor :api_key

    def method_missing(sym, *args, &block)
      new(self.api_key).send(sym, *args, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gibbon-0.2.0 lib/gibbon.rb