Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Create Subscriber fixes #16

Merged
merged 5 commits into from

2 participants

@chrismcguire
Collaborator

-Create collection now raises a CreationError when create fails rather than returning false.
-Search Subscribers now acceptions hashes passed in for custom_fields value.

@pianoman19372 pianoman19372 merged commit df99dc7 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @chrismcguire
  2. @chrismcguire
  3. @chrismcguire

    Spec: pass hash keys as strings, not symbols

    chrismcguire authored
    +Addresses an issue that was causing the tests to fail with oauth gem
    0.4.7
  4. @chrismcguire
  5. @chrismcguire
This page is out of date. Refresh to see the latest.
View
2  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"
View
1  lib/aweber.rb
@@ -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
29 lib/aweber/collection.rb
@@ -51,6 +51,11 @@ 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)
@@ -58,7 +63,7 @@ def search(params={})
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
17 spec/collection_spec.rb
@@ -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
Something went wrong with that request. Please try again.