Skip to content

Commit f6c56a1

Browse files
author
Robert Gleeson
committed
expose the response as ResponseError#code, ResponseError#message
I've changed ResponseError a little bit to expose the response code & its message or content as ruby methods that return the type that makes most sense(so for code, that'd be Fixnum, and '__content__' as a string). I maintained backwards compatibility with the old object in the sense that ResponseError#inspect returns the same string as before. I've found this code to be useful when ResponseError is too generic and I need to take certain action based on the response code.
1 parent 955866a commit f6c56a1

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/pardot/error.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
module Pardot
2-
32
class Error < StandardError; end
43
class NetError < Error; end
5-
class ResponseError < Error; end
64
class ExpiredApiKeyError < Error; end
7-
5+
6+
class ResponseError < Error
7+
def initialize(res)
8+
@res = res
9+
end
10+
11+
def to_s
12+
@res["__content__"]
13+
end
14+
15+
def code
16+
@res["code"].to_i
17+
end
18+
19+
def inspect
20+
@res.inspect.to_s
21+
end
22+
end
823
end

spec/pardot/error_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
describe Pardot::ResponseError do
3+
before do
4+
@res = {
5+
"code" => "9",
6+
"__content__" => "A prospect with the specified email address already exists"
7+
}
8+
end
9+
10+
describe '#code' do
11+
subject do
12+
described_class.new(@res).code
13+
end
14+
specify do
15+
should == 9
16+
end
17+
end
18+
19+
describe '#to_s, #message' do
20+
subject do
21+
described_class.new(@res)
22+
end
23+
specify do
24+
subject.to_s.should == @res["__content__"]
25+
end
26+
specify do
27+
subject.message.should == @res["__content__"]
28+
end
29+
end
30+
31+
describe '#inspect' do
32+
subject do
33+
described_class.new(@res).inspect
34+
end
35+
specify do
36+
should == @res.to_s
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)