pandarus 0.6.11 → 0.6.12
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/lib/pandarus/api_base.rb +1 -1
- data/lib/pandarus/remote_collection.rb +22 -6
- data/lib/pandarus/version.rb +1 -1
- data/spec/lib/pandarus/remote_collection_spec.rb +43 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc52b8c16be9219b4d254f887dce321efb1bb631
|
4
|
+
data.tar.gz: 3b4c28d06d6cb043f8fc0a8f6a930a8c91488941
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bafb0b9104a9bae6ab7250889085fa3b44a79b35cfe706996433b76b797791ce440bffbcf51e25e4d19363d9b51876fafd5e163a75a8fa891e7eb9583756e982
|
7
|
+
data.tar.gz: 9cecc7f837dbafdc4ebded24998749726225e14f796f4910a4ebf99042da3e7a29ec8430676dce6ab8fd08af7fdd02009266e14a34ead6db4c4e4ccddbee78eb
|
data/lib/pandarus/api_base.rb
CHANGED
@@ -6,7 +6,8 @@ module Pandarus
|
|
6
6
|
@http_client = http_client
|
7
7
|
@target_class = target_class
|
8
8
|
@path = path
|
9
|
-
@
|
9
|
+
@cache_pages = query_params[:cache_pages].nil? ? true : query_params[:cache_pages]
|
10
|
+
@query_params = query_params.except(:cache_pages)
|
10
11
|
|
11
12
|
@pagination_links = []
|
12
13
|
@next_page_cache = {}
|
@@ -39,17 +40,24 @@ module Pandarus
|
|
39
40
|
|
40
41
|
def first_page
|
41
42
|
@pagination_links = []
|
42
|
-
@first_response
|
43
|
+
response = @first_response || @http_client.get do |request|
|
43
44
|
request.path = join_paths(base_path, @path)
|
44
45
|
request.params = request.params.merge(@query_params) unless @query_params.empty?
|
45
46
|
end
|
46
|
-
|
47
|
+
@first_response ||= response if @cache_pages
|
48
|
+
handle_response(response)
|
47
49
|
end
|
48
50
|
|
49
51
|
def next_page
|
50
52
|
key = @pagination_links.last.next
|
51
|
-
|
52
|
-
|
53
|
+
# cache_pages defaults to true, but for massive remote collections
|
54
|
+
# keeping everything cached will eventually eat all the available memory
|
55
|
+
if @cache_pages
|
56
|
+
@next_page_cache[key] ||= @http_client.get(key)
|
57
|
+
handle_response @next_page_cache[key]
|
58
|
+
else
|
59
|
+
handle_response @http_client.get(key)
|
60
|
+
end
|
53
61
|
end
|
54
62
|
|
55
63
|
private
|
@@ -62,11 +70,19 @@ module Pandarus
|
|
62
70
|
def handle_response(response)
|
63
71
|
response_links = response.env[:pagination_links]
|
64
72
|
if response_links && !@pagination_links.any? {|existing_links| existing_links == response_links }
|
65
|
-
|
73
|
+
update_pagination_links(response.env[:pagination_links])
|
66
74
|
end
|
67
75
|
response.body.map{|member| @target_class.new(member) }
|
68
76
|
end
|
69
77
|
|
78
|
+
def update_pagination_links(response_pages)
|
79
|
+
if @cache_pages
|
80
|
+
@pagination_links << response_pages
|
81
|
+
else
|
82
|
+
@pagination_links = [response_pages]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
70
86
|
def base_path
|
71
87
|
@http_client.url_prefix.path
|
72
88
|
end
|
data/lib/pandarus/version.rb
CHANGED
@@ -40,6 +40,17 @@ module Pandarus
|
|
40
40
|
it 'must fetch the first page' do
|
41
41
|
expect(page.all?{|member| User === member}).to be_truthy
|
42
42
|
end
|
43
|
+
|
44
|
+
it 'caches the first page by default' do
|
45
|
+
page
|
46
|
+
expect(collection.instance_variable_get(:@first_response)).to be_present
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'does not cache the first page when page caching disabled' do
|
50
|
+
collection.instance_variable_set(:@cache_pages, false)
|
51
|
+
page
|
52
|
+
expect(collection.instance_variable_get(:@first_response)).to be_nil
|
53
|
+
end
|
43
54
|
end
|
44
55
|
|
45
56
|
describe '#to_a' do
|
@@ -51,6 +62,19 @@ module Pandarus
|
|
51
62
|
end
|
52
63
|
end
|
53
64
|
|
65
|
+
it 'defaults page caching to true' do
|
66
|
+
VCR.use_cassette('remote_collection_single_page') do
|
67
|
+
expect(collection.instance_variable_get(:@cache_pages)).to be_truthy
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'set page caching to false when the proper arg is passed' do
|
72
|
+
VCR.use_cassette('remote_collection_single_page') do
|
73
|
+
collection = RemoteCollection.new(client, Section, '', {cache_pages: false})
|
74
|
+
expect(collection.instance_variable_get(:@cache_pages)).to be_falsey
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
54
78
|
it 'must happily fetch a collection with a single page' do
|
55
79
|
VCR.use_cassette('remote_collection_single_page') do
|
56
80
|
collection = RemoteCollection.new(client, Section, '/v1/courses/14/sections', {})
|
@@ -87,6 +111,25 @@ module Pandarus
|
|
87
111
|
|
88
112
|
expect(collection.to_a.size).to eq 21
|
89
113
|
end
|
114
|
+
|
115
|
+
it 'properly caches page links when enabled' do
|
116
|
+
VCR.use_cassette('remote_collection_all_pages') do
|
117
|
+
array = collection.to_a
|
118
|
+
expect(collection.instance_variable_get(:@pagination_links).length).to eq 3
|
119
|
+
expect(collection.instance_variable_get(:@next_page_cache).keys.length).to eq 2
|
120
|
+
expect(array.size).to eq 21
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'does not cache page links when disabled' do
|
125
|
+
VCR.use_cassette('remote_collection_all_pages') do
|
126
|
+
collection.instance_variable_set(:@cache_pages, false)
|
127
|
+
array = collection.to_a
|
128
|
+
expect(collection.instance_variable_get(:@pagination_links).length).to eq 1
|
129
|
+
expect(collection.instance_variable_get(:@next_page_cache)).to be_empty
|
130
|
+
expect(array.size).to eq 21
|
131
|
+
end
|
132
|
+
end
|
90
133
|
end
|
91
134
|
end
|
92
135
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pandarus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Duane Johnson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|