Permalink
Browse files

Use GoCardless.client by default for Resources

  • Loading branch information...
1 parent d29e04d commit acb716a961afe74fef31e17e15d8c161b7a539d8 @hmarr hmarr committed Nov 17, 2011
View
1 Gemfile
@@ -7,5 +7,6 @@ group :development do
if RUBY_PLATFORM.downcase.include?("darwin")
gem "guard-rspec", "~> 0.5.4"
gem "rb-fsevent", "~> 0.4.3.1"
+ gem "growl_notify", "~> 0.0.3"
end
end
View
2 lib/gocardless/bill.rb
@@ -17,7 +17,7 @@ class Bill < Resource
def source
klass = GoCardless.const_get(Utils.camelize(source_type.to_s))
- klass.find(@client, @source_id)
+ klass.find_with_client(client, @source_id)
end
def source=(obj)
View
16 lib/gocardless/client.rb
@@ -119,42 +119,42 @@ def api_put(path, data = {})
# @return [Merchant] the merchant associated with the client's access token
def merchant
raise ClientError, 'Access token missing' unless @access_token
- Merchant.new(self, api_get("/merchants/#{merchant_id}"))
+ Merchant.new_with_client(self, api_get("/merchants/#{merchant_id}"))
end
# @method subscripton(id)
# @param [String] id of the subscription
# @return [Subscription] the subscription matching the id requested
def subscription(id)
- Subscription.find(self, id)
+ Subscription.find_with_client(self, id)
end
# @method pre_authorization(id)
# @param [String] id of the pre_authorization
# @return [PreAuthorization] the pre_authorization matching the id requested
def pre_authorization(id)
- PreAuthorization.find(self, id)
+ PreAuthorization.find_with_client(self, id)
end
# @method user(id)
# @param [String] id of the user
# @return [User] the User matching the id requested
def user(id)
- User.find(self, id)
+ User.find_with_client(self, id)
end
# @method bill(id)
# @param [String] id of the bill
# @return [Bill] the Bill matching the id requested
def bill(id)
- Bill.find(self, id)
+ Bill.find_with_client(self, id)
end
# @method payment(id)
# @param [String] id of the payment
# @return [Payment] the payment matching the id requested
def payment(id)
- Payment.find(self, id)
+ Payment.find_with_client(self, id)
end
# Create a new bill under a given pre-authorization
@@ -163,7 +163,7 @@ def payment(id)
# @param [Hash] attrs must include +:pre_authorization_id+ and +:amount+
# @return [Bill] the created bill object
def create_bill(attrs)
- Bill.new(self, attrs).save
+ Bill.new_with_client(self, attrs).save
end
# Generate the URL for creating a new subscription. The parameters passed
@@ -235,7 +235,7 @@ def confirm_resource(params)
# Initialize the correct class according to the resource's type
klass = GoCardless.const_get(Utils.camelize(params[:resource_type]))
- klass.find(self, params[:resource_id])
+ klass.find_with_client(self, params[:resource_id])
else
raise SignatureError, 'An invalid signature was detected'
end
View
10 lib/gocardless/merchant.rb
@@ -12,35 +12,35 @@ class Merchant < Resource
def subscriptions(params = {})
path = "/merchants/#{self.id}/subscriptions"
@client.api_get(path, params).map do |attrs|
- GoCardless::Subscription.new(@client, attrs)
+ GoCardless::Subscription.new_with_client(@client, attrs)
end
end
def pre_authorizations(params = {})
path = "/merchants/#{self.id}/pre_authorizations"
@client.api_get(path, params).map do |attrs|
- GoCardless::PreAuthorization.new(@client, attrs)
+ GoCardless::PreAuthorization.new_with_client(@client, attrs)
end
end
def users(params = {})
path = "/merchants/#{self.id}/users"
@client.api_get(path, params).map do |attrs|
- GoCardless::User.new(@client, attrs)
+ GoCardless::User.new_with_client(@client, attrs)
end
end
def bills(params = {})
path = "/merchants/#{self.id}/bills"
@client.api_get(path, params).map do |attrs|
- GoCardless::Bill.new(@client, attrs)
+ GoCardless::Bill.new_with_client(@client, attrs)
end
end
def payments(params = {})
path = "/merchants/#{self.id}/payments"
@client.api_get(path, params).map do |attrs|
- GoCardless::Payment.new(@client, attrs)
+ GoCardless::Payment.new_with_client(@client, attrs)
end
end
end
View
33 lib/gocardless/resource.rb
@@ -2,9 +2,7 @@
module GoCardless
class Resource
- def initialize(client, hash = {})
- @client = client
-
+ def initialize(hash = {})
# Handle sub resources
sub_resource_uris = hash.delete('sub_resource_uris')
unless sub_resource_uris.nil?
@@ -33,7 +31,7 @@ def initialize(client, hash = {})
params = args.first || {}
query = default_query.nil? ? nil : default_query.merge(params)
client.api_get(path, query).map do |attrs|
- klass.new(client, attrs)
+ klass.new(attrs).tap { |m| m.client = client }
end
end
end
@@ -43,13 +41,25 @@ def initialize(client, hash = {})
hash.each { |key,val| send("#{key}=", val) if respond_to?("#{key}=") }
end
+ attr_writer :client
+
class << self
attr_accessor :endpoint
- def find(client, id)
+ def new_with_client(client, attrs = {})
+ self.new(attrs).tap { |obj| obj.client = client }
+ end
+
+ def find_with_client(client_obj, id)
path = endpoint.gsub(':id', id.to_s)
- data = client.api_get(path)
- self.new(client, data)
+ data = client_obj.api_get(path)
+ obj = self.new(data)
+ obj.client = client_obj
+ obj
+ end
+
+ def find(id)
+ self.find_with_client(client, id)
end
def date_writer(*args)
@@ -78,7 +88,7 @@ def reference_reader(*args)
define_method(name.to_sym) do
obj_id = instance_variable_get("@#{attr}")
klass = GoCardless.const_get(Utils.camelize(name))
- klass.find(@client, obj_id)
+ klass.find_with_client(client, obj_id)
end
end
end
@@ -133,6 +143,7 @@ def updatable?
def to_hash
attrs = instance_variables.map { |v| v.to_s.sub(/^@/, '') }
+ attrs.delete 'client'
Hash[attrs.select { |v| respond_to? v }.map { |v| [v.to_sym, send(v)] }]
end
@@ -158,6 +169,10 @@ def save
protected
+ def client
+ @client || GoCardless.client
+ end
+
def save_data(data)
method = if self.persisted?
raise "#{self.class} cannot be updated" unless self.class.updatable?
@@ -167,7 +182,7 @@ def save_data(data)
'post'
end
path = self.class.endpoint.gsub(':id', id.to_s)
- response = @client.send("api_#{method}", path, data)
+ response = client.send("api_#{method}", path, data)
response.each { |key,val| send("#{key}=", val) if respond_to?("#{key}=") } if response.is_a? Hash
end
end
View
11 spec/bill_spec.rb
@@ -4,12 +4,13 @@
before :each do
@app_id = 'abc'
@app_secret = 'xyz'
- @client = GoCardless::Client.new(:app_id => @app_id, :app_secret => @app_secret)
+ GoCardless.account_details = {:app_id => @app_id, :app_secret => @app_secret,
+ :token => 'xxx manage_merchant:1'}
+ @client = GoCardless.client
end
it "source getter works" do
- b = GoCardless::Bill.new(@client, :source_type => :subscription,
- :source_id => 123)
+ b = GoCardless::Bill.new(:source_type => :subscription, :source_id => 123)
@client.access_token = 'TOKEN manage_merchant:123'
stub_get(@client, :id => 123)
source = b.source
@@ -18,8 +19,8 @@
end
it "source setter works" do
- b = GoCardless::Bill.new(@client)
- b.source = GoCardless::Subscription.new(@client, :id => 123)
+ b = GoCardless::Bill.new
+ b.source = GoCardless::Subscription.new(:id => 123)
b.source_id.should == 123
b.source_type.should.to_s == 'subscription'
end
View
10 spec/client_spec.rb
@@ -146,7 +146,7 @@
merchant_url = '/api/v1/merchants/123'
token.expects(:get).with { |p,o| p == merchant_url }.returns response
- GoCardless::Merchant.stubs(:new)
+ GoCardless::Merchant.stubs(:new_with_client)
@client.merchant
end
@@ -254,8 +254,8 @@
it "returns the correct object when the signature is valid" do
@client.stubs(:request).returns(stub(:parsed => {}))
- subscription = GoCardless::Subscription.new @client
- GoCardless::Subscription.expects(:find).returns subscription
+ subscription = GoCardless::Subscription.new_with_client @client
+ GoCardless::Subscription.expects(:find_with_client).returns subscription
# confirm_resource should use the Subcription class because
# the :response_type is set to subscription
@@ -264,7 +264,7 @@
end
it "includes valid http basic credentials" do
- GoCardless::Subscription.stubs(:find)
+ GoCardless::Subscription.stubs(:find_with_client)
auth = 'Basic YWJjOnh5eg=='
@client.expects(:request).once.with do |type, path, opts|
opts.should include :headers
@@ -276,7 +276,7 @@
it "works with string params" do
@client.stubs(:request)
- GoCardless::Subscription.stubs(:find)
+ GoCardless::Subscription.stubs(:find_with_client)
params = Hash[@params.dup.map { |k,v| [k.to_s, v] }]
params.keys.each { |p| p.should be_a String }
# No ArgumentErrors should be raised
View
2 spec/merchant_spec.rb
@@ -13,7 +13,7 @@
index_methods.each do |method|
it "##{method} works correctly" do
- merchant = GoCardless::Merchant.new(@client)
+ merchant = GoCardless::Merchant.new_with_client(@client)
data = [{:id => 1}, {:id => 2}]
stub_get(@client, data)
View
58 spec/resource_spec.rb
@@ -6,7 +6,7 @@
attr_accessor :id, :name, :uri
end
props = {:id => 1, :name => 'test', :uri => 'http://test'}
- resource = test_resource.new(mock, props)
+ resource = test_resource.new(props)
props.each { |k,v| resource.send(k).should == v }
end
@@ -25,7 +25,7 @@
date_writer :created_at
end
- resource = test_resource.new(nil)
+ resource = test_resource.new
time = '2011-12-12T12:00:00Z'
resource.created_at = time
date_time = resource.instance_variable_get(:@created_at)
@@ -51,21 +51,21 @@
date_accessor :created_at
end
- resource = test_resource.new(nil)
+ resource = test_resource.new
date = DateTime.now
resource.instance_variable_set(:@created_at, date)
resource.created_at.should == date
end
end
- describe "#find" do
+ describe "#find_with_client" do
it "instantiates the correct object" do
test_resource = Class.new(GoCardless::Resource) do
self.endpoint = '/test/:id'
end
mock_client = mock
mock_client.expects(:api_get).returns({:id => 123})
- resource = test_resource.find(mock_client, 123)
+ resource = test_resource.find_with_client(mock_client, 123)
resource.should be_a test_resource
resource.id.should == 123
end
@@ -88,8 +88,8 @@
reference_writer :user_id
end
- resource = test_resource.new(nil)
- resource.user = GoCardless::User.new(nil, :id => 123)
+ resource = test_resource.new
+ resource.user = GoCardless::User.new(:id => 123)
resource.instance_variable_get(:@user_id).should == 123
end
@@ -106,7 +106,7 @@
reference_writer :user_id
end
expect do
- test_resource.new(nil).user = 'asdf'
+ test_resource.new.user = 'asdf'
end.to raise_exception ArgumentError
end
end
@@ -135,7 +135,7 @@
reference_reader :user_id
end
- resource = test_resource.new(@client)
+ resource = test_resource.new_with_client(@client)
resource.instance_variable_set(:@user_id, 123)
@client.access_token = 'TOKEN manage_merchant:123'
stub_get(@client, {:id => 123})
@@ -171,8 +171,8 @@
end
it "#persisted? works" do
- GoCardless::Resource.new(nil).persisted?.should be_false
- GoCardless::Resource.new(nil, :id => 1).persisted?.should be_true
+ GoCardless::Resource.new.persisted?.should be_false
+ GoCardless::Resource.new(:id => 1).persisted?.should be_true
end
describe "#save" do
@@ -193,28 +193,28 @@
it "sends the correct data parameters" do
client = mock
data = {:x => 1, :y => 2}
- resource = @test_resource.new(client, data)
+ resource = @test_resource.new_with_client(client, data)
client.expects(:api_post).with(anything, data)
resource.save
end
it "sends the correct path" do
client = mock
- resource = @test_resource.new(client)
+ resource = @test_resource.new_with_client(client)
client.expects(:api_post).with('/test', anything)
resource.save
end
it "POSTs when not persisted" do
client = mock
- resource = @test_resource.new(client)
+ resource = @test_resource.new_with_client(client)
client.expects(:api_post)
resource.save
end
it "PUTs when already persisted" do
client = mock
- resource = @test_resource.new(client, :id => 1)
+ resource = @test_resource.new_with_client(client, :id => 1)
client.expects(:api_put)
resource.save
end
@@ -227,7 +227,7 @@
end
client = mock('client') { stubs :api_post }
- test_resource.new(client).save
+ test_resource.new_with_client(client).save
end
it "succeeds when persisted and update allowed" do
@@ -237,23 +237,23 @@
end
client = mock('client') { stubs :api_put }
- test_resource.new(client, :id => 1).save
+ test_resource.new_with_client(client, :id => 1).save
end
it "fails when not persisted and create not allowed" do
test_resource = Class.new(GoCardless::Resource) do
updatable
end
- expect { test_resource.new(mock).save }.to raise_error
+ expect { test_resource.new.save }.to raise_error
end
it "fails when persisted and update not allowed" do
test_resource = Class.new(GoCardless::Resource) do
creatable
end
- expect { test_resource.new(mock, :id => 1).save }.to raise_error
+ expect { test_resource.new(:id => 1).save }.to raise_error
end
end
@@ -263,7 +263,7 @@
end
attrs = {:id => 1, :uri => 'http:', :x => 'y'}
- resource = test_resource.new('CLIENT', attrs)
+ resource = test_resource.new_with_client(mock, attrs)
resource.to_hash.should == attrs
end
@@ -274,7 +274,7 @@
reference_accessor :person_id
end
- bill = test_resource.new(nil, {
+ bill = test_resource.new({
:amount => '10',
:when => DateTime.now,
:person_id => 15
@@ -324,49 +324,49 @@ class UpdatableResource < GoCardless::Resource
end
it "are defined on instances" do
- r = @test_resource.new(stub, @attrs)
+ r = @test_resource.new(@attrs)
r.should respond_to :bills
end
it "aren't defined for other instances of the class" do
- @test_resource.new(stub, @attrs)
- resource = @test_resource.new stub
+ @test_resource.new(@attrs)
+ resource = @test_resource.new
resource.should_not respond_to :bills
end
it "use the correct uri path" do
client = mock()
client.expects(:api_get).with('/api/bills/', anything).returns([])
- r = @test_resource.new(client, @attrs)
+ r = @test_resource.new_with_client(client, @attrs)
r.bills
end
it "strips the api prefix from the path" do
client = mock()
client.expects(:api_get).with('/bills/', anything).returns([])
uris = {'bills' => 'https://test.com/api/v123/bills/'}
- r = @test_resource.new(client, 'sub_resource_uris' => uris)
+ r = @test_resource.new_with_client(client, 'sub_resource_uris' => uris)
r.bills
end
it "use the correct query string params" do
client = mock()
client.expects(:api_get).with(anything, 'merchant_id' => '1').returns([])
- r = @test_resource.new(client, @attrs)
+ r = @test_resource.new_with_client(client, @attrs)
r.bills
end
it "adds provided params to query string params" do
client = mock()
params = { 'merchant_id' => '1', :amount => '10.00' }
client.expects(:api_get).with(anything, params).returns([])
- r = @test_resource.new(client, @attrs)
+ r = @test_resource.new_with_client(client, @attrs)
r.bills(:amount => '10.00')
end
it "return instances of the correct resource class" do
client = stub(:api_get => [{:id => 1}])
- r = @test_resource.new(client, @attrs)
+ r = @test_resource.new_with_client(client, @attrs)
ret = r.bills
ret.should be_a Array
ret.first.should be_a GoCardless::Bill

0 comments on commit acb716a

Please sign in to comment.