Permalink
Browse files

Merge pull request #16 from aweber/failure_message

Create Subscriber fixes
  • Loading branch information...
2 parents 073b766 + a9f0194 commit df99dc70d60a54f68a45dd2b60ad581a4fe0b3b4 @pianoman19372 pianoman19372 committed Oct 9, 2012
Showing with 31 additions and 18 deletions.
  1. +1 −1 aweber.gemspec
  2. +1 −0 lib/aweber.rb
  3. +20 −9 lib/aweber/collection.rb
  4. +9 −8 spec/collection_spec.rb
View
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "aweber"
- s.version = "1.4.3"
+ s.version = "1.5.0"
s.platform = Gem::Platform::RUBY
s.summary = "Ruby interface to AWeber's API"
s.description = "Ruby interface to AWeber's API"
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
@@ -51,14 +51,19 @@ def initialize(client, klass, data={})
end
def search(params={})
+ if params.has_key?('custom_fields')
+ if params['custom_fields'].is_a?(Hash)
+ params['custom_fields'] = params['custom_fields'].to_json
+ end
+ end
params = params.map { |k,v| "#{h(k)}=#{h(v)}" }.join("&")
uri = "#{path}?ws.op=find&#{params}"
response = client.get(uri).merge(:parent => parent)
response["total_size"] ||= response["entries"].size
self.class.new(client, @klass, response)
end
-
+
def create(attrs={})
params = attrs.merge("ws.op" => "create")
@@ -70,18 +75,24 @@ def create(attrs={})
response = client.post(path, params)
- return false unless response.is_a? Net::HTTPCreated
+ if response.is_a? Net::HTTPCreated
+ resource = get(response["location"]).merge(:parent => self)
+ resource = @klass.new(client, resource)
- 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
@@ -93,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
@@ -140,7 +151,7 @@ def method_missing(method, *args)
end
super
end
-
+
def client
@client
end
View
@@ -80,30 +80,31 @@
end
describe "when creating" do
- let(:params) {{ "ws.op" => "create", :name => "foo" }}
+ let(:params) {{ "ws.op" => "create", "name" => "foo" }}
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
- @lists.create(:name => "foo").should be_an AWeber::Resource
+ @lists.create("name" => "foo").should be_an AWeber::Resource
end
it "should have the collection as a parent" do
- @lists.create(:name => "foo").parent.should == @lists
+ @lists.create("name" => "foo").parent.should == @lists
end
it "should set operation to create when creating subscriber" do
- expected = { "ws.op" => "create", :name => "foo", :custom_fields => {"Signature" => '1234'} }
+ 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

0 comments on commit df99dc7

Please sign in to comment.