Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit 79cefdc

Browse files
committed
Merge pull request #23 from CambridgeEducation/change_pagination_api
Support for each and each_page
2 parents 0e468fd + 32c7403 commit 79cefdc

7 files changed

Lines changed: 103 additions & 195 deletions

File tree

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@ end
3535

3636
### Surveys
3737

38-
To paginate all your surveys:
38+
To enumerate all your surveys:
3939

4040
```ruby
41-
surveys = QualtricsAPI.surveys.fetch
42-
# => #<QualtricsAPI::SurveyCollection:0x007fcb72cce350 ....>
43-
44-
surveys = surveys.next_page # to fetch next page if not on the last page
45-
# => #<QualtricsAPI::SurveyCollection:0x007fcb72cce350 ....>
41+
QualtricsAPI.surveys.each do |survey|
42+
# => #<QualtricsAPI::Survey:0x007fcb72cce350 ....>
43+
end
44+
```
4645

47-
surveys.next_page?
48-
# => true
46+
To enumerate individual pages:
47+
```ruby
48+
QualtricsAPI.surveys.each_page do |page|
49+
page.each do |survey|
50+
# => #<QualtricsAPI::Survey:0x007fcb72cce350 ....>
51+
end
52+
end
4953
```
5054

5155
You can search for a survey by id:
@@ -137,14 +141,22 @@ export.status
137141

138142
### Panels
139143

140-
To paginate all the panels:
144+
To enumerate all the panels:
141145

142146
```ruby
143-
panels = QualtricsAPI.panels.fetch
144-
# => #<QualtricsAPI::PanelCollection:0x007f8769aae2c0 ....>
147+
QualtricsAPI.panels.each do |panel|
148+
# => #<QualtricsAPI::Panel:0x007f8769aae2c0 ....>
149+
end
150+
```
151+
152+
To enumerate individual pages:
145153

146-
panels = panels.next_page # get next page if panels.next_page?
147-
# => #<QualtricsAPI::PanelCollection:0x007f8769aae2c0 ....>
154+
```ruby
155+
QualtricsAPI.panels.each_page do |page|
156+
page.each do |survey|
157+
# => #<QualtricsAPI::Panel:0x007f8769aae2c0 ....>
158+
end
159+
end
148160
```
149161

150162
You can search for a panel by id:

fixtures/vcr_cassettes/panel_collection_fetch_fail.yml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/vcr_cassettes/panel_member_collection_fetch_fail.yml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/qualtrics_api/base_collection.rb

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ class BaseCollection
66
include QualtricsAPI::Extensions::SerializableCollection
77
include QualtricsAPI::Connectable
88

9-
values do
10-
attribute :page, Array, :default => []
11-
attribute :fetched, Boolean, :default => false
12-
attribute :next_endpoint, String
13-
end
14-
15-
def_delegator :page, :each
16-
17-
def fetch
18-
parse_fetch_response(QualtricsAPI.connection(self).get(list_endpoint))
19-
self
9+
def each
10+
each_page do |page|
11+
page.each do |element|
12+
yield element
13+
end
14+
end
2015
end
2116

22-
def next_page
23-
raise NotFoundError unless next_page?
24-
self.class.new.tap do |r|
25-
r.parse_fetch_response(QualtricsAPI.connection(self).get(next_endpoint))
26-
r.propagate_connection(self)
17+
def each_page
18+
endpoint = list_endpoint
19+
loop do
20+
response = QualtricsAPI.connection(self).get(endpoint)
21+
endpoint = response.body["result"]["nextPage"]
22+
yield parse_page(response)
23+
break unless endpoint
2724
end
2825
end
2926

@@ -33,19 +30,16 @@ def find(id)
3330
build_result(response.body['result']).propagate_connection(self)
3431
end
3532

36-
def next_page?
37-
raise NotYetFetchedError unless fetched
38-
!next_endpoint.nil?
39-
end
40-
4133
protected
4234

43-
def parse_fetch_response(response)
44-
@page = response.body["result"]["elements"].map do |element|
35+
def page_endpoint(fetched)
36+
fetched ? @next_endpoint : list_endpoint
37+
end
38+
39+
def parse_page(response)
40+
response.body["result"]["elements"].map do |element|
4541
build_result(element).propagate_connection(self)
4642
end
47-
@next_endpoint = response.body["result"]["nextPage"]
48-
@fetched = true
4943
end
5044
end
5145
end

spec/lib/panel_collection_spec.rb

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require 'spec_helper'
22

33
describe QualtricsAPI::PanelCollection do
4-
it "has no @page when initialized" do
5-
expect(subject.page).to eq []
6-
end
7-
84
describe "integration" do
95
describe "#find" do
106
let(:result) do
@@ -29,57 +25,31 @@
2925
end
3026
end
3127
end
32-
describe "#fetch" do
28+
describe "#each_page" do
3329
describe "when success" do
34-
before do
35-
expect(subject.page.size).to eq 0
36-
end
37-
3830
let!(:result) do
3931
VCR.use_cassette("panel_collection_fetch_success") do
40-
subject.fetch
32+
subject.each_page do |page|
33+
return page
34+
end
4135
end
4236
end
4337

4438
it "populates the collection" do
45-
expect(subject.page.size).to eq 1
46-
expect(subject.first).to be_a QualtricsAPI::Panel
47-
end
48-
49-
it "returns itself" do
50-
expect(result).to eq subject
39+
expect(result.size).to eq 1
40+
expect(result.first).to be_a QualtricsAPI::Panel
5141
end
5242
end
5343

5444
describe "when failed" do
5545
it "raises error and does not reset panels" do
56-
subject.instance_variable_set :@page, [QualtricsAPI::Panel.new({})]
5746
expect do
5847
VCR.use_cassette("panel_collection_fetch_fail") do
59-
begin
60-
subject.fetch
61-
rescue
62-
nil
63-
end
48+
subject.each_page
6449
end
65-
end.not_to change { subject.page }
50+
end.to raise_error(QualtricsAPI::BadRequestError)
6651
end
6752
end
6853
end
6954
end
70-
71-
describe 'equality' do
72-
subject { described_class.new(page: [QualtricsAPI::Panel.new("panelId" => "p1"), QualtricsAPI::Panel.new("panelId" => "p2")]) }
73-
context 'when same' do
74-
it 'returns true' do
75-
expect(subject).to eq(described_class.new(page: subject.page))
76-
end
77-
end
78-
79-
context 'when different' do
80-
it 'returns false' do
81-
expect(subject).not_to eq(described_class.new)
82-
end
83-
end
84-
end
8555
end

spec/lib/panel_member_collection_spec.rb

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require 'spec_helper'
22

33
describe QualtricsAPI::PanelMemberCollection do
4-
it "has no @page when initialized" do
5-
expect(subject.page).to eq []
6-
end
7-
84
describe "integration" do
95
subject { described_class.new(id: 'ABCD') }
106

@@ -34,34 +30,25 @@
3430

3531
describe "#fetch" do
3632
describe "when success" do
37-
before do
38-
expect(subject.page.size).to eq 0
39-
end
40-
4133
let!(:result) do
4234
VCR.use_cassette("panel_member_collection_fetch_success") do
43-
subject.fetch
35+
subject.each_page do |page|
36+
return page
37+
end
4438
end
4539
end
4640

4741
it "populates the collection" do
48-
expect(subject.page.size).to eq 1
49-
expect(subject.first).to be_a QualtricsAPI::PanelMember
50-
end
51-
52-
it "returns itself" do
53-
expect(result).to eq subject
42+
expect(result.size).to eq 1
43+
expect(result.first).to be_a QualtricsAPI::PanelMember
5444
end
5545
end
5646

5747
describe "when failed" do
58-
it "raise error and does not change panels" do
59-
subject.instance_variable_set :@page, [QualtricsAPI::PanelMember.new({})]
60-
expect do
61-
VCR.use_cassette("panel_member_collection_fetch_fail") do
62-
expect { subject.fetch }.to raise_error
63-
end
64-
end.not_to change { subject.page }
48+
it "raise error" do
49+
VCR.use_cassette("panel_member_collection_fetch_fail") do
50+
expect { subject.each_page }.to raise_error(QualtricsAPI::BadRequestError)
51+
end
6552
end
6653
end
6754
end
@@ -95,24 +82,9 @@
9582
let(:panel_members) { [QualtricsAPI::PanelMember.new] }
9683

9784
it "returns results" do
98-
expect { result }.to raise_error
85+
expect { result }.to raise_error(QualtricsAPI::BadRequestError)
9986
end
10087
end
10188
end
10289
end
103-
104-
describe 'equality' do
105-
subject { described_class.new(page: [QualtricsAPI::PanelMember.new("recipientID" => "p1"), QualtricsAPI::PanelMember.new("recipientID" => "p1")]) }
106-
context 'when same' do
107-
it 'returns true' do
108-
expect(subject).to eq(described_class.new(page: subject.page))
109-
end
110-
end
111-
112-
context 'when different' do
113-
it 'returns false' do
114-
expect(subject).not_to eq(described_class.new)
115-
end
116-
end
117-
end
11890
end

0 commit comments

Comments
 (0)