Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use GoCardless.client by default for Resources

  • Loading branch information...
commit acb716a961afe74fef31e17e15d8c161b7a539d8 1 parent d29e04d
Harry Marr authored November 17, 2011
1  Gemfile
@@ -7,5 +7,6 @@ group :development do
7 7
   if RUBY_PLATFORM.downcase.include?("darwin")
8 8
     gem "guard-rspec", "~> 0.5.4"
9 9
     gem "rb-fsevent", "~> 0.4.3.1"
  10
+    gem "growl_notify", "~> 0.0.3"
10 11
   end
11 12
 end
2  lib/gocardless/bill.rb
@@ -17,7 +17,7 @@ class Bill < Resource
17 17
 
18 18
     def source
19 19
       klass = GoCardless.const_get(Utils.camelize(source_type.to_s))
20  
-      klass.find(@client, @source_id)
  20
+      klass.find_with_client(client, @source_id)
21 21
     end
22 22
 
23 23
     def source=(obj)
16  lib/gocardless/client.rb
@@ -119,42 +119,42 @@ def api_put(path, data = {})
119 119
     # @return [Merchant] the merchant associated with the client's access token
120 120
     def merchant
121 121
       raise ClientError, 'Access token missing' unless @access_token
122  
-      Merchant.new(self, api_get("/merchants/#{merchant_id}"))
  122
+      Merchant.new_with_client(self, api_get("/merchants/#{merchant_id}"))
123 123
     end
124 124
 
125 125
     # @method subscripton(id)
126 126
     # @param [String] id of the subscription
127 127
     # @return [Subscription] the subscription matching the id requested
128 128
     def subscription(id)
129  
-      Subscription.find(self, id)
  129
+      Subscription.find_with_client(self, id)
130 130
     end
131 131
 
132 132
     # @method pre_authorization(id)
133 133
     # @param [String] id of the pre_authorization
134 134
     # @return [PreAuthorization] the pre_authorization matching the id requested
135 135
     def pre_authorization(id)
136  
-      PreAuthorization.find(self, id)
  136
+      PreAuthorization.find_with_client(self, id)
137 137
     end
138 138
 
139 139
     # @method user(id)
140 140
     # @param [String] id of the user
141 141
     # @return [User] the User matching the id requested
142 142
     def user(id)
143  
-      User.find(self, id)
  143
+      User.find_with_client(self, id)
144 144
     end
145 145
 
146 146
     # @method bill(id)
147 147
     # @param [String] id of the bill
148 148
     # @return [Bill] the Bill matching the id requested
149 149
     def bill(id)
150  
-      Bill.find(self, id)
  150
+      Bill.find_with_client(self, id)
151 151
     end
152 152
 
153 153
     # @method payment(id)
154 154
     # @param [String] id of the payment
155 155
     # @return [Payment] the payment matching the id requested
156 156
     def payment(id)
157  
-      Payment.find(self, id)
  157
+      Payment.find_with_client(self, id)
158 158
     end
159 159
 
160 160
     # Create a new bill under a given pre-authorization
@@ -163,7 +163,7 @@ def payment(id)
163 163
     # @param [Hash] attrs must include +:pre_authorization_id+ and +:amount+
164 164
     # @return [Bill] the created bill object
165 165
     def create_bill(attrs)
166  
-      Bill.new(self, attrs).save
  166
+      Bill.new_with_client(self, attrs).save
167 167
     end
168 168
 
169 169
     # Generate the URL for creating a new subscription. The parameters passed
@@ -235,7 +235,7 @@ def confirm_resource(params)
235 235
 
236 236
         # Initialize the correct class according to the resource's type
237 237
         klass = GoCardless.const_get(Utils.camelize(params[:resource_type]))
238  
-        klass.find(self, params[:resource_id])
  238
+        klass.find_with_client(self, params[:resource_id])
239 239
       else
240 240
         raise SignatureError, 'An invalid signature was detected'
241 241
       end
10  lib/gocardless/merchant.rb
@@ -12,35 +12,35 @@ class Merchant < Resource
12 12
     def subscriptions(params = {})
13 13
       path = "/merchants/#{self.id}/subscriptions"
14 14
       @client.api_get(path, params).map do |attrs|
15  
-        GoCardless::Subscription.new(@client, attrs)
  15
+        GoCardless::Subscription.new_with_client(@client, attrs)
16 16
       end
17 17
     end
18 18
 
19 19
     def pre_authorizations(params = {})
20 20
       path = "/merchants/#{self.id}/pre_authorizations"
21 21
       @client.api_get(path, params).map do |attrs|
22  
-        GoCardless::PreAuthorization.new(@client, attrs)
  22
+        GoCardless::PreAuthorization.new_with_client(@client, attrs)
23 23
       end
24 24
     end
25 25
 
26 26
     def users(params = {})
27 27
       path = "/merchants/#{self.id}/users"
28 28
       @client.api_get(path, params).map do |attrs|
29  
-        GoCardless::User.new(@client, attrs)
  29
+        GoCardless::User.new_with_client(@client, attrs)
30 30
       end
31 31
     end
32 32
 
33 33
     def bills(params = {})
34 34
       path = "/merchants/#{self.id}/bills"
35 35
       @client.api_get(path, params).map do |attrs|
36  
-        GoCardless::Bill.new(@client, attrs)
  36
+        GoCardless::Bill.new_with_client(@client, attrs)
37 37
       end
38 38
     end
39 39
 
40 40
     def payments(params = {})
41 41
       path = "/merchants/#{self.id}/payments"
42 42
       @client.api_get(path, params).map do |attrs|
43  
-        GoCardless::Payment.new(@client, attrs)
  43
+        GoCardless::Payment.new_with_client(@client, attrs)
44 44
       end
45 45
     end
46 46
   end
33  lib/gocardless/resource.rb
@@ -2,9 +2,7 @@
2 2
 
3 3
 module GoCardless
4 4
   class Resource
5  
-    def initialize(client, hash = {})
6  
-      @client = client
7  
-
  5
+    def initialize(hash = {})
8 6
       # Handle sub resources
9 7
       sub_resource_uris = hash.delete('sub_resource_uris')
10 8
       unless sub_resource_uris.nil?
@@ -33,7 +31,7 @@ def initialize(client, hash = {})
33 31
             params = args.first || {}
34 32
             query = default_query.nil? ? nil : default_query.merge(params)
35 33
             client.api_get(path, query).map do |attrs|
36  
-              klass.new(client, attrs)
  34
+              klass.new(attrs).tap { |m| m.client = client }
37 35
             end
38 36
           end
39 37
         end
@@ -43,13 +41,25 @@ def initialize(client, hash = {})
43 41
       hash.each { |key,val| send("#{key}=", val) if respond_to?("#{key}=") }
44 42
     end
45 43
 
  44
+    attr_writer :client
  45
+
46 46
     class << self
47 47
       attr_accessor :endpoint
48 48
 
49  
-      def find(client, id)
  49
+      def new_with_client(client, attrs = {})
  50
+        self.new(attrs).tap { |obj| obj.client = client }
  51
+      end
  52
+
  53
+      def find_with_client(client_obj, id)
50 54
         path = endpoint.gsub(':id', id.to_s)
51  
-        data = client.api_get(path)
52  
-        self.new(client, data)
  55
+        data = client_obj.api_get(path)
  56
+        obj = self.new(data)
  57
+        obj.client = client_obj
  58
+        obj
  59
+      end
  60
+
  61
+      def find(id)
  62
+        self.find_with_client(client, id)
53 63
       end
54 64
 
55 65
       def date_writer(*args)
@@ -78,7 +88,7 @@ def reference_reader(*args)
78 88
           define_method(name.to_sym) do
79 89
             obj_id = instance_variable_get("@#{attr}")
80 90
             klass = GoCardless.const_get(Utils.camelize(name))
81  
-            klass.find(@client, obj_id)
  91
+            klass.find_with_client(client, obj_id)
82 92
           end
83 93
         end
84 94
       end
@@ -133,6 +143,7 @@ def updatable?
133 143
 
134 144
     def to_hash
135 145
       attrs = instance_variables.map { |v| v.to_s.sub(/^@/, '') }
  146
+      attrs.delete 'client'
136 147
       Hash[attrs.select { |v| respond_to? v }.map { |v| [v.to_sym, send(v)] }]
137 148
     end
138 149
 
@@ -158,6 +169,10 @@ def save
158 169
 
159 170
   protected
160 171
 
  172
+    def client
  173
+      @client || GoCardless.client
  174
+    end
  175
+
161 176
     def save_data(data)
162 177
       method = if self.persisted?
163 178
         raise "#{self.class} cannot be updated" unless self.class.updatable?
@@ -167,7 +182,7 @@ def save_data(data)
167 182
         'post'
168 183
       end
169 184
       path = self.class.endpoint.gsub(':id', id.to_s)
170  
-      response = @client.send("api_#{method}", path, data)
  185
+      response = client.send("api_#{method}", path, data)
171 186
       response.each { |key,val| send("#{key}=", val) if respond_to?("#{key}=") } if response.is_a? Hash
172 187
     end
173 188
   end
11  spec/bill_spec.rb
@@ -4,12 +4,13 @@
4 4
   before :each do
5 5
     @app_id = 'abc'
6 6
     @app_secret = 'xyz'
7  
-    @client = GoCardless::Client.new(:app_id => @app_id, :app_secret => @app_secret)
  7
+    GoCardless.account_details = {:app_id => @app_id, :app_secret => @app_secret,
  8
+                                  :token  => 'xxx manage_merchant:1'}
  9
+    @client = GoCardless.client
8 10
   end
9 11
 
10 12
   it "source getter works" do
11  
-    b = GoCardless::Bill.new(@client, :source_type => :subscription,
12  
-                                       :source_id => 123)
  13
+    b = GoCardless::Bill.new(:source_type => :subscription, :source_id => 123)
13 14
     @client.access_token = 'TOKEN manage_merchant:123'
14 15
     stub_get(@client, :id => 123)
15 16
     source = b.source
@@ -18,8 +19,8 @@
18 19
   end
19 20
 
20 21
   it "source setter works" do
21  
-    b = GoCardless::Bill.new(@client)
22  
-    b.source = GoCardless::Subscription.new(@client, :id => 123)
  22
+    b = GoCardless::Bill.new
  23
+    b.source = GoCardless::Subscription.new(:id => 123)
23 24
     b.source_id.should == 123
24 25
     b.source_type.should.to_s == 'subscription'
25 26
   end
10  spec/client_spec.rb
@@ -146,7 +146,7 @@
146 146
       merchant_url = '/api/v1/merchants/123'
147 147
       token.expects(:get).with { |p,o| p == merchant_url }.returns response
148 148
 
149  
-      GoCardless::Merchant.stubs(:new)
  149
+      GoCardless::Merchant.stubs(:new_with_client)
150 150
 
151 151
       @client.merchant
152 152
     end
@@ -254,8 +254,8 @@
254 254
 
255 255
     it "returns the correct object when the signature is valid" do
256 256
       @client.stubs(:request).returns(stub(:parsed => {}))
257  
-      subscription = GoCardless::Subscription.new @client
258  
-      GoCardless::Subscription.expects(:find).returns subscription
  257
+      subscription = GoCardless::Subscription.new_with_client @client
  258
+      GoCardless::Subscription.expects(:find_with_client).returns subscription
259 259
 
260 260
       # confirm_resource should use the Subcription class because
261 261
       # the :response_type is set to subscription
@@ -264,7 +264,7 @@
264 264
     end
265 265
 
266 266
     it "includes valid http basic credentials" do
267  
-      GoCardless::Subscription.stubs(:find)
  267
+      GoCardless::Subscription.stubs(:find_with_client)
268 268
       auth = 'Basic YWJjOnh5eg=='
269 269
       @client.expects(:request).once.with do |type, path, opts|
270 270
         opts.should include :headers
@@ -276,7 +276,7 @@
276 276
 
277 277
     it "works with string params" do
278 278
       @client.stubs(:request)
279  
-      GoCardless::Subscription.stubs(:find)
  279
+      GoCardless::Subscription.stubs(:find_with_client)
280 280
       params = Hash[@params.dup.map { |k,v| [k.to_s, v] }]
281 281
       params.keys.each { |p| p.should be_a String }
282 282
       # No ArgumentErrors should be raised
2  spec/merchant_spec.rb
@@ -13,7 +13,7 @@
13 13
 
14 14
   index_methods.each do |method|
15 15
     it "##{method} works correctly" do
16  
-      merchant = GoCardless::Merchant.new(@client)
  16
+      merchant = GoCardless::Merchant.new_with_client(@client)
17 17
 
18 18
       data = [{:id => 1}, {:id => 2}]
19 19
       stub_get(@client, data)
58  spec/resource_spec.rb
@@ -6,7 +6,7 @@
6 6
       attr_accessor :id, :name, :uri
7 7
     end
8 8
     props = {:id => 1, :name => 'test', :uri => 'http://test'}
9  
-    resource = test_resource.new(mock, props)
  9
+    resource = test_resource.new(props)
10 10
     props.each { |k,v| resource.send(k).should == v }
11 11
   end
12 12
 
@@ -25,7 +25,7 @@
25 25
         date_writer :created_at
26 26
       end
27 27
 
28  
-      resource = test_resource.new(nil)
  28
+      resource = test_resource.new
29 29
       time = '2011-12-12T12:00:00Z'
30 30
       resource.created_at = time
31 31
       date_time = resource.instance_variable_get(:@created_at)
@@ -51,21 +51,21 @@
51 51
         date_accessor :created_at
52 52
       end
53 53
 
54  
-      resource = test_resource.new(nil)
  54
+      resource = test_resource.new
55 55
       date = DateTime.now
56 56
       resource.instance_variable_set(:@created_at, date)
57 57
       resource.created_at.should == date
58 58
     end
59 59
   end
60 60
 
61  
-  describe "#find" do
  61
+  describe "#find_with_client" do
62 62
     it "instantiates the correct object" do
63 63
       test_resource = Class.new(GoCardless::Resource) do
64 64
         self.endpoint = '/test/:id'
65 65
       end
66 66
       mock_client = mock
67 67
       mock_client.expects(:api_get).returns({:id => 123})
68  
-      resource = test_resource.find(mock_client, 123)
  68
+      resource = test_resource.find_with_client(mock_client, 123)
69 69
       resource.should be_a test_resource
70 70
       resource.id.should == 123
71 71
     end
@@ -88,8 +88,8 @@
88 88
         reference_writer :user_id
89 89
       end
90 90
 
91  
-      resource = test_resource.new(nil)
92  
-      resource.user = GoCardless::User.new(nil, :id => 123)
  91
+      resource = test_resource.new
  92
+      resource.user = GoCardless::User.new(:id => 123)
93 93
       resource.instance_variable_get(:@user_id).should == 123
94 94
     end
95 95
 
@@ -106,7 +106,7 @@
106 106
         reference_writer :user_id
107 107
       end
108 108
       expect do
109  
-        test_resource.new(nil).user = 'asdf'
  109
+        test_resource.new.user = 'asdf'
110 110
       end.to raise_exception ArgumentError
111 111
     end
112 112
   end
@@ -135,7 +135,7 @@
135 135
         reference_reader :user_id
136 136
       end
137 137
 
138  
-      resource = test_resource.new(@client)
  138
+      resource = test_resource.new_with_client(@client)
139 139
       resource.instance_variable_set(:@user_id, 123)
140 140
       @client.access_token = 'TOKEN manage_merchant:123'
141 141
       stub_get(@client, {:id => 123})
@@ -171,8 +171,8 @@
171 171
   end
172 172
 
173 173
   it "#persisted? works" do
174  
-    GoCardless::Resource.new(nil).persisted?.should be_false
175  
-    GoCardless::Resource.new(nil, :id => 1).persisted?.should be_true
  174
+    GoCardless::Resource.new.persisted?.should be_false
  175
+    GoCardless::Resource.new(:id => 1).persisted?.should be_true
176 176
   end
177 177
 
178 178
   describe "#save" do
@@ -193,28 +193,28 @@
193 193
       it "sends the correct data parameters" do
194 194
         client = mock
195 195
         data = {:x => 1, :y => 2}
196  
-        resource = @test_resource.new(client, data)
  196
+        resource = @test_resource.new_with_client(client, data)
197 197
         client.expects(:api_post).with(anything, data)
198 198
         resource.save
199 199
       end
200 200
 
201 201
       it "sends the correct path" do
202 202
         client = mock
203  
-        resource = @test_resource.new(client)
  203
+        resource = @test_resource.new_with_client(client)
204 204
         client.expects(:api_post).with('/test', anything)
205 205
         resource.save
206 206
       end
207 207
 
208 208
       it "POSTs when not persisted" do
209 209
         client = mock
210  
-        resource = @test_resource.new(client)
  210
+        resource = @test_resource.new_with_client(client)
211 211
         client.expects(:api_post)
212 212
         resource.save
213 213
       end
214 214
 
215 215
       it "PUTs when already persisted" do
216 216
         client = mock
217  
-        resource = @test_resource.new(client, :id => 1)
  217
+        resource = @test_resource.new_with_client(client, :id => 1)
218 218
         client.expects(:api_put)
219 219
         resource.save
220 220
       end
@@ -227,7 +227,7 @@
227 227
       end
228 228
 
229 229
       client = mock('client') { stubs :api_post }
230  
-      test_resource.new(client).save
  230
+      test_resource.new_with_client(client).save
231 231
     end
232 232
 
233 233
     it "succeeds when persisted and update allowed" do
@@ -237,7 +237,7 @@
237 237
       end
238 238
 
239 239
       client = mock('client') { stubs :api_put }
240  
-      test_resource.new(client, :id => 1).save
  240
+      test_resource.new_with_client(client, :id => 1).save
241 241
     end
242 242
 
243 243
     it "fails when not persisted and create not allowed" do
@@ -245,7 +245,7 @@
245 245
         updatable
246 246
       end
247 247
 
248  
-      expect { test_resource.new(mock).save }.to raise_error
  248
+      expect { test_resource.new.save }.to raise_error
249 249
     end
250 250
 
251 251
     it "fails when persisted and update not allowed" do
@@ -253,7 +253,7 @@
253 253
         creatable
254 254
       end
255 255
 
256  
-      expect { test_resource.new(mock, :id => 1).save }.to raise_error
  256
+      expect { test_resource.new(:id => 1).save }.to raise_error
257 257
     end
258 258
   end
259 259
 
@@ -263,7 +263,7 @@
263 263
     end
264 264
 
265 265
     attrs = {:id => 1, :uri => 'http:', :x => 'y'}
266  
-    resource = test_resource.new('CLIENT', attrs)
  266
+    resource = test_resource.new_with_client(mock, attrs)
267 267
     resource.to_hash.should == attrs
268 268
   end
269 269
 
@@ -274,7 +274,7 @@
274 274
       reference_accessor :person_id
275 275
     end
276 276
 
277  
-    bill = test_resource.new(nil, {
  277
+    bill = test_resource.new({
278 278
       :amount => '10',
279 279
       :when => DateTime.now,
280 280
       :person_id => 15
@@ -324,20 +324,20 @@ class UpdatableResource < GoCardless::Resource
324 324
     end
325 325
 
326 326
     it "are defined on instances" do
327  
-      r = @test_resource.new(stub, @attrs)
  327
+      r = @test_resource.new(@attrs)
328 328
       r.should respond_to :bills
329 329
     end
330 330
 
331 331
     it "aren't defined for other instances of the class" do
332  
-      @test_resource.new(stub, @attrs)
333  
-      resource = @test_resource.new stub
  332
+      @test_resource.new(@attrs)
  333
+      resource = @test_resource.new
334 334
       resource.should_not respond_to :bills
335 335
     end
336 336
 
337 337
     it "use the correct uri path" do
338 338
       client = mock()
339 339
       client.expects(:api_get).with('/api/bills/', anything).returns([])
340  
-      r = @test_resource.new(client, @attrs)
  340
+      r = @test_resource.new_with_client(client, @attrs)
341 341
       r.bills
342 342
     end
343 343
 
@@ -345,14 +345,14 @@ class UpdatableResource < GoCardless::Resource
345 345
       client = mock()
346 346
       client.expects(:api_get).with('/bills/', anything).returns([])
347 347
       uris = {'bills' => 'https://test.com/api/v123/bills/'}
348  
-      r = @test_resource.new(client, 'sub_resource_uris' => uris)
  348
+      r = @test_resource.new_with_client(client, 'sub_resource_uris' => uris)
349 349
       r.bills
350 350
     end
351 351
 
352 352
     it "use the correct query string params" do
353 353
       client = mock()
354 354
       client.expects(:api_get).with(anything, 'merchant_id' => '1').returns([])
355  
-      r = @test_resource.new(client, @attrs)
  355
+      r = @test_resource.new_with_client(client, @attrs)
356 356
       r.bills
357 357
     end
358 358
 
@@ -360,13 +360,13 @@ class UpdatableResource < GoCardless::Resource
360 360
       client = mock()
361 361
       params = { 'merchant_id' => '1', :amount => '10.00' }
362 362
       client.expects(:api_get).with(anything, params).returns([])
363  
-      r = @test_resource.new(client, @attrs)
  363
+      r = @test_resource.new_with_client(client, @attrs)
364 364
       r.bills(:amount => '10.00')
365 365
     end
366 366
 
367 367
     it "return instances of the correct resource class" do
368 368
       client = stub(:api_get => [{:id => 1}])
369  
-      r = @test_resource.new(client, @attrs)
  369
+      r = @test_resource.new_with_client(client, @attrs)
370 370
       ret = r.bills
371 371
       ret.should be_a Array
372 372
       ret.first.should be_a GoCardless::Bill

0 notes on commit acb716a

Please sign in to comment.
Something went wrong with that request. Please try again.