Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit c0d3890

Browse files
author
Tim Rogers
authored
Merge pull request #39 from duffelhq/timrogers/list-200-all-resources
Auto-paginate through all remaining resources 200 at a time
2 parents 07ef4a6 + f690237 commit c0d3890

15 files changed

+282
-101
lines changed

lib/duffel_api/services/aircraft_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ def list(options = {})
2222
end
2323

2424
# Returns an `Enumerator` which can automatically cycle through multiple
25-
# pages of `Resources;:Aircraft`
25+
# pages of `Resources::Aircraft`.
26+
#
27+
# By default, this will use pages of 200 results under the hood, but this
28+
# can be customised by specifying the `:limit` option in the `:params`.
2629
#
2730
# @param options [Hash] options passed to `#list`, for example `:params` to
2831
# send an HTTP querystring with filters
2932
# @return [Enumerator]
3033
# @raise [Errors::Error] when the Duffel API returns an error
3134
def all(options = {})
35+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
36+
3237
DuffelAPI::Paginator.new(
3338
service: self,
3439
options: options,

lib/duffel_api/services/airlines_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ def list(options = {})
2121
end
2222

2323
# Returns an `Enumerator` which can automatically cycle through multiple
24-
# pages of `Resources;:Airline`s
24+
# pages of `Resources::Airline`s.
25+
#
26+
# By default, this will use pages of 200 results under the hood, but this
27+
# can be customised by specifying the `:limit` option in the `:params`.
2528
#
2629
# @param options [Hash] options passed to `#list`, for example `:params` to
2730
# send an HTTP querystring with filters
2831
# @return [Enumerator]
2932
# @raise [Errors::Error] when the Duffel API returns an error
3033
def all(options = {})
34+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
35+
3136
DuffelAPI::Paginator.new(
3237
service: self,
3338
options: options,

lib/duffel_api/services/airports_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ def list(options = {})
2222
end
2323

2424
# Returns an `Enumerator` which can automatically cycle through multiple
25-
# pages of `Resources;:Airport`s
25+
# pages of `Resources::Airport`s.
26+
#
27+
# By default, this will use pages of 200 results under the hood, but this
28+
# can be customised by specifying the `:limit` option in the `:params`.
2629
#
2730
# @param options [Hash] options passed to `#list`, for example `:params` to
2831
# send an HTTP querystring with filters
2932
# @return [Enumerator]
3033
# @raise [Errors::Error] when the Duffel API returns an error
3134
def all(options = {})
35+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
36+
3237
DuffelAPI::Paginator.new(
3338
service: self,
3439
options: options,

lib/duffel_api/services/offer_requests_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ def list(options = {})
5959
end
6060

6161
# Returns an `Enumerator` which can automatically cycle through multiple
62-
# pages of `Resources;:OfferRequest`s
62+
# pages of `Resources::OfferRequest`s.
63+
#
64+
# By default, this will use pages of 200 results under the hood, but this
65+
# can be customised by specifying the `:limit` option in the `:params`.
6366
#
6467
# @param options [Hash] options passed to `#list`, for example `:params` to
6568
# send an HTTP querystring with filters
6669
# @return [Enumerator]
6770
# @raise [Errors::Error] when the Duffel API returns an error
6871
def all(options = {})
72+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
73+
6974
Paginator.new(
7075
service: self,
7176
options: options,

lib/duffel_api/services/offers_service.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def list(options = {})
3232
# @return [Enumerator]
3333
# @raise [Errors::Error] when the Duffel API returns an error
3434
def all(options = {})
35-
options[:params] ||= {}
36-
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params])
35+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
3736

3837
Paginator.new(
3938
service: self,

lib/duffel_api/services/order_cancellations_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@ def list(options = {})
7171
end
7272

7373
# Returns an `Enumerator` which can automatically cycle through multiple
74-
# pages of `Resources;:OrderCancellation`s
74+
# pages of `Resources::OrderCancellation`s.
75+
#
76+
# By default, this will use pages of 200 results under the hood, but this
77+
# can be customised by specifying the `:limit` option in the `:params`.
7578
#
7679
# @param options [Hash] options passed to `#list`, for example `:params` to
7780
# send an HTTP querystring with filters
7881
# @return [Enumerator]
7982
# @raise [Errors::Error] when the Duffel API returns an error
8083
def all(options = {})
84+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
85+
8186
Paginator.new(
8287
service: self,
8388
options: options,

lib/duffel_api/services/order_change_offers_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ def list(options = {})
2222
end
2323

2424
# Returns an `Enumerator` which can automatically cycle through multiple
25-
# pages of `Resources;:OrderChangeOffer`s
25+
# pages of `Resources::OrderChangeOffer`s.
26+
#
27+
# By default, this will use pages of 200 results under the hood, but this
28+
# can be customised by specifying the `:limit` option in the `:params`.
2629
#
2730
# @param options [Hash] options passed to `#list`, for example `:params` to
2831
# send an HTTP querystring with filters
2932
# @return [Enumerator]
3033
# @raise [Errors::Error] when the Duffel API returns an error
3134
def all(options = {})
35+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
36+
3237
Paginator.new(
3338
service: self,
3439
options: options,

lib/duffel_api/services/orders_service.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ def list(options = {})
7070
end
7171

7272
# Returns an `Enumerator` which can automatically cycle through multiple
73-
# pages of `Resources;:Order`s
73+
# pages of `Resources::Order`s.
74+
#
75+
# By default, this will use pages of 200 results under the hood, but this
76+
# can be customised by specifying the `:limit` option in the `:params`.
7477
#
7578
# @param options [Hash] options passed to `#list`, for example `:params` to
7679
# send an HTTP querystring with filters
7780
# @return [Enumerator]
7881
# @raise [Errors::Error] when the Duffel API returns an error
7982
def all(options = {})
83+
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})
84+
8085
Paginator.new(
8186
service: self,
8287
options: options,

spec/duffel_api/services/aircraft_service_spec.rb

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,38 @@
8686
end
8787

8888
describe "#all" do
89-
let!(:first_response_stub) do
90-
stub_request(:get, "https://api.duffel.com/air/aircraft").to_return(
91-
body: first_page_response_body,
92-
headers: response_headers,
93-
)
94-
end
95-
9689
let(:first_page_response_body) { load_fixture("aircraft/list.json") }
9790

9891
let(:last_page_response_body) do
9992
convert_list_response_to_last_page(first_page_response_body)
10093
end
10194

95+
let(:expected_query_params) do
96+
{ limit: 200 }
97+
end
98+
99+
let!(:first_response_stub) do
100+
stub_request(:get, "https://api.duffel.com/air/aircraft").
101+
with(query: expected_query_params).
102+
to_return(
103+
body: first_page_response_body,
104+
headers: response_headers,
105+
)
106+
end
107+
102108
let!(:second_response_stub) do
103109
stub_request(:get, "https://api.duffel.com/air/aircraft").
104-
with(query: { "after" => "g3QAAAACZAACaWRtAAAAGmFyY18wMDAwOVZNRjh" \
105-
"BZ3BWNXNkTzB4WEIwZAAEbmFtZW0AAAAPQWlyYn" \
106-
"VzIEEzNDAtNTAw" }).
110+
with(query: expected_query_params.merge(
111+
"after" => "g3QAAAACZAACaWRtAAAAGmFyY18wMDAwOVZNRjhBZ3BWNXNkTzB4WEIwZAAEbmFt" \
112+
"ZW0AAAAPQWlyYnVzIEEzNDAtNTAw",
113+
)).
107114
to_return(
108115
body: last_page_response_body,
109116
headers: response_headers,
110117
)
111118
end
112119

113-
it "automatically makes the extra requests to load all the pages" do
120+
it "automatically makes the requests to load all pages, 200 results at a time" do
114121
expect(client.aircraft.all.to_a.length).to eq(100)
115122
expect(first_response_stub).to have_been_requested
116123
expect(second_response_stub).to have_been_requested
@@ -137,6 +144,21 @@
137144
expect(api_response.request_id).to eq(response_headers["x-request-id"])
138145
expect(api_response.status_code).to eq(200)
139146
end
147+
148+
context "customising the limit per page" do
149+
let(:expected_query_params) do
150+
{ limit: 33 }
151+
end
152+
153+
it "requests the requested number of items per page from the API" do
154+
client.aircraft.
155+
all(params: { limit: 33 }).
156+
to_a
157+
158+
expect(first_response_stub).to have_been_requested
159+
expect(second_response_stub).to have_been_requested
160+
end
161+
end
140162
end
141163

142164
describe "#get" do

spec/duffel_api/services/airlines_service_spec.rb

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,38 @@
8686
end
8787

8888
describe "#all" do
89-
let!(:first_response_stub) do
90-
stub_request(:get, "https://api.duffel.com/air/airlines").to_return(
91-
body: first_page_response_body,
92-
headers: response_headers,
93-
)
94-
end
95-
9689
let(:first_page_response_body) { load_fixture("airlines/list.json") }
9790

9891
let(:last_page_response_body) do
9992
convert_list_response_to_last_page(first_page_response_body)
10093
end
10194

95+
let(:expected_query_params) do
96+
{ limit: 200 }
97+
end
98+
99+
let!(:first_response_stub) do
100+
stub_request(:get, "https://api.duffel.com/air/airlines").
101+
with(query: expected_query_params).
102+
to_return(
103+
body: first_page_response_body,
104+
headers: response_headers,
105+
)
106+
end
107+
102108
let!(:second_response_stub) do
103109
stub_request(:get, "https://api.duffel.com/air/airlines").
104-
with(query: { "after" => "g3QAAAACZAACaWRtAAAAGmFybF8wMDAwOVZNRTd" \
105-
"EQUdpSmp3b21odjM1ZAAEbmFtZW0AAAARQWZyaX" \
106-
"FpeWFoIEFpcndheXM=" }).
110+
with(query: expected_query_params.merge(
111+
after: "g3QAAAACZAACaWRtAAAAGmFybF8wMDAwOVZNRTdEQUdpSmp3b21odjM1ZAAEbmFtZW0AA" \
112+
"AARQWZyaXFpeWFoIEFpcndheXM=",
113+
)).
107114
to_return(
108115
body: last_page_response_body,
109116
headers: response_headers,
110117
)
111118
end
112119

113-
it "automatically makes the extra requests to load all the pages" do
120+
it "automatically makes the requests to load all pages, 200 results at a time" do
114121
expect(client.airlines.all.to_a.length).to eq(100)
115122
expect(first_response_stub).to have_been_requested
116123
expect(second_response_stub).to have_been_requested
@@ -137,6 +144,21 @@
137144
expect(api_response.request_id).to eq(response_headers["x-request-id"])
138145
expect(api_response.status_code).to eq(200)
139146
end
147+
148+
context "customising the limit per page" do
149+
let(:expected_query_params) do
150+
{ limit: 33 }
151+
end
152+
153+
it "requests the requested number of items per page from the API" do
154+
client.airlines.
155+
all(params: { limit: 33 }).
156+
to_a
157+
158+
expect(first_response_stub).to have_been_requested
159+
expect(second_response_stub).to have_been_requested
160+
end
161+
end
140162
end
141163

142164
describe "#get" do

0 commit comments

Comments
 (0)