acfs 0.33.1.1.b281 → 0.33.1.1.b285

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTNiNTcyZmU5MTRmY2Y0NjA2ODgwNmE2ZWJhNjZjOWY1ZTBiMGE2MA==
4
+ ZTc2ZGE4MDNiNjgwNWMzNTBkYzJjNzU1YmEzMjRhOGFkMjE0ZDQ4MQ==
5
5
  data.tar.gz: !binary |-
6
- YzEzYjY4NDk0NGFkMWFkMDg0N2QyYjExNGNjNzEyMTIwMjhiMjA4Mw==
6
+ NjM5MzYwYTAwMmIzZDVjY2IyZDk5NmVkZDA0MGZiZjg2NjdjNTRlNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGEyZWYxMTFiZGMzMjQ4N2ZiMWQyNDVjYmFkZGViNGEzM2VmZTI2OGU5YTlh
10
- ZmRmNDUzZDE0N2FjNjNmNzlmZDk5YjJkNDA1ZDFiNDk5OTMyNzA2Y2M4M2Yz
11
- MWU1YmJmNzQ3NWIxYTZhYzM5YmRiY2FmMzQzOGZkODU0MTYxMDA=
9
+ ZmFlMzIwMDg0NGRmZjAwZDM0YmZjYTEzMTc4MzYxYTdiYjc1NTc4M2I1NGMy
10
+ ZWVmYzc0YmNkNGM4OGZmMTBiYTFkOGQ5ODc4NDdlMDM0MjU1MTNhMTFkNTFk
11
+ NTQxNGRlMDgyMGUwNzcxNmRlMmRkOGRlOGEwYjI5NmVlNWYyZTc=
12
12
  data.tar.gz: !binary |-
13
- MzZmZWJjNGRiOTZkMWIzMDMxNWFkNGViODc2OGM1MDM0YTljMjBhMzFiN2Zk
14
- MTEwNjg0NjM5YTQyODJjZDJlNzkwM2RlMzBiZGQwNDUyYmI5YjA5OTk1Yzc1
15
- NGI1ZTMyYzUwYjkyMzYxNDllZjZiMGUzZGQ3NTA5MGVkZmU0NWE=
13
+ ZTg2ZTRmZDQyOTg0MmI2MDRmNmNiMTRjY2MyYTlhNjdkMzdjNmViYjMwNzk3
14
+ MmYxZGI3YjY5YmM2ZTU0NTg5YjAxODZhN2U5MzAyMTc4MjY1YWE5MTljM2I3
15
+ OTA0ZWI1MmJiNmU1YzE5MDkxYjM2ODkwMWQ2ZDZlZTJlMjRjMTg=
@@ -1,12 +1,14 @@
1
1
  require 'delegate'
2
2
 
3
3
  require 'acfs/model/loadable'
4
+ require 'acfs/collections/paginatable'
4
5
 
5
6
  module Acfs
6
7
 
7
8
  class Collection < ::Delegator
8
9
  include Model::Loadable
9
10
  include Acfs::Util::Callbacks
11
+ include Collections::Paginatable
10
12
 
11
13
  def initialize
12
14
  super([])
@@ -0,0 +1,16 @@
1
+ module Acfs::Collections
2
+ module Paginatable
3
+ def total_pages
4
+ @total_pages
5
+ end
6
+
7
+ def current_page
8
+ @current_page
9
+ end
10
+
11
+ def setup_pagination(params, header)
12
+ @current_page = Integer(params.fetch(:page, 1)) rescue params[:page]
13
+ @total_pages = Integer(header['X-Total-Pages']) if header['X-Total-Pages']
14
+ end
15
+ end
16
+ end
@@ -75,10 +75,11 @@ module Acfs::Model
75
75
  collection = ::Acfs::Collection.new
76
76
  collection.__callbacks__ << block if block
77
77
 
78
- operation :list, params: params do |data|
78
+ operation :list, params: params do |data, header|
79
79
  data.each do |obj|
80
80
  collection << create_resource(obj)
81
81
  end
82
+ collection.setup_pagination params, header
82
83
  collection.loaded!
83
84
  collection.__invoke__
84
85
  end
@@ -50,7 +50,7 @@ module Acfs
50
50
  request = ::Acfs::Request.new url, method: method, params: params, data: data
51
51
  request.on_complete do |response|
52
52
  handle_failure response unless response.success?
53
- callback.call response.data
53
+ callback.call response.data, response.headers
54
54
  end
55
55
  request
56
56
  end
@@ -47,7 +47,8 @@ module Acfs
47
47
  if (err = opts[:raise])
48
48
  raise_error op, err, opts[:return]
49
49
  elsif (data = opts[:return])
50
- op.call data
50
+ headers = opts[:headers] || {}
51
+ op.call data, headers
51
52
  else
52
53
  raise ArgumentError, 'Unsupported stub.'
53
54
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Acfs::Collection do
4
+ describe 'Pagination' do
5
+ let(:params) { Hash.new }
6
+ let!(:collection) { MyUser.all params }
7
+
8
+ subject { Acfs.run; collection }
9
+
10
+ context 'without explicit page parameter' do
11
+ before do
12
+ stub_request(:get, 'http://users.example.org/users').to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
13
+ headers: {'X-Total-Pages' => '2'})
14
+ end
15
+
16
+ its(:total_pages) { should eq 2 }
17
+ its(:current_page) { should eq 1 }
18
+ end
19
+
20
+ context 'with page parameter' do
21
+ let(:params) { {page: 2} }
22
+ before do
23
+ stub_request(:get, 'http://users.example.org/users?page=2').to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
24
+ headers: {'X-Total-Pages' => '2'})
25
+ end
26
+
27
+ its(:total_pages) { should eq 2 }
28
+ its(:current_page) { should eq 2 }
29
+ end
30
+
31
+ context 'with non-numerical page parameter' do
32
+ let(:params) { {page: 'e546f5'} }
33
+ before do
34
+ stub_request(:get, 'http://users.example.org/users?page=e546f5').to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
35
+ headers: {'X-Total-Pages' => '2'})
36
+ end
37
+
38
+ its(:total_pages) { should eq 2 }
39
+ its(:current_page) { should eq 'e546f5' }
40
+ end
41
+ end
42
+ end
@@ -154,6 +154,20 @@ describe Acfs::Stub do
154
154
  expect(computers.last).to be_a Mac
155
155
  end
156
156
  end
157
+
158
+ context 'with header' do
159
+ before do
160
+ Acfs::Stub.resource Comment, :list,
161
+ return: [{ id: 1, text: 'Foo' }, { id: 2, text: 'Bar' }],
162
+ headers: headers
163
+ end
164
+
165
+ let!(:comments) { Comment.all }
166
+ let(:headers) { {'X-Total-Pages' => '2'} }
167
+ subject { Acfs.run; comments }
168
+
169
+ its(:total_pages) { should eq 2 }
170
+ end
157
171
  end
158
172
 
159
173
  context 'with update action' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.33.1.1.b281
4
+ version: 0.33.1.1.b285
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -123,6 +123,7 @@ files:
123
123
  - lib/acfs/adapter/base.rb
124
124
  - lib/acfs/adapter/typhoeus.rb
125
125
  - lib/acfs/collection.rb
126
+ - lib/acfs/collections/paginatable.rb
126
127
  - lib/acfs/configuration.rb
127
128
  - lib/acfs/errors.rb
128
129
  - lib/acfs/global.rb
@@ -170,6 +171,7 @@ files:
170
171
  - lib/acfs/util.rb
171
172
  - lib/acfs/version.rb
172
173
  - lib/acfs/yard.rb
174
+ - spec/acfs/collection_spec.rb
173
175
  - spec/acfs/configuration_spec.rb
174
176
  - spec/acfs/middleware/json_decoder_spec.rb
175
177
  - spec/acfs/middleware/msgpack_decoder_spec.rb
@@ -225,6 +227,7 @@ signing_key:
225
227
  specification_version: 4
226
228
  summary: An abstract API base client for service oriented application.
227
229
  test_files:
230
+ - spec/acfs/collection_spec.rb
228
231
  - spec/acfs/configuration_spec.rb
229
232
  - spec/acfs/middleware/json_decoder_spec.rb
230
233
  - spec/acfs/middleware/msgpack_decoder_spec.rb