acfs 0.34.0.1.b286 → 0.34.0.1.b287

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDM4N2M1ZWVjMjA0N2UwZmNjMDhlZDA4YmQ4YTBiODhkZDc3NTY0NA==
4
+ ZTA0YWNiNjAxMDkzZGNhNjRhYzAyMGM5MGI2NGYwNjMzMDY2OTY0Mg==
5
5
  data.tar.gz: !binary |-
6
- NGQzZWRhNGI0Yjk4MzZjZjA2ZmY0YzFiNzI1ZTczZDI5NTA2ZTk1Mw==
6
+ OWIwMTgxYjg5NDc5NjMyZDY5YTc2MzVkY2JiMmRiOGYwZDZkZWMwNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YThiNjVlZjVjNTMzY2Y1NGQzNDdlZTcxYmFlNzYxMGUyZWZjNzVhNWJmMmQx
10
- MzllN2YxOTc1NjNjZDM1ZmE3MjljODE0N2ZmZmVjNjcxOGYwZTFlMDc4NjJl
11
- NmRlY2Y2MzRmOTU1OTVmNjFiNzRlYzZiZWFlZmZhYjkwNzc0NTA=
9
+ MzUwM2JjOWI3OTU0ZGZlZDJhOGYyMTY4N2UxNDA3MTU1Zjg3M2MyMjE3YTc5
10
+ ZTVjYzJlY2U0N2VmODU4MjQ0YzRiOGQ4ZmVmZmY0OWVjMTA1N2I0MjIzNDgz
11
+ OTRjMmM1MTA2OGMwMTY4YTNjZjNkMjU1NjgxZWU0YzBlMWI1MGQ=
12
12
  data.tar.gz: !binary |-
13
- OTY5ZmY4YThjOTkxOGI3NDkzMWE1MzlmMGY3MDA4YzczNzk5MmZmYjFkZWMy
14
- Y2M3NDU3ZGMzNTdmZmM3YTJiNTk4ZTM5NWY3MmM0NWY2ZjA1OTc3MzQ1NTQz
15
- NmY3ZWZjMzgwOThkMzExN2NiNDA3YTE5MzVmZDAxMTUxZmEwMzQ=
13
+ ODQyY2FhMDMzMjZiODdiZTgwYTEyNjhmZjlkMjRhNGI4MGU5M2ZjYzU1MzAw
14
+ NWU2N2Q1MWZjOWE3N2NiMTUxOWRjMWZiZTE2YmYzZTA4MmE2NWQwMTg4NGEw
15
+ NGMyMDUxYjE3MzI2YTM3ZTcxY2MwNmE2N2NmYzhmMGZhYmNmYTI=
@@ -10,8 +10,10 @@ module Acfs
10
10
  include Acfs::Util::Callbacks
11
11
  include Collections::Paginatable
12
12
 
13
- def initialize
13
+ def initialize(resource_class)
14
14
  super([])
15
+
16
+ @resource_class = resource_class
15
17
  end
16
18
 
17
19
  def __getobj__
@@ -1,5 +1,13 @@
1
1
  module Acfs::Collections
2
2
  module Paginatable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def self.operation(action, opts = {}, &block)
7
+ opts[:url]
8
+ end
9
+ end
10
+
3
11
  def total_pages
4
12
  @total_pages
5
13
  end
@@ -8,9 +16,56 @@ module Acfs::Collections
8
16
  @current_page
9
17
  end
10
18
 
11
- def setup_pagination(params, header)
19
+ def process_response(response)
20
+ setup_params response.request.params if response.request
21
+ setup_headers response.headers
22
+ end
23
+
24
+ def next_page
25
+ page 'next'
26
+ end
27
+
28
+ def prev_page
29
+ page 'prev'
30
+ end
31
+
32
+ def first_page
33
+ page 'first'
34
+ end
35
+
36
+ def last_page
37
+ page 'last'
38
+ end
39
+
40
+ def page(rel)
41
+ if relations[rel]
42
+ @resource_class.all nil, url: relations[rel]
43
+ else
44
+ raise ArgumentError.new "No relative page `#{rel}'."
45
+ end
46
+ end
47
+
48
+ private
49
+ def relations
50
+ @relations ||= {}
51
+ end
52
+
53
+ def setup_headers(headers)
54
+ @total_pages = Integer(headers['X-Total-Pages']) if headers['X-Total-Pages']
55
+
56
+ setup_links headers['Link'] if headers['Link']
57
+ end
58
+
59
+ def setup_links(links)
60
+ links.split(/,\s+/).each do |link|
61
+ if link =~ /^\s*<([^>]+)>.*\s+rel="([\w_-]+)".*$/
62
+ relations[$2] = $1
63
+ end
64
+ end
65
+ end
66
+
67
+ def setup_params(params)
12
68
  @current_page = Integer(params.fetch(:page, 1)) rescue params[:page]
13
- @total_pages = Integer(header['X-Total-Pages']) if header['X-Total-Pages']
14
69
  end
15
70
  end
16
71
  end
@@ -71,15 +71,15 @@ module Acfs::Model
71
71
  #
72
72
  # @return [ Collection ] Collection of requested resources.
73
73
  #
74
- def all(params = {}, &block)
75
- collection = ::Acfs::Collection.new
74
+ def all(params = {}, opts = {}, &block)
75
+ collection = ::Acfs::Collection.new self
76
76
  collection.__callbacks__ << block if block
77
77
 
78
- operation :list, params: params do |data, header|
78
+ operation :list, opts.merge(params: params) do |data, response|
79
79
  data.each do |obj|
80
80
  collection << create_resource(obj)
81
81
  end
82
- collection.setup_pagination params, header
82
+ collection.process_response response
83
83
  collection.loaded!
84
84
  collection.__invoke__
85
85
  end
@@ -151,7 +151,7 @@ module Acfs::Model
151
151
  end
152
152
 
153
153
  def find_multiple(ids, opts, &block)
154
- ::Acfs::Collection.new.tap do |collection|
154
+ ::Acfs::Collection.new(self).tap do |collection|
155
155
  collection.__callbacks__ << block unless block.nil?
156
156
 
157
157
  counter = 0
@@ -19,8 +19,12 @@ module Acfs
19
19
  @params = (opts[:params] || {}).dup
20
20
  @data = (opts[:data] || {}).dup
21
21
 
22
- @location = resource.location(action: @action).extract_from(@params, @data)
23
- @url = location.str
22
+ if opts[:url]
23
+ @url = opts[:url]
24
+ else
25
+ @location = resource.location(action: @action).extract_from(@params, @data)
26
+ @url = location.str
27
+ end
24
28
 
25
29
  @callback = block
26
30
  end
@@ -39,7 +43,11 @@ module Acfs
39
43
  end
40
44
 
41
45
  def full_params
42
- (id ? params.merge(id: id) : params).merge location.args
46
+ (id ? params.merge(id: id) : params).merge location_args
47
+ end
48
+
49
+ def location_args
50
+ location ? location.args : {}
43
51
  end
44
52
 
45
53
  def method
@@ -50,7 +58,7 @@ module Acfs
50
58
  request = ::Acfs::Request.new url, method: method, params: params, data: data
51
59
  request.on_complete do |response|
52
60
  handle_failure response unless response.success?
53
- callback.call response.data, response.headers
61
+ callback.call response.data, response
54
62
  end
55
63
  request
56
64
  end
data/lib/acfs/request.rb CHANGED
@@ -18,8 +18,6 @@ module Acfs
18
18
  @headers = options.delete(:headers) || {}
19
19
  @params = options.delete(:params) || {}
20
20
  @method = options.delete(:method) || :get
21
-
22
- url.query = nil # params.any? ? params.to_param : nil
23
21
  end.to_s
24
22
  on_complete &block if block_given?
25
23
  end
data/lib/acfs/stub.rb CHANGED
@@ -5,7 +5,7 @@ module Acfs
5
5
  # Global handler for stubbing resources.
6
6
  #
7
7
  class Stub
8
- ACTIONS = [ :read, :create, :update, :delete, :list ]
8
+ ACTIONS = [:read, :create, :update, :delete, :list]
9
9
 
10
10
  attr_reader :opts
11
11
 
@@ -26,8 +26,8 @@ module Acfs
26
26
  return true if opts[:with] == params || data == opts[:with]
27
27
  return true if (opts[:with].nil? && params.empty? && data.empty?)
28
28
 
29
- return true if opts[:with].reject{|k,v|v.nil?} == params.reject{|k,v|v.nil?}
30
- return true if opts[:with].reject{|k,v|v.nil?} == data.reject{|k,v|v.nil?}
29
+ return true if opts[:with].reject { |k, v| v.nil? } == params.reject { |k, v| v.nil? }
30
+ return true if opts[:with].reject { |k, v| v.nil? } == data.reject { |k, v| v.nil? }
31
31
 
32
32
  false
33
33
  end
@@ -44,11 +44,17 @@ module Acfs
44
44
  def call(op)
45
45
  calls << op
46
46
 
47
- if (err = opts[:raise])
47
+ err = opts[:raise]
48
+ data = opts[:return]
49
+
50
+ if err
48
51
  raise_error op, err, opts[:return]
49
- elsif (data = opts[:return])
50
- headers = opts[:headers] || {}
51
- op.call data, headers
52
+ elsif data
53
+ response = Acfs::Response.new op.request,
54
+ headers: opts[:headers] || {},
55
+ status: opts[:status] || 200,
56
+ data: data || {}
57
+ op.call data, response
52
58
  else
53
59
  raise ArgumentError, 'Unsupported stub.'
54
60
  end
@@ -72,7 +78,7 @@ module Acfs
72
78
  raise ArgumentError, "Unknown action `#{action}`." unless ACTIONS.include? action
73
79
 
74
80
  Stub.new(opts).tap do |stub|
75
- stubs[klass] ||= {}
81
+ stubs[klass] ||= {}
76
82
  stubs[klass][action] ||= []
77
83
  stubs[klass][action] << stub
78
84
  end
@@ -90,8 +96,13 @@ module Acfs
90
96
  @enabled ||= false
91
97
  end
92
98
 
93
- def enable; @enabled = true end
94
- def disable; @enabled = false end
99
+ def enable;
100
+ @enabled = true
101
+ end
102
+
103
+ def disable;
104
+ @enabled = false
105
+ end
95
106
 
96
107
  # Clear all stubs.
97
108
  #
@@ -1,16 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Acfs::Collection do
4
+ let(:model) { MyUser }
5
+
4
6
  describe 'Pagination' do
5
7
  let(:params) { Hash.new }
6
- let!(:collection) { MyUser.all params }
8
+ let!(:collection) { model.all params }
7
9
 
8
10
  subject { Acfs.run; collection }
9
11
 
10
12
  context 'without explicit page parameter' do
11
13
  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
+ stub_request(:get, 'http://users.example.org/users').
15
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
16
+ headers: {'X-Total-Pages' => '2'})
14
17
  end
15
18
 
16
19
  its(:total_pages) { should eq 2 }
@@ -20,8 +23,9 @@ describe Acfs::Collection do
20
23
  context 'with page parameter' do
21
24
  let(:params) { {page: 2} }
22
25
  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'})
26
+ stub_request(:get, 'http://users.example.org/users?page=2').
27
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
28
+ headers: {'X-Total-Pages' => '2'})
25
29
  end
26
30
 
27
31
  its(:total_pages) { should eq 2 }
@@ -31,12 +35,109 @@ describe Acfs::Collection do
31
35
  context 'with non-numerical page parameter' do
32
36
  let(:params) { {page: 'e546f5'} }
33
37
  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'})
38
+ stub_request(:get, 'http://users.example.org/users?page=e546f5').
39
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
40
+ headers: {'X-Total-Pages' => '2'})
36
41
  end
37
42
 
38
43
  its(:total_pages) { should eq 2 }
39
44
  its(:current_page) { should eq 'e546f5' }
40
45
  end
46
+
47
+ describe '#next_page' do
48
+ before do
49
+ stub_request(:get, 'http://users.example.org/users').
50
+ to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
51
+ headers: {
52
+ 'X-Total-Pages' => '2',
53
+ 'Link' => '<http://users.example.org/users?page=2>; rel="next"'
54
+ })
55
+ end
56
+ let!(:req) do
57
+ stub_request(:get, 'http://users.example.org/users?page=2').to_return response([])
58
+ end
59
+ let!(:collection) { model.all }
60
+ subject { Acfs.run; collection.next_page }
61
+
62
+ it { should be_a Acfs::Collection }
63
+
64
+ it 'should have fetched page 2' do
65
+ subject
66
+ Acfs.run
67
+ expect(req).to have_been_requested
68
+ end
69
+ end
70
+
71
+ describe '#prev_page' do
72
+ before do
73
+ stub_request(:get, 'http://users.example.org/users?page=2').
74
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
75
+ headers: {
76
+ 'X-Total-Pages' => '2',
77
+ 'Link' => '<http://users.example.org/users>; rel="prev"'
78
+ })
79
+ end
80
+ let!(:req) do
81
+ stub_request(:get, 'http://users.example.org/users').to_return response([])
82
+ end
83
+ let!(:collection) { model.all page: 2 }
84
+ subject { Acfs.run; collection.prev_page }
85
+
86
+ it { should be_a Acfs::Collection }
87
+
88
+ it 'should have fetched page 1' do
89
+ subject
90
+ Acfs.run
91
+ expect(req).to have_been_requested
92
+ end
93
+ end
94
+
95
+ describe '#first_page' do
96
+ before do
97
+ stub_request(:get, 'http://users.example.org/users?page=2').
98
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
99
+ headers: {
100
+ 'X-Total-Pages' => '2',
101
+ 'Link' => '<http://users.example.org/users>; rel="first"'
102
+ })
103
+ end
104
+ let!(:req) do
105
+ stub_request(:get, 'http://users.example.org/users').to_return response([])
106
+ end
107
+ let!(:collection) { model.all page: 2 }
108
+ subject { Acfs.run; collection.first_page }
109
+
110
+ it { should be_a Acfs::Collection }
111
+
112
+ it 'should have fetched page 1' do
113
+ subject
114
+ Acfs.run
115
+ expect(req).to have_been_requested
116
+ end
117
+ end
118
+
119
+ describe '#last_page' do
120
+ before do
121
+ stub_request(:get, 'http://users.example.org/users?page=2').
122
+ to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
123
+ headers: {
124
+ 'X-Total-Pages' => '2',
125
+ 'Link' => '<http://users.example.org/users?page=12>; rel="last"'
126
+ })
127
+ end
128
+ let!(:req) do
129
+ stub_request(:get, 'http://users.example.org/users?page=12').to_return response([])
130
+ end
131
+ let!(:collection) { model.all page: 2 }
132
+ subject { Acfs.run; collection.last_page }
133
+
134
+ it { should be_a Acfs::Collection }
135
+
136
+ it 'should have fetched page 1' do
137
+ subject
138
+ Acfs.run
139
+ expect(req).to have_been_requested
140
+ end
141
+ end
41
142
  end
42
143
  end
@@ -21,15 +21,6 @@ describe Acfs::Request do
21
21
  expect(request.url).to be == "#{url}"
22
22
  end
23
23
  end
24
-
25
- context 'with parameters in URL' do
26
- let(:url) { 'http://api.example.org/v1/examples?b=ac' }
27
- let(:params) { { id: 10 }}
28
-
29
- it 'should strip query from URL' do
30
- expect(request.url).to be == 'http://api.example.org/v1/examples'
31
- end
32
- end
33
24
  end
34
25
 
35
26
  describe '#headers' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.0.1.b286
4
+ version: 0.34.0.1.b287
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen