Permalink
Browse files

Raise CreationError when create fails.

  • Loading branch information...
1 parent 43634b7 commit 95b56474317d6a36b98e7d58bed7dbd3eb3e9575 @chrismcguire chrismcguire committed Oct 5, 2012
Showing with 30 additions and 13 deletions.
  1. +1 −0 lib/aweber.rb
  2. +15 −9 lib/aweber/collection.rb
  3. +5 −4 spec/collection_spec.rb
  4. +9 −0 spec/fixtures/error.json
View
@@ -61,6 +61,7 @@ class NotFoundError < Exception; end
class UnknownRequestError < Exception; end
class RateLimitError < Exception; end
class ForbiddenRequestError < Exception; end
+ class CreationError < Exception; end
end
$LOAD_PATH << File.dirname(__FILE__) unless $LOAD_PATH.include?(File.dirname(__FILE__))
View
@@ -63,7 +63,7 @@ def search(params={})
self.class.new(client, @klass, response)
end
-
+
def create(attrs={})
params = attrs.merge("ws.op" => "create")
@@ -75,18 +75,24 @@ def create(attrs={})
response = client.post(path, params)
- return false unless response.is_a? Net::HTTPCreated
-
- resource = get(response["location"]).merge(:parent => self)
- resource = @klass.new(client, resource)
+ if response.is_a? Net::HTTPCreated
+ resource = get(response["location"]).merge(:parent => self)
+ resource = @klass.new(client, resource)
- self[resource.id] = resource
+ self[resource.id] = resource
+ else
+ if response
+ raise CreationError, JSON.parse(response.body)['error']['message'], caller
+ else
+ raise CreationError, "No response received", caller
+ end
+ end
end
def [](id)
@entries[id] ||= fetch_entry(id)
end
-
+
def []=(key, resource)
@entries[key] = resource
end
@@ -98,7 +104,7 @@ def each
def inspect
"#<AW::Collection(#{@klass.to_s}) size=\"#{size}\">"
end
-
+
def path
parent and parent.path.to_s + @klass.path.to_s or @klass.path.to_s
end
@@ -145,7 +151,7 @@ def method_missing(method, *args)
end
super
end
-
+
def client
@client
end
View
@@ -84,12 +84,13 @@
it "should set the operation to 'create'" do
@aweber.should_receive(:post).with(@lists.path, params)
- @lists.create(:name => "foo")
+ expect { @lists.create(:name => "foo")}.to raise_error
+
end
- it "should return false if the create failed" do
+ it "should return raise CreationError if the create failed" do
@oauth.should_receive(:post).and_return(nil)
- @lists.create(:name => "foo").should == false
+ expect {@lists.create(:name => "foo")}.to raise_error
end
it "should return the new object if the create succeeded" do
@@ -103,7 +104,7 @@
it "should set operation to create when creating subscriber" do
expected = { "ws.op" => "create", :name => "foo", :custom_fields => {"Signature" => '1234'} }
@aweber.should_receive(:post).with(@subscribers.path, expected)
- @subscribers.create(:name => "foo", :custom_fields => {"Signature" => '1234'})
+ expect {@subscribers.create(:name => "foo", :custom_fields => {"Signature" => '1234'})}.to raise_error
end
end
View
@@ -0,0 +1,9 @@
+{
+ "error":
+ {
+ "status": 400,
+ "documentation_url": "https://labs.aweber.com/docs/troubleshooting#badrequest",
+ "message": "email: Subscriber already subscribed.",
+ "type": "WebServiceError"
+ }
+}

0 comments on commit 95b5647

Please sign in to comment.