Permalink
Browse files

moar specs

  • Loading branch information...
1 parent a3db68b commit 16ca88b5860ab4bc9e50d856645bb92dcaf15383 @bumi committed May 17, 2011
Showing with 73 additions and 6 deletions.
  1. +12 −6 lib/paypal_nvp.rb
  2. +61 −0 spec/paypal_nvp_spec.rb
View
18 lib/paypal_nvp.rb
@@ -2,7 +2,7 @@
require "net/https"
require "cgi"
-class PaypalNVP
+class PaypalNvp
attr_accessor :config, :logger
@@ -15,7 +15,7 @@ class PaypalNVP
def initialize(config={})
self.config = DEFAULT_OPTIONS.merge(config)
- self.logger = args.delete(:logger) || Logger.new(STDOUT)
+ self.logger = config.delete(:logger) || Logger.new(STDOUT)
end
def query_string_for(data)
@@ -32,6 +32,15 @@ def query_string_for(data)
query.join("&")
end
+ def hash_from_query_string(query_string)
+ hash = {}
+ query_string.split("&").each do |element|
+ a = element.split("=")
+ hash[a[0]] = CGI.unescape(a[1]) if a.size == 2
+ end
+ hash
+ end
+
def call_paypal(data)
uri = URI.parse(self.config[:url])
@@ -51,10 +60,7 @@ def call_paypal(data)
response_hash = { :response => response }
if response.kind_of? Net::HTTPSuccess
- response.body.split("&").each do |element|
- a = element.split("=")
- response_hash[a[0]] = CGI.unescape(a[1]) if a.size == 2
- end
+ response_hash.merge! self.hash_from_query_string(response.body)
end
response_hash
end
View
61 spec/paypal_nvp_spec.rb
@@ -1,4 +1,65 @@
require "spec_helper"
describe "PaypalNVP" do
+
+ describe "initialize" do
+
+ context "overwriting defaults" do
+ subject { PaypalNvp.new(:version => "1").config[:version] }
+ it { should == "1" }
+ end
+
+ context "with defaults" do
+ subject { PaypalNvp.new }
+
+ its(:config) do
+ should eql({
+ :version => "50.0",
+ :sandbox => false,
+ :url => "",
+ :params => {}
+ })
+ end
+
+ end
+ end
+
+ describe "Request to Paypal" do
+
+ describe "Query String" do
+
+ let(:paypal) { PaypalNvp.new }
+
+ it "should create a valid querystring from request params" do
+ paypal.query_string_for({ :foo => "bar", :bar => "foo" }).should == "FOO=bar&BAR=foo&USER=&PWD=&SIGNATURE="
+ end
+
+ %w{user pwd signature}.each do |param|
+ it "should include #{param} by default" do
+ paypal.query_string_for({}).should include("#{param.to_s.upcase}=#{URI.escape(paypal.config[param.to_sym].to_s)}")
+ end
+ end
+
+ end
+
+ end
+
+ describe "Paypal response" do
+
+ let(:paypal) { PaypalNvp.new }
+
+ it "should convert a query string like" do
+ paypal.hash_from_query_string("FOO=bar&BAR=foo&SIGNATURE=123456").should eql({
+ "FOO" => "bar", "BAR" => "foo", "SIGNATURE" => "123456"
+ })
+ end
+
+ it "should remove blank attributes" do
+ paypal.hash_from_query_string("FOO=bar&BAR=foo&USER=&PWD=&SIGNATURE=123456").should eql({
+ "FOO" => "bar", "BAR" => "foo", "SIGNATURE" => "123456"
+ })
+ end
+
+ end
+
end

0 comments on commit 16ca88b

Please sign in to comment.