Created
September 27, 2011 17:20
-
-
Save daveworth/1245674 to your computer and use it in GitHub Desktop.
Gibbon extension tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
daveworth
commented
Sep 27, 2011
[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