gocardless 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +10 -0
 - data/README.md +1 -4
 - data/lib/gocardless.rb +0 -1
 - data/lib/gocardless/bill.rb +26 -14
 - data/lib/gocardless/client.rb +16 -12
 - data/lib/gocardless/merchant.rb +1 -0
 - data/lib/gocardless/page.rb +43 -0
 - data/lib/gocardless/paginator.rb +86 -0
 - data/lib/gocardless/pre_authorization.rb +2 -2
 - data/lib/gocardless/resource.rb +18 -12
 - data/lib/gocardless/subscription.rb +1 -5
 - data/lib/gocardless/user.rb +6 -1
 - data/lib/gocardless/version.rb +1 -1
 - data/spec/bill_spec.rb +23 -73
 - data/spec/client_spec.rb +10 -10
 - data/spec/page_spec.rb +93 -0
 - data/spec/paginator_spec.rb +134 -0
 - data/spec/pre_authorization_spec.rb +11 -48
 - data/spec/resource_spec.rb +81 -105
 - data/spec/spec_helper.rb +14 -1
 - data/spec/subscription_spec.rb +11 -48
 - data/spec/user_spec.rb +5 -7
 - metadata +8 -3
 - data/lib/gocardless/payment.rb +0 -9
 
    
        data/spec/client_spec.rb
    CHANGED
    
    | 
         @@ -101,7 +101,7 @@ describe GoCardless::Client do 
     | 
|
| 
       101 
101 
     | 
    
         
             
                describe "with valid params" do
         
     | 
| 
       102 
102 
     | 
    
         
             
                  let(:oauth_client) { @client.instance_variable_get(:@oauth_client) }
         
     | 
| 
       103 
103 
     | 
    
         
             
                  let(:fake_token) do
         
     | 
| 
       104 
     | 
    
         
            -
                     
     | 
| 
      
 104 
     | 
    
         
            +
                    double(:params => {'scope' => 'manage_merchant:x'}, :token  => 'abc')
         
     | 
| 
       105 
105 
     | 
    
         
             
                  end
         
     | 
| 
       106 
106 
     | 
    
         | 
| 
       107 
107 
     | 
    
         
             
                  before { oauth_client.auth_code.stub(:get_token).and_return(fake_token) }
         
     | 
| 
         @@ -182,7 +182,7 @@ describe GoCardless::Client do 
     | 
|
| 
       182 
182 
     | 
    
         
             
                it "uses the correct path prefix" do
         
     | 
| 
       183 
183 
     | 
    
         
             
                  @client.access_token = 'TOKEN123'
         
     | 
| 
       184 
184 
     | 
    
         
             
                  token = @client.instance_variable_get(:@access_token)
         
     | 
| 
       185 
     | 
    
         
            -
                  r =  
     | 
| 
      
 185 
     | 
    
         
            +
                  r = double
         
     | 
| 
       186 
186 
     | 
    
         
             
                  r.stub(:parsed)
         
     | 
| 
       187 
187 
     | 
    
         
             
                  token.should_receive(:get).with { |p,o| p =~ %r|/api/v1/test| }.and_return(r)
         
     | 
| 
       188 
188 
     | 
    
         
             
                  @client.api_get('/test')
         
     | 
| 
         @@ -197,7 +197,7 @@ describe GoCardless::Client do 
     | 
|
| 
       197 
197 
     | 
    
         
             
                it "encodes data to json" do
         
     | 
| 
       198 
198 
     | 
    
         
             
                  @client.access_token = 'TOKEN123'
         
     | 
| 
       199 
199 
     | 
    
         
             
                  token = @client.instance_variable_get(:@access_token)
         
     | 
| 
       200 
     | 
    
         
            -
                  r =  
     | 
| 
      
 200 
     | 
    
         
            +
                  r = double
         
     | 
| 
       201 
201 
     | 
    
         
             
                  r.stub(:parsed)
         
     | 
| 
       202 
202 
     | 
    
         
             
                  token.should_receive(:post).with { |p,opts| opts[:body] == '{"a":1}' }.and_return(r)
         
     | 
| 
       203 
203 
     | 
    
         
             
                  @client.api_post('/test', {:a => 1})
         
     | 
| 
         @@ -212,7 +212,7 @@ describe GoCardless::Client do 
     | 
|
| 
       212 
212 
     | 
    
         
             
                it "encodes data to json" do
         
     | 
| 
       213 
213 
     | 
    
         
             
                  @client.access_token = 'TOKEN123'
         
     | 
| 
       214 
214 
     | 
    
         
             
                  token = @client.instance_variable_get(:@access_token)
         
     | 
| 
       215 
     | 
    
         
            -
                  r =  
     | 
| 
      
 215 
     | 
    
         
            +
                  r = double
         
     | 
| 
       216 
216 
     | 
    
         
             
                  r.stub(:parsed)
         
     | 
| 
       217 
217 
     | 
    
         
             
                  token.should_receive(:delete).with { |p,opts| opts[:body] == '{"a":1}' }.and_return(r)
         
     | 
| 
       218 
218 
     | 
    
         
             
                  @client.api_delete('/test', {:a => 1})
         
     | 
| 
         @@ -227,7 +227,7 @@ describe GoCardless::Client do 
     | 
|
| 
       227 
227 
     | 
    
         
             
                it "looks up the correct merchant" do
         
     | 
| 
       228 
228 
     | 
    
         
             
                  @client.access_token = 'TOKEN'
         
     | 
| 
       229 
229 
     | 
    
         
             
                  @client.merchant_id = '123'
         
     | 
| 
       230 
     | 
    
         
            -
                  response =  
     | 
| 
      
 230 
     | 
    
         
            +
                  response = double
         
     | 
| 
       231 
231 
     | 
    
         
             
                  response.should_receive(:parsed)
         
     | 
| 
       232 
232 
     | 
    
         | 
| 
       233 
233 
     | 
    
         
             
                  token = @client.instance_variable_get(:@access_token)
         
     | 
| 
         @@ -242,7 +242,7 @@ describe GoCardless::Client do 
     | 
|
| 
       242 
242 
     | 
    
         
             
                it "creates a Merchant object" do
         
     | 
| 
       243 
243 
     | 
    
         
             
                  @client.access_token = 'TOKEN'
         
     | 
| 
       244 
244 
     | 
    
         
             
                  @client.merchant_id = '123'
         
     | 
| 
       245 
     | 
    
         
            -
                  response =  
     | 
| 
      
 245 
     | 
    
         
            +
                  response = double
         
     | 
| 
       246 
246 
     | 
    
         
             
                  response.should_receive(:parsed).and_return({:name => 'test', :id => 123})
         
     | 
| 
       247 
247 
     | 
    
         | 
| 
       248 
248 
     | 
    
         
             
                  token = @client.instance_variable_get(:@access_token)
         
     | 
| 
         @@ -255,7 +255,7 @@ describe GoCardless::Client do 
     | 
|
| 
       255 
255 
     | 
    
         
             
                end
         
     | 
| 
       256 
256 
     | 
    
         
             
              end
         
     | 
| 
       257 
257 
     | 
    
         | 
| 
       258 
     | 
    
         
            -
              %w 
     | 
| 
      
 258 
     | 
    
         
            +
              %w(subscription pre_authorization user bill).each do |resource|
         
     | 
| 
       259 
259 
     | 
    
         
             
                describe "##{resource}" do
         
     | 
| 
       260 
260 
     | 
    
         
             
                  it "and_return the correct #{GoCardless::Utils.camelize(resource)} object" do
         
     | 
| 
       261 
261 
     | 
    
         
             
                    @client.access_token = 'TOKEN'
         
     | 
| 
         @@ -311,12 +311,12 @@ describe GoCardless::Client do 
     | 
|
| 
       311 
311 
     | 
    
         | 
| 
       312 
312 
     | 
    
         
             
                it "confirms the resource when the signature is valid" do
         
     | 
| 
       313 
313 
     | 
    
         
             
                  # Once for confirm, once to fetch result
         
     | 
| 
       314 
     | 
    
         
            -
                  @client.should_receive(:request).twice.and_return( 
     | 
| 
      
 314 
     | 
    
         
            +
                  @client.should_receive(:request).twice.and_return(double(:parsed => {}))
         
     | 
| 
       315 
315 
     | 
    
         
             
                  @client.confirm_resource(@client.send(:sign_params, @params))
         
     | 
| 
       316 
316 
     | 
    
         
             
                end
         
     | 
| 
       317 
317 
     | 
    
         | 
| 
       318 
318 
     | 
    
         
             
                it "and_return the correct object when the signature is valid" do
         
     | 
| 
       319 
     | 
    
         
            -
                  @client.stub(:request).and_return( 
     | 
| 
      
 319 
     | 
    
         
            +
                  @client.stub(:request).and_return(double(:parsed => {}))
         
     | 
| 
       320 
320 
     | 
    
         
             
                  subscription = GoCardless::Subscription.new_with_client @client
         
     | 
| 
       321 
321 
     | 
    
         
             
                  GoCardless::Subscription.should_receive(:find_with_client).and_return subscription
         
     | 
| 
       322 
322 
     | 
    
         | 
| 
         @@ -373,7 +373,7 @@ describe GoCardless::Client do 
     | 
|
| 
       373 
373 
     | 
    
         
             
                  params_indifferent_access = HashWithIndifferentAccess.new(params)
         
     | 
| 
       374 
374 
     | 
    
         
             
                  expect do
         
     | 
| 
       375 
375 
     | 
    
         
             
                    @client.response_params_valid? params_indifferent_access
         
     | 
| 
       376 
     | 
    
         
            -
                  end.to_not raise_exception 
     | 
| 
      
 376 
     | 
    
         
            +
                  end.to_not raise_exception
         
     | 
| 
       377 
377 
     | 
    
         
             
                end
         
     | 
| 
       378 
378 
     | 
    
         | 
| 
       379 
379 
     | 
    
         
             
                it "rejects other params not required for the signature" do
         
     | 
    
        data/spec/page_spec.rb
    ADDED
    
    | 
         @@ -0,0 +1,93 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'gocardless/page'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            describe GoCardless::Page do
         
     | 
| 
      
 5 
     | 
    
         
            +
              #let(:resource_class) { Class.new(GoCardless::Resource) { } }
         
     | 
| 
      
 6 
     | 
    
         
            +
              let(:resource_class) { GoCardless::Resource }
         
     | 
| 
      
 7 
     | 
    
         
            +
              let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:data) {[ { :id => 'a' }, { :id => 'b' } ]}
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              let(:page) { GoCardless::Page.new(resource_class, data, links) }
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              describe "#has_next?" do
         
     | 
| 
      
 13 
     | 
    
         
            +
                subject { page.has_next? }
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                context "when there is next page available" do
         
     | 
| 
      
 16 
     | 
    
         
            +
                  let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 17 
     | 
    
         
            +
                  it { should be_true }
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                context "when there is no next page" do
         
     | 
| 
      
 21 
     | 
    
         
            +
                  let(:links) {{ "previous" => 1, "first" => 1 }}
         
     | 
| 
      
 22 
     | 
    
         
            +
                  it { should be_false }
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
              describe "#next_page" do
         
     | 
| 
      
 27 
     | 
    
         
            +
                subject { page.next_page }
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                context "when there is next page available" do
         
     | 
| 
      
 30 
     | 
    
         
            +
                  let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 31 
     | 
    
         
            +
                  it { should == 2 }
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                context "when there is no next page" do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  let(:links) {{ "previous" => 1, "first" => 1 }}
         
     | 
| 
      
 36 
     | 
    
         
            +
                  it { should be_nil }
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
              describe "#previous_page" do
         
     | 
| 
      
 41 
     | 
    
         
            +
                subject { page.previous_page }
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                context "when there is previous page available" do
         
     | 
| 
      
 44 
     | 
    
         
            +
                  let(:links) {{ "previous" => 1, "first" => 1 }}
         
     | 
| 
      
 45 
     | 
    
         
            +
                  it { should == 1 }
         
     | 
| 
      
 46 
     | 
    
         
            +
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                context "when there is no previous page" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                  let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 50 
     | 
    
         
            +
                  it { should be_nil }
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
              end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
              describe "#first_page" do
         
     | 
| 
      
 55 
     | 
    
         
            +
                subject { page.first_page }
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                context "when there is first page available" do
         
     | 
| 
      
 58 
     | 
    
         
            +
                  let(:links) {{ "first" => 1, "previous" => 1 }}
         
     | 
| 
      
 59 
     | 
    
         
            +
                  it { should == 1 }
         
     | 
| 
      
 60 
     | 
    
         
            +
                end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                context "when there is no first page" do
         
     | 
| 
      
 63 
     | 
    
         
            +
                  let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 64 
     | 
    
         
            +
                  it { should be_nil }
         
     | 
| 
      
 65 
     | 
    
         
            +
                end
         
     | 
| 
      
 66 
     | 
    
         
            +
              end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
              describe "#last_page" do
         
     | 
| 
      
 69 
     | 
    
         
            +
                subject { page.last_page }
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                context "when there is last page available" do
         
     | 
| 
      
 72 
     | 
    
         
            +
                  let(:links) {{ "next" => 2, "last" => 2 }}
         
     | 
| 
      
 73 
     | 
    
         
            +
                  it { should == 2 }
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                context "when there is no last page" do
         
     | 
| 
      
 77 
     | 
    
         
            +
                  let(:links) {{ "previous" => 1, "first" => 1 }}
         
     | 
| 
      
 78 
     | 
    
         
            +
                  it { should be_nil }
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
              end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
              describe "#each" do
         
     | 
| 
      
 83 
     | 
    
         
            +
                it "yields resource instances for each data item" do
         
     | 
| 
      
 84 
     | 
    
         
            +
                  resources = [a_kind_of(resource_class), a_kind_of(resource_class)]
         
     | 
| 
      
 85 
     | 
    
         
            +
                  expect { |b| page.each(&b) }.to yield_successive_args(*resources)
         
     | 
| 
      
 86 
     | 
    
         
            +
                end
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                it "properly initialises the resources" do
         
     | 
| 
      
 89 
     | 
    
         
            +
                  page.map(&:id).should == ['a', 'b']
         
     | 
| 
      
 90 
     | 
    
         
            +
                end
         
     | 
| 
      
 91 
     | 
    
         
            +
              end
         
     | 
| 
      
 92 
     | 
    
         
            +
            end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
         @@ -0,0 +1,134 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'gocardless/paginator'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            describe GoCardless::Paginator do
         
     | 
| 
      
 5 
     | 
    
         
            +
              let(:resource_class) { GoCardless::Resource }
         
     | 
| 
      
 6 
     | 
    
         
            +
              let(:path) { '/test' }
         
     | 
| 
      
 7 
     | 
    
         
            +
              let(:query) { { :status => 'active' } }
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:per_page) { 10 }
         
     | 
| 
      
 9 
     | 
    
         
            +
              let(:page_number) { 1 }
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              let(:headers_p1) {{
         
     | 
| 
      
 12 
     | 
    
         
            +
                'X-Pagination' => '{"records":15,"pages":2,"links":{"next":2,"last":2}}'
         
     | 
| 
      
 13 
     | 
    
         
            +
              }}
         
     | 
| 
      
 14 
     | 
    
         
            +
              let(:response_p1) { double(:headers => headers_p1, :parsed => [{:id => 'a'}]) }
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              let(:headers_p2) {{
         
     | 
| 
      
 17 
     | 
    
         
            +
                'X-Pagination' => '{"records":15,"pages":2,"links":{"previous":1,"first":1}}'
         
     | 
| 
      
 18 
     | 
    
         
            +
              }}
         
     | 
| 
      
 19 
     | 
    
         
            +
              let(:response_p2) { double(:headers => headers_p2, :parsed => [{:id => 'b'}]) }
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
              let(:client) { double('client') }
         
     | 
| 
      
 22 
     | 
    
         
            +
              before { client.stub(:api_request).and_return(response_p1, response_p2,
         
     | 
| 
      
 23 
     | 
    
         
            +
                                                            response_p1, response_p2) }
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
              let(:paginator) { described_class.new(client, resource_class, path, query) }
         
     | 
| 
      
 26 
     | 
    
         
            +
              before { paginator.per_page(per_page) }
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              describe "#per_page" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                context "given no arguments" do
         
     | 
| 
      
 30 
     | 
    
         
            +
                  subject { paginator.per_page }
         
     | 
| 
      
 31 
     | 
    
         
            +
                  it { should == per_page }
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                context "given an argument" do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  it "is chainable" do
         
     | 
| 
      
 36 
     | 
    
         
            +
                    paginator.per_page(60).should == paginator
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                it "resets pagination metadata" do
         
     | 
| 
      
 41 
     | 
    
         
            +
                  paginator.should_receive(:load_page).exactly(2).times
         
     | 
| 
      
 42 
     | 
    
         
            +
                  paginator.count  # reset metadata, check that we have to reload it
         
     | 
| 
      
 43 
     | 
    
         
            +
                  paginator.per_page(50)
         
     | 
| 
      
 44 
     | 
    
         
            +
                  paginator.count
         
     | 
| 
      
 45 
     | 
    
         
            +
                end
         
     | 
| 
      
 46 
     | 
    
         
            +
              end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
              describe "#load_page" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                it "asks the client for the correct path" do
         
     | 
| 
      
 50 
     | 
    
         
            +
                  client.should_receive(:api_request).
         
     | 
| 
      
 51 
     | 
    
         
            +
                         with(:get, '/test', anything).
         
     | 
| 
      
 52 
     | 
    
         
            +
                         and_return(response_p1)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  paginator.page(page_number)
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                it "passes the correct pagination parameters through" do
         
     | 
| 
      
 57 
     | 
    
         
            +
                  pagination_params = { :page => page_number, :per_page => per_page }
         
     | 
| 
      
 58 
     | 
    
         
            +
                  client.should_receive(:api_request) do |_, _, opts|
         
     | 
| 
      
 59 
     | 
    
         
            +
                    opts[:params].should include pagination_params
         
     | 
| 
      
 60 
     | 
    
         
            +
                  end.and_return(response_p1)
         
     | 
| 
      
 61 
     | 
    
         
            +
                  paginator.page(page_number)
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
              describe "#each" do
         
     | 
| 
      
 66 
     | 
    
         
            +
                it "yields every item from each page" do
         
     | 
| 
      
 67 
     | 
    
         
            +
                  resources = [a_kind_of(resource_class), a_kind_of(resource_class)]
         
     | 
| 
      
 68 
     | 
    
         
            +
                  expect { |b| paginator.each(&b) }.to yield_successive_args(*resources)
         
     | 
| 
      
 69 
     | 
    
         
            +
                end
         
     | 
| 
      
 70 
     | 
    
         
            +
              end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
              describe "#each_page" do
         
     | 
| 
      
 73 
     | 
    
         
            +
                let(:pages) { [a_kind_of(GoCardless::Page), a_kind_of(GoCardless::Page)] }
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                it "yields each page until there are none left" do
         
     | 
| 
      
 76 
     | 
    
         
            +
                  expect { |b| paginator.each_page(&b) }.to yield_successive_args(*pages)
         
     | 
| 
      
 77 
     | 
    
         
            +
                end
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                it "can be iterated over multiple times" do
         
     | 
| 
      
 80 
     | 
    
         
            +
                  2.times do
         
     | 
| 
      
 81 
     | 
    
         
            +
                    expect { |b| paginator.each_page(&b) }.to yield_successive_args(*pages)
         
     | 
| 
      
 82 
     | 
    
         
            +
                  end
         
     | 
| 
      
 83 
     | 
    
         
            +
                end
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
              describe "#count" do
         
     | 
| 
      
 87 
     | 
    
         
            +
                subject { paginator.count }
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
      
 89 
     | 
    
         
            +
                context "when metadata is loaded" do
         
     | 
| 
      
 90 
     | 
    
         
            +
                  before { paginator.page(1) }
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                  it { should == 15 }
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                  it "doesn't reload metadata" do
         
     | 
| 
      
 95 
     | 
    
         
            +
                    paginator.should_not_receive(:load_page)
         
     | 
| 
      
 96 
     | 
    
         
            +
                    paginator.count
         
     | 
| 
      
 97 
     | 
    
         
            +
                  end
         
     | 
| 
      
 98 
     | 
    
         
            +
                end
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                context "when metadata is not loaded" do
         
     | 
| 
      
 101 
     | 
    
         
            +
                  it { should == 15 }
         
     | 
| 
      
 102 
     | 
    
         
            +
             
     | 
| 
      
 103 
     | 
    
         
            +
                  it "loads metadata" do
         
     | 
| 
      
 104 
     | 
    
         
            +
                    paginator.should_receive(:load_page)
         
     | 
| 
      
 105 
     | 
    
         
            +
                    paginator.count
         
     | 
| 
      
 106 
     | 
    
         
            +
                  end
         
     | 
| 
      
 107 
     | 
    
         
            +
                end
         
     | 
| 
      
 108 
     | 
    
         
            +
              end
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
              describe "#page_count" do
         
     | 
| 
      
 111 
     | 
    
         
            +
                subject { paginator.page_count }
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                context "when metadata is loaded" do
         
     | 
| 
      
 114 
     | 
    
         
            +
                  before { paginator.page(1) }
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                  it { should == 2 }
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
                  it "doesn't reload metadata" do
         
     | 
| 
      
 119 
     | 
    
         
            +
                    paginator.should_not_receive(:load_page)
         
     | 
| 
      
 120 
     | 
    
         
            +
                    paginator.page_count
         
     | 
| 
      
 121 
     | 
    
         
            +
                  end
         
     | 
| 
      
 122 
     | 
    
         
            +
                end
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
                context "when metadata is not loaded" do
         
     | 
| 
      
 125 
     | 
    
         
            +
                  it { should == 2 }
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                  it "loads metadata" do
         
     | 
| 
      
 128 
     | 
    
         
            +
                    paginator.should_receive(:load_page)
         
     | 
| 
      
 129 
     | 
    
         
            +
                    paginator.page_count
         
     | 
| 
      
 130 
     | 
    
         
            +
                  end
         
     | 
| 
      
 131 
     | 
    
         
            +
                end
         
     | 
| 
      
 132 
     | 
    
         
            +
              end
         
     | 
| 
      
 133 
     | 
    
         
            +
            end
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
         @@ -1,57 +1,20 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'spec_helper'
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            describe GoCardless::PreAuthorization do
         
     | 
| 
       4 
     | 
    
         
            -
              before  
     | 
| 
       5 
     | 
    
         
            -
                 
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
                GoCardless.account_details = {:app_id => @app_id, :app_secret => @app_secret,
         
     | 
| 
       8 
     | 
    
         
            -
                                              :token  => 'xxx', :merchant_id => '1'}
         
     | 
| 
       9 
     | 
    
         
            -
                @client = GoCardless.client
         
     | 
| 
      
 4 
     | 
    
         
            +
              before do
         
     | 
| 
      
 5 
     | 
    
         
            +
                GoCardless.account_details = {:app_id => 'abc', :app_secret => 'xyz',
         
     | 
| 
      
 6 
     | 
    
         
            +
                                              :token  => 'xxx', :merchant_id => '123'}
         
     | 
| 
       10 
7 
     | 
    
         
             
              end
         
     | 
| 
      
 8 
     | 
    
         
            +
              let(:client) { GoCardless.client }
         
     | 
| 
      
 9 
     | 
    
         
            +
              let(:preauth) { GoCardless::PreAuthorization.new(:id => '009988') }
         
     | 
| 
       11 
10 
     | 
    
         | 
| 
       12 
11 
     | 
    
         
             
              it "should be cancellable" do
         
     | 
| 
       13 
     | 
    
         
            -
                 
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                s.cancel!
         
     | 
| 
      
 12 
     | 
    
         
            +
                client.should_receive(:api_put).with('/pre_authorizations/009988/cancel')
         
     | 
| 
      
 13 
     | 
    
         
            +
                preauth.cancel!
         
     | 
| 
       16 
14 
     | 
    
         
             
              end
         
     | 
| 
       17 
15 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
               
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
                it "and_return false otherwise" do
         
     | 
| 
       24 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new.inactive?.should be_false
         
     | 
| 
       25 
     | 
    
         
            -
                end
         
     | 
| 
       26 
     | 
    
         
            -
              end
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
              describe "active query method" do
         
     | 
| 
       29 
     | 
    
         
            -
                it "and_return true when the subscription status is active" do
         
     | 
| 
       30 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new(:status => 'active').active?.should be_true
         
     | 
| 
       31 
     | 
    
         
            -
                end
         
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
                it "and_return false otherwise" do
         
     | 
| 
       34 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new.active?.should be_false
         
     | 
| 
       35 
     | 
    
         
            -
                end
         
     | 
| 
       36 
     | 
    
         
            -
              end
         
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
              describe "cancelled query method" do
         
     | 
| 
       39 
     | 
    
         
            -
                it "and_return true when the subscription status is cancelled" do
         
     | 
| 
       40 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new(:status => 'cancelled').cancelled?.should be_true
         
     | 
| 
       41 
     | 
    
         
            -
                end
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
                it "and_return false otherwise" do
         
     | 
| 
       44 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new.cancelled?.should be_false
         
     | 
| 
       45 
     | 
    
         
            -
                end
         
     | 
| 
       46 
     | 
    
         
            -
              end
         
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
              describe "expired query method" do
         
     | 
| 
       49 
     | 
    
         
            -
                it "and_return true when the subscription status is expired" do
         
     | 
| 
       50 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new(:status => 'expired').expired?.should be_true
         
     | 
| 
       51 
     | 
    
         
            -
                end
         
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
                it "and_return false otherwise" do
         
     | 
| 
       54 
     | 
    
         
            -
                  GoCardless::PreAuthorization.new.expired?.should be_false
         
     | 
| 
       55 
     | 
    
         
            -
                end
         
     | 
| 
       56 
     | 
    
         
            -
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
              it_behaves_like "it has a query method for", "inactive"
         
     | 
| 
      
 17 
     | 
    
         
            +
              it_behaves_like "it has a query method for", "active"
         
     | 
| 
      
 18 
     | 
    
         
            +
              it_behaves_like "it has a query method for", "cancelled"
         
     | 
| 
      
 19 
     | 
    
         
            +
              it_behaves_like "it has a query method for", "expired"
         
     | 
| 
       57 
20 
     | 
    
         
             
            end
         
     | 
    
        data/spec/resource_spec.rb
    CHANGED
    
    | 
         @@ -11,23 +11,18 @@ describe GoCardless::Resource do 
     | 
|
| 
       11 
11 
     | 
    
         
             
              end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
13 
     | 
    
         
             
              describe "#date_writer" do
         
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                   
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 15 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { date_writer :created_at, :modified_at }
         
     | 
| 
      
 16 
     | 
    
         
            +
                end
         
     | 
| 
       18 
17 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
      
 18 
     | 
    
         
            +
                describe "creates date writers" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :created_at= }
         
     | 
| 
      
 20 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :modified_at= }
         
     | 
| 
       21 
21 
     | 
    
         
             
                end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                it "date writers work properly" do
         
     | 
| 
       24 
     | 
    
         
            -
                  test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       25 
     | 
    
         
            -
                    date_writer :created_at
         
     | 
| 
       26 
     | 
    
         
            -
                  end
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
                  resource = test_resource.new
         
     | 
| 
       29 
24 
     | 
    
         
             
                  time = '2011-12-12T12:00:00Z'
         
     | 
| 
       30 
     | 
    
         
            -
                  resource.created_at  
     | 
| 
      
 25 
     | 
    
         
            +
                  resource = test_resource.new(:created_at => time)
         
     | 
| 
       31 
26 
     | 
    
         
             
                  date_time = resource.instance_variable_get(:@created_at)
         
     | 
| 
       32 
27 
     | 
    
         
             
                  date_time.should be_instance_of DateTime
         
     | 
| 
       33 
28 
     | 
    
         
             
                  date_time.strftime('%Y-%m-%dT%H:%M:%SZ').should == time
         
     | 
| 
         @@ -35,22 +30,18 @@ describe GoCardless::Resource do 
     | 
|
| 
       35 
30 
     | 
    
         
             
              end
         
     | 
| 
       36 
31 
     | 
    
         | 
| 
       37 
32 
     | 
    
         
             
              describe "#date_accessor" do
         
     | 
| 
       38 
     | 
    
         
            -
                 
     | 
| 
       39 
     | 
    
         
            -
                   
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 34 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { date_accessor :created_at, :modified_at }
         
     | 
| 
      
 35 
     | 
    
         
            +
                end
         
     | 
| 
       42 
36 
     | 
    
         | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       45 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       46 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
      
 37 
     | 
    
         
            +
                describe "creates date readers and writers" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :created_at= }
         
     | 
| 
      
 39 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :created_at }
         
     | 
| 
      
 40 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :modified_at= }
         
     | 
| 
      
 41 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :modified_at }
         
     | 
| 
       47 
42 
     | 
    
         
             
                end
         
     | 
| 
       48 
43 
     | 
    
         | 
| 
       49 
44 
     | 
    
         
             
                it "date readers work properly" do
         
     | 
| 
       50 
     | 
    
         
            -
                  test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       51 
     | 
    
         
            -
                    date_accessor :created_at
         
     | 
| 
       52 
     | 
    
         
            -
                  end
         
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
45 
     | 
    
         
             
                  resource = test_resource.new
         
     | 
| 
       55 
46 
     | 
    
         
             
                  date = DateTime.now
         
     | 
| 
       56 
47 
     | 
    
         
             
                  resource.instance_variable_set(:@created_at, date)
         
     | 
| 
         @@ -59,11 +50,12 @@ describe GoCardless::Resource do 
     | 
|
| 
       59 
50 
     | 
    
         
             
              end
         
     | 
| 
       60 
51 
     | 
    
         | 
| 
       61 
52 
     | 
    
         
             
              describe ".find_with_client" do
         
     | 
| 
      
 53 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 54 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { self.endpoint = '/test/:id' }
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
       62 
57 
     | 
    
         
             
                it "instantiates the correct object" do
         
     | 
| 
       63 
     | 
    
         
            -
                   
     | 
| 
       64 
     | 
    
         
            -
                    self.endpoint = '/test/:id'
         
     | 
| 
       65 
     | 
    
         
            -
                  end
         
     | 
| 
       66 
     | 
    
         
            -
                  mock_client = mock
         
     | 
| 
      
 58 
     | 
    
         
            +
                  mock_client = double
         
     | 
| 
       67 
59 
     | 
    
         
             
                  mock_client.should_receive(:api_get).and_return({:id => 123})
         
     | 
| 
       68 
60 
     | 
    
         
             
                  resource = test_resource.find_with_client(mock_client, 123)
         
     | 
| 
       69 
61 
     | 
    
         
             
                  resource.should be_a test_resource
         
     | 
| 
         @@ -72,41 +64,35 @@ describe GoCardless::Resource do 
     | 
|
| 
       72 
64 
     | 
    
         
             
              end
         
     | 
| 
       73 
65 
     | 
    
         | 
| 
       74 
66 
     | 
    
         
             
              describe ".find" do
         
     | 
| 
      
 67 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 68 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { self.endpoint = '/test/:id' }
         
     | 
| 
      
 69 
     | 
    
         
            +
                end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
       75 
71 
     | 
    
         
             
                it "calls find with the default client" do
         
     | 
| 
       76 
     | 
    
         
            -
                   
     | 
| 
       77 
     | 
    
         
            -
                    self.endpoint = '/test/:id'
         
     | 
| 
       78 
     | 
    
         
            -
                  end
         
     | 
| 
       79 
     | 
    
         
            -
                  GoCardless.stub(:client => mock)
         
     | 
| 
      
 72 
     | 
    
         
            +
                  GoCardless.stub(:client => double)
         
     | 
| 
       80 
73 
     | 
    
         
             
                  test_resource.should_receive(:find_with_client).with(GoCardless.client, 1)
         
     | 
| 
       81 
74 
     | 
    
         
             
                  test_resource.find(1)
         
     | 
| 
       82 
75 
     | 
    
         
             
                  unset_ivar GoCardless, :client
         
     | 
| 
       83 
76 
     | 
    
         
             
                end
         
     | 
| 
       84 
77 
     | 
    
         | 
| 
       85 
78 
     | 
    
         
             
                it "raises a helpful error when there is no default client" do
         
     | 
| 
       86 
     | 
    
         
            -
                  test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       87 
     | 
    
         
            -
                    self.endpoint = '/test/:id'
         
     | 
| 
       88 
     | 
    
         
            -
                  end
         
     | 
| 
       89 
79 
     | 
    
         
             
                  expect { test_resource.find(1) }.to raise_error
         
     | 
| 
       90 
80 
     | 
    
         
             
                end
         
     | 
| 
       91 
81 
     | 
    
         
             
              end
         
     | 
| 
       92 
82 
     | 
    
         | 
| 
       93 
83 
     | 
    
         
             
              describe "#reference_writer" do
         
     | 
| 
       94 
     | 
    
         
            -
                 
     | 
| 
       95 
     | 
    
         
            -
                   
     | 
| 
       96 
     | 
    
         
            -
             
     | 
| 
       97 
     | 
    
         
            -
                  end
         
     | 
| 
      
 84 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 85 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { reference_writer :merchant_id, :user_id }
         
     | 
| 
      
 86 
     | 
    
         
            +
                end
         
     | 
| 
       98 
87 
     | 
    
         | 
| 
       99 
     | 
    
         
            -
             
     | 
| 
       100 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       101 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       102 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
      
 88 
     | 
    
         
            +
                describe "creates reference writers" do
         
     | 
| 
      
 89 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant= }
         
     | 
| 
      
 90 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant_id= }
         
     | 
| 
      
 91 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user= }
         
     | 
| 
      
 92 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user_id= }
         
     | 
| 
       103 
93 
     | 
    
         
             
                end
         
     | 
| 
       104 
94 
     | 
    
         | 
| 
       105 
95 
     | 
    
         
             
                it "direct assignment methods work properly" do
         
     | 
| 
       106 
     | 
    
         
            -
                  test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       107 
     | 
    
         
            -
                    reference_writer :user_id
         
     | 
| 
       108 
     | 
    
         
            -
                  end
         
     | 
| 
       109 
     | 
    
         
            -
             
     | 
| 
       110 
96 
     | 
    
         
             
                  resource = test_resource.new
         
     | 
| 
       111 
97 
     | 
    
         
             
                  resource.user = GoCardless::User.new(:id => 123)
         
     | 
| 
       112 
98 
     | 
    
         
             
                  resource.instance_variable_get(:@user_id).should == 123
         
     | 
| 
         @@ -121,12 +107,7 @@ describe GoCardless::Resource do 
     | 
|
| 
       121 
107 
     | 
    
         
             
                end
         
     | 
| 
       122 
108 
     | 
    
         | 
| 
       123 
109 
     | 
    
         
             
                it "fails with the wrong object type" do
         
     | 
| 
       124 
     | 
    
         
            -
                  test_resource =  
     | 
| 
       125 
     | 
    
         
            -
                    reference_writer :user_id
         
     | 
| 
       126 
     | 
    
         
            -
                  end
         
     | 
| 
       127 
     | 
    
         
            -
                  expect do
         
     | 
| 
       128 
     | 
    
         
            -
                    test_resource.new.user = 'asdf'
         
     | 
| 
       129 
     | 
    
         
            -
                  end.to raise_exception ArgumentError
         
     | 
| 
      
 110 
     | 
    
         
            +
                  expect { test_resource.new.user = 'asdf' }.to raise_exception ArgumentError
         
     | 
| 
       130 
111 
     | 
    
         
             
                end
         
     | 
| 
       131 
112 
     | 
    
         
             
              end
         
     | 
| 
       132 
113 
     | 
    
         | 
| 
         @@ -138,22 +119,18 @@ describe GoCardless::Resource do 
     | 
|
| 
       138 
119 
     | 
    
         
             
                  @redirect_uri = 'http://test.com/cb'
         
     | 
| 
       139 
120 
     | 
    
         
             
                end
         
     | 
| 
       140 
121 
     | 
    
         | 
| 
       141 
     | 
    
         
            -
                 
     | 
| 
       142 
     | 
    
         
            -
                   
     | 
| 
       143 
     | 
    
         
            -
             
     | 
| 
       144 
     | 
    
         
            -
                  end
         
     | 
| 
      
 122 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 123 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) { reference_reader :merchant_id, :user_id }
         
     | 
| 
      
 124 
     | 
    
         
            +
                end
         
     | 
| 
       145 
125 
     | 
    
         | 
| 
       146 
     | 
    
         
            -
             
     | 
| 
       147 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       148 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       149 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
      
 126 
     | 
    
         
            +
                describe "creates reference readers" do
         
     | 
| 
      
 127 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant }
         
     | 
| 
      
 128 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant_id }
         
     | 
| 
      
 129 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user }
         
     | 
| 
      
 130 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user_id }
         
     | 
| 
       150 
131 
     | 
    
         
             
                end
         
     | 
| 
       151 
132 
     | 
    
         | 
| 
       152 
133 
     | 
    
         
             
                it "lookup methods work properly" do
         
     | 
| 
       153 
     | 
    
         
            -
                  test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       154 
     | 
    
         
            -
                    reference_reader :user_id
         
     | 
| 
       155 
     | 
    
         
            -
                  end
         
     | 
| 
       156 
     | 
    
         
            -
             
     | 
| 
       157 
134 
     | 
    
         
             
                  resource = test_resource.new_with_client(@client)
         
     | 
| 
       158 
135 
     | 
    
         
             
                  resource.instance_variable_set(:@user_id, 123)
         
     | 
| 
       159 
136 
     | 
    
         
             
                  @client.access_token = 'TOKEN'
         
     | 
| 
         @@ -174,19 +151,21 @@ describe GoCardless::Resource do 
     | 
|
| 
       174 
151 
     | 
    
         
             
              end
         
     | 
| 
       175 
152 
     | 
    
         | 
| 
       176 
153 
     | 
    
         
             
              describe "#reference_accessor" do
         
     | 
| 
       177 
     | 
    
         
            -
                 
     | 
| 
       178 
     | 
    
         
            -
                   
     | 
| 
      
 154 
     | 
    
         
            +
                let(:test_resource) do
         
     | 
| 
      
 155 
     | 
    
         
            +
                  Class.new(GoCardless::Resource) do
         
     | 
| 
       179 
156 
     | 
    
         
             
                    reference_accessor :merchant_id, :user_id
         
     | 
| 
       180 
157 
     | 
    
         
             
                  end
         
     | 
| 
      
 158 
     | 
    
         
            +
                end
         
     | 
| 
       181 
159 
     | 
    
         | 
| 
       182 
     | 
    
         
            -
             
     | 
| 
       183 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       184 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       185 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       186 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       187 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       188 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
       189 
     | 
    
         
            -
                  test_resource.instance_methods.map(&: 
     | 
| 
      
 160 
     | 
    
         
            +
                describe "creates reference readers and writers" do
         
     | 
| 
      
 161 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant }
         
     | 
| 
      
 162 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant_id }
         
     | 
| 
      
 163 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user }
         
     | 
| 
      
 164 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user_id }
         
     | 
| 
      
 165 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant= }
         
     | 
| 
      
 166 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :merchant_id= }
         
     | 
| 
      
 167 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user= }
         
     | 
| 
      
 168 
     | 
    
         
            +
                  specify { test_resource.instance_methods.map(&:to_sym).should include :user_id= }
         
     | 
| 
       190 
169 
     | 
    
         
             
                end
         
     | 
| 
       191 
170 
     | 
    
         
             
              end
         
     | 
| 
       192 
171 
     | 
    
         | 
| 
         @@ -211,7 +190,7 @@ describe GoCardless::Resource do 
     | 
|
| 
       211 
190 
     | 
    
         
             
                  end
         
     | 
| 
       212 
191 
     | 
    
         | 
| 
       213 
192 
     | 
    
         
             
                  it "sends the correct data parameters" do
         
     | 
| 
       214 
     | 
    
         
            -
                    client =  
     | 
| 
      
 193 
     | 
    
         
            +
                    client = double
         
     | 
| 
       215 
194 
     | 
    
         
             
                    data = {:x => 1, :y => 2}
         
     | 
| 
       216 
195 
     | 
    
         
             
                    resource = @test_resource.new_with_client(client, data)
         
     | 
| 
       217 
196 
     | 
    
         
             
                    client.should_receive(:api_post).with(anything, data)
         
     | 
| 
         @@ -219,21 +198,21 @@ describe GoCardless::Resource do 
     | 
|
| 
       219 
198 
     | 
    
         
             
                  end
         
     | 
| 
       220 
199 
     | 
    
         | 
| 
       221 
200 
     | 
    
         
             
                  it "sends the correct path" do
         
     | 
| 
       222 
     | 
    
         
            -
                    client =  
     | 
| 
      
 201 
     | 
    
         
            +
                    client = double
         
     | 
| 
       223 
202 
     | 
    
         
             
                    resource = @test_resource.new_with_client(client)
         
     | 
| 
       224 
203 
     | 
    
         
             
                    client.should_receive(:api_post).with('/test', anything)
         
     | 
| 
       225 
204 
     | 
    
         
             
                    resource.save
         
     | 
| 
       226 
205 
     | 
    
         
             
                  end
         
     | 
| 
       227 
206 
     | 
    
         | 
| 
       228 
207 
     | 
    
         
             
                  it "POSTs when not persisted" do
         
     | 
| 
       229 
     | 
    
         
            -
                    client =  
     | 
| 
      
 208 
     | 
    
         
            +
                    client = double
         
     | 
| 
       230 
209 
     | 
    
         
             
                    resource = @test_resource.new_with_client(client)
         
     | 
| 
       231 
210 
     | 
    
         
             
                    client.should_receive(:api_post)
         
     | 
| 
       232 
211 
     | 
    
         
             
                    resource.save
         
     | 
| 
       233 
212 
     | 
    
         
             
                  end
         
     | 
| 
       234 
213 
     | 
    
         | 
| 
       235 
214 
     | 
    
         
             
                  it "PUTs when already persisted" do
         
     | 
| 
       236 
     | 
    
         
            -
                    client =  
     | 
| 
      
 215 
     | 
    
         
            +
                    client = double
         
     | 
| 
       237 
216 
     | 
    
         
             
                    resource = @test_resource.new_with_client(client, :id => 1)
         
     | 
| 
       238 
217 
     | 
    
         
             
                    client.should_receive(:api_put)
         
     | 
| 
       239 
218 
     | 
    
         
             
                    resource.save
         
     | 
| 
         @@ -283,7 +262,7 @@ describe GoCardless::Resource do 
     | 
|
| 
       283 
262 
     | 
    
         
             
                end
         
     | 
| 
       284 
263 
     | 
    
         | 
| 
       285 
264 
     | 
    
         
             
                attrs = {:id => 1, :uri => 'http:', :x => 'y'}
         
     | 
| 
       286 
     | 
    
         
            -
                resource = test_resource.new_with_client( 
     | 
| 
      
 265 
     | 
    
         
            +
                resource = test_resource.new_with_client(double, attrs)
         
     | 
| 
       287 
266 
     | 
    
         
             
                resource.to_hash.should == attrs
         
     | 
| 
       288 
267 
     | 
    
         
             
              end
         
     | 
| 
       289 
268 
     | 
    
         | 
| 
         @@ -334,9 +313,8 @@ describe GoCardless::Resource do 
     | 
|
| 
       334 
313 
     | 
    
         
             
              end
         
     | 
| 
       335 
314 
     | 
    
         | 
| 
       336 
315 
     | 
    
         
             
              describe "sub_resource_uri methods" do
         
     | 
| 
      
 316 
     | 
    
         
            +
                let(:test_resource) { Class.new(GoCardless::Resource) }
         
     | 
| 
       337 
317 
     | 
    
         
             
                before :each do
         
     | 
| 
       338 
     | 
    
         
            -
                  @test_resource = Class.new(GoCardless::Resource) do
         
     | 
| 
       339 
     | 
    
         
            -
                  end
         
     | 
| 
       340 
318 
     | 
    
         
             
                  @attrs = {
         
     | 
| 
       341 
319 
     | 
    
         
             
                    'sub_resource_uris' => {
         
     | 
| 
       342 
320 
     | 
    
         
             
                      'bills' => 'https://test.com/api/bills/?merchant_id=1'
         
     | 
| 
         @@ -345,51 +323,52 @@ describe GoCardless::Resource do 
     | 
|
| 
       345 
323 
     | 
    
         
             
                end
         
     | 
| 
       346 
324 
     | 
    
         | 
| 
       347 
325 
     | 
    
         
             
                it "are defined on instances" do
         
     | 
| 
       348 
     | 
    
         
            -
                  r =  
     | 
| 
      
 326 
     | 
    
         
            +
                  r = test_resource.new(@attrs)
         
     | 
| 
       349 
327 
     | 
    
         
             
                  r.should respond_to :bills
         
     | 
| 
       350 
328 
     | 
    
         
             
                end
         
     | 
| 
       351 
329 
     | 
    
         | 
| 
       352 
330 
     | 
    
         
             
                it "aren't defined for other instances of the class" do
         
     | 
| 
       353 
     | 
    
         
            -
                   
     | 
| 
       354 
     | 
    
         
            -
                  resource =  
     | 
| 
      
 331 
     | 
    
         
            +
                  test_resource.new(@attrs)
         
     | 
| 
      
 332 
     | 
    
         
            +
                  resource = test_resource.new
         
     | 
| 
       355 
333 
     | 
    
         
             
                  resource.should_not respond_to :bills
         
     | 
| 
       356 
334 
     | 
    
         
             
                end
         
     | 
| 
       357 
335 
     | 
    
         | 
| 
       358 
336 
     | 
    
         
             
                it "use the correct uri path" do
         
     | 
| 
       359 
     | 
    
         
            -
                   
     | 
| 
       360 
     | 
    
         
            -
             
     | 
| 
       361 
     | 
    
         
            -
                  r =  
     | 
| 
      
 337 
     | 
    
         
            +
                  GoCardless::Paginator.should_receive(:new).
         
     | 
| 
      
 338 
     | 
    
         
            +
                                        with(anything, anything, '/api/bills/', anything)
         
     | 
| 
      
 339 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, @attrs)
         
     | 
| 
       362 
340 
     | 
    
         
             
                  r.bills
         
     | 
| 
       363 
341 
     | 
    
         
             
                end
         
     | 
| 
       364 
342 
     | 
    
         | 
| 
       365 
343 
     | 
    
         
             
                it "strips the api prefix from the path" do
         
     | 
| 
       366 
     | 
    
         
            -
                   
     | 
| 
       367 
     | 
    
         
            -
             
     | 
| 
      
 344 
     | 
    
         
            +
                  GoCardless::Paginator.should_receive(:new).
         
     | 
| 
      
 345 
     | 
    
         
            +
                                        with(anything, anything, '/bills/', anything)
         
     | 
| 
       368 
346 
     | 
    
         
             
                  uris = {'bills' => 'https://test.com/api/v123/bills/'}
         
     | 
| 
       369 
     | 
    
         
            -
                  r =  
     | 
| 
      
 347 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, 'sub_resource_uris' => uris)
         
     | 
| 
       370 
348 
     | 
    
         
             
                  r.bills
         
     | 
| 
       371 
349 
     | 
    
         
             
                end
         
     | 
| 
       372 
350 
     | 
    
         | 
| 
       373 
351 
     | 
    
         
             
                it "use the correct query string params" do
         
     | 
| 
       374 
     | 
    
         
            -
                   
     | 
| 
       375 
     | 
    
         
            -
                   
     | 
| 
       376 
     | 
    
         
            -
             
     | 
| 
      
 352 
     | 
    
         
            +
                  query = { 'merchant_id' => '1' }
         
     | 
| 
      
 353 
     | 
    
         
            +
                  GoCardless::Paginator.should_receive(:new).
         
     | 
| 
      
 354 
     | 
    
         
            +
                                        with(anything, anything, anything, query)
         
     | 
| 
      
 355 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, @attrs)
         
     | 
| 
       377 
356 
     | 
    
         
             
                  r.bills
         
     | 
| 
       378 
357 
     | 
    
         
             
                end
         
     | 
| 
       379 
358 
     | 
    
         | 
| 
       380 
359 
     | 
    
         
             
                it "adds provided params to existing query string params" do
         
     | 
| 
       381 
     | 
    
         
            -
                  client = mock()
         
     | 
| 
       382 
360 
     | 
    
         
             
                  params = { 'merchant_id' => '1', :amount => '10.00' }
         
     | 
| 
       383 
     | 
    
         
            -
                   
     | 
| 
       384 
     | 
    
         
            -
             
     | 
| 
      
 361 
     | 
    
         
            +
                  GoCardless::Paginator.should_receive(:new).
         
     | 
| 
      
 362 
     | 
    
         
            +
                                        with(anything, anything, anything, params)
         
     | 
| 
      
 363 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, @attrs)
         
     | 
| 
       385 
364 
     | 
    
         
             
                  r.bills(:amount => '10.00')
         
     | 
| 
       386 
365 
     | 
    
         
             
                end
         
     | 
| 
       387 
366 
     | 
    
         | 
| 
       388 
367 
     | 
    
         
             
                it "adds provided params when there are no existing query string params" do
         
     | 
| 
       389 
     | 
    
         
            -
                  client = mock()
         
     | 
| 
       390 
368 
     | 
    
         
             
                  params = { :source_id => 'xxx' }
         
     | 
| 
       391 
     | 
    
         
            -
                   
     | 
| 
       392 
     | 
    
         
            -
             
     | 
| 
      
 369 
     | 
    
         
            +
                  GoCardless::Paginator.should_receive(:new).
         
     | 
| 
      
 370 
     | 
    
         
            +
                                        with(anything, anything, anything, params)
         
     | 
| 
      
 371 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, {
         
     | 
| 
       393 
372 
     | 
    
         
             
                    'sub_resource_uris' => {
         
     | 
| 
       394 
373 
     | 
    
         
             
                      'bills' => 'https://test.com/merchants/1/bills'
         
     | 
| 
       395 
374 
     | 
    
         
             
                    }
         
     | 
| 
         @@ -398,11 +377,8 @@ describe GoCardless::Resource do 
     | 
|
| 
       398 
377 
     | 
    
         
             
                end
         
     | 
| 
       399 
378 
     | 
    
         | 
| 
       400 
379 
     | 
    
         
             
                it "return instances of the correct resource class" do
         
     | 
| 
       401 
     | 
    
         
            -
                   
     | 
| 
       402 
     | 
    
         
            -
                  r  
     | 
| 
       403 
     | 
    
         
            -
                  ret = r.bills
         
     | 
| 
       404 
     | 
    
         
            -
                  ret.should be_a Array
         
     | 
| 
       405 
     | 
    
         
            -
                  ret.first.should be_a GoCardless::Bill
         
     | 
| 
      
 380 
     | 
    
         
            +
                  r = test_resource.new_with_client(double, @attrs)
         
     | 
| 
      
 381 
     | 
    
         
            +
                  r.bills.should be_a GoCardless::Paginator
         
     | 
| 
       406 
382 
     | 
    
         
             
                end
         
     | 
| 
       407 
383 
     | 
    
         
             
              end
         
     | 
| 
       408 
384 
     | 
    
         
             
            end
         
     |