Skip to content

Instantly share code, notes, and snippets.

@daveworth
Created September 27, 2011 17:20
Show Gist options
  • Save daveworth/1245674 to your computer and use it in GitHub Desktop.
Save daveworth/1245674 to your computer and use it in GitHub Desktop.
Gibbon extension tests
require 'active_support'
require 'httparty'
require 'json'
require 'cgi'
class Gibbon
include HTTParty
format :plain
default_timeout 30
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
##...
protected
def dc_from_api_key
(@api_key.blank? or @api_key !~ /-/) ? '' : "#{@api_key.split("-").last}."
end
end
class GibbonExport < Gibbon
def export_api_url
"http://#{dc_from_api_key}api.mailchimp.com/export/1.0/"
end
def call(method, params = {})
url = export_api_url + method + "/"
params = @default_params.merge(params)
response = self.class.post(url, :body => params, :timeout => @timeout)
response.body.lines
end
end
require 'helper'
require 'cgi'
require 'ruby-debug'
class TestGibbon < Test::Unit::TestCase
@@Klass = Gibbon
## ... lots of working tests for Gibbon class
context "export API" do
setup do
@@Klass = GibbonExport
@key = "TESTKEY-us1"
@gibbon = @@Klass.new(@key)
@url = "http://us1.api.mailchimp.com/export/1.0/"
@body = {"apikey" => @key}
end
should "not escape string parameters" do
@param = "list id"
expect_post(@url, @body.merge("id" => @param))
@gibbon.say_hello(:id => @param)
end
end
private
def expect_post(expected_url, expected_body, expected_timeout=nil)
@@Klass.expects(:post).with do |url, opts|
url == expected_url &&
JSON.parse(URI::decode(opts[:body])) == expected_body &&
opts[:timeout] == expected_timeout
end.returns(Struct.new(:body).new("") )
end
end
@daveworth
Copy link
Author

   84      end
   85  
   86      should "not escape string parameters" do
   87        @param = "list id"
   88  debugger
=> 89        expect_post(@url, @body.merge("id" => @param))
   90        @gibbon.say_hello(:id => @param)
   91      end
   92    end
   93  
/Users/dworth/Documents/BatchBlue/gibbon/test/test_gibbon.rb:89
expect_post(@url, @body.merge("id" => @param))
(rdb:1) @@Klass
GibbonExport
(rdb:1) s
[92, 101] in /Users/dworth/Documents/BatchBlue/gibbon/test/test_gibbon.rb
   92    end
   93  
   94    private
   95  
   96    def expect_post(expected_url, expected_body, expected_timeout=nil)
=> 97      @@Klass.expects(:post).with do |url, opts|
   98        url == expected_url &&
   99        JSON.parse(URI::decode(opts[:body])) == expected_body &&
   100        opts[:timeout] == expected_timeout
   101      end.returns(Struct.new(:body).new("") )
/Users/dworth/Documents/BatchBlue/gibbon/test/test_gibbon.rb:97
@@Klass.expects(:post).with do |url, opts|
(rdb:1) @@Klass
GibbonExport
(rdb:1) 

@daveworth
Copy link
Author

[84, 93] in /Users/dworth/Documents/BatchBlue/gibbon/test/test_gibbon.rb
   84      end
   85  
   86      should "not escape string parameters" do
   87        @param = "list id"
   88  debugger
=> 89        expect_post(@url, @body.merge("id" => @param))
   90        @gibbon.say_hello(:id => @param)
   91      end
   92    end
   93  
/Users/dworth/Documents/BatchBlue/gibbon/test/test_gibbon.rb:89
expect_post(@url, @body.merge("id" => @param))
(rdb:1) b GibbonExport#call
Breakpoint 1 at GibbonExport::call
(rdb:1) c
Breakpoint 1 at GibbonExport:call
[64, 73] in /Users/dworth/Documents/BatchBlue/gibbon/lib/gibbon.rb
   64  
   65    def export_api_url
   66      "http://#{dc_from_api_key}api.mailchimp.com/export/1.0/"
   67    end
   68  
=> 69    def call(method, params = {})
   70      url = export_api_url + method + "/"
   71      params = @default_params.merge(params)
   72      response = self.class.post(url, :body => params, :timeout => @timeout)
   73  
/Users/dworth/Documents/BatchBlue/gibbon/lib/gibbon.rb:69
def call(method, params = {})
(rdb:1) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment