Skip to content
This repository has been archived by the owner on Aug 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from aweber/failure_message
Browse files Browse the repository at this point in the history
Create Subscriber fixes
  • Loading branch information
pianoman19372 committed Oct 9, 2012
2 parents 073b766 + a9f0194 commit df99dc7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion aweber.gemspec
@@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/aweber.rb
Expand Up @@ -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__))
Expand Down
29 changes: 20 additions & 9 deletions lib/aweber/collection.rb
Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -140,7 +151,7 @@ def method_missing(method, *args)
end
super
end

def client
@client
end
Expand Down
17 changes: 9 additions & 8 deletions spec/collection_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit df99dc7

Please sign in to comment.