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

Commit

Permalink
Merge pull request #39 from duffelhq/timrogers/list-200-all-resources
Browse files Browse the repository at this point in the history
Auto-paginate through all remaining resources 200 at a time
  • Loading branch information
Tim Rogers authored Feb 15, 2022
2 parents 07ef4a6 + f690237 commit c0d3890
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 101 deletions.
7 changes: 6 additions & 1 deletion lib/duffel_api/services/aircraft_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:Aircraft`
# pages of `Resources::Aircraft`.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

DuffelAPI::Paginator.new(
service: self,
options: options,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/airlines_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:Airline`s
# pages of `Resources::Airline`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

DuffelAPI::Paginator.new(
service: self,
options: options,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/airports_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:Airport`s
# pages of `Resources::Airport`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

DuffelAPI::Paginator.new(
service: self,
options: options,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/offer_requests_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:OfferRequest`s
# pages of `Resources::OfferRequest`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

Paginator.new(
service: self,
options: options,
Expand Down
3 changes: 1 addition & 2 deletions lib/duffel_api/services/offers_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def list(options = {})
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] ||= {}
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params])
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

Paginator.new(
service: self,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/order_cancellations_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:OrderCancellation`s
# pages of `Resources::OrderCancellation`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

Paginator.new(
service: self,
options: options,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/order_change_offers_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:OrderChangeOffer`s
# pages of `Resources::OrderChangeOffer`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

Paginator.new(
service: self,
options: options,
Expand Down
7 changes: 6 additions & 1 deletion lib/duffel_api/services/orders_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ def list(options = {})
end

# Returns an `Enumerator` which can automatically cycle through multiple
# pages of `Resources;:Order`s
# pages of `Resources::Order`s.
#
# By default, this will use pages of 200 results under the hood, but this
# can be customised by specifying the `:limit` option in the `:params`.
#
# @param options [Hash] options passed to `#list`, for example `:params` to
# send an HTTP querystring with filters
# @return [Enumerator]
# @raise [Errors::Error] when the Duffel API returns an error
def all(options = {})
options[:params] = DEFAULT_ALL_PARAMS.merge(options[:params] || {})

Paginator.new(
service: self,
options: options,
Expand Down
44 changes: 33 additions & 11 deletions spec/duffel_api/services/aircraft_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,38 @@
end

describe "#all" do
let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/aircraft").to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let(:first_page_response_body) { load_fixture("aircraft/list.json") }

let(:last_page_response_body) do
convert_list_response_to_last_page(first_page_response_body)
end

let(:expected_query_params) do
{ limit: 200 }
end

let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/aircraft").
with(query: expected_query_params).
to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let!(:second_response_stub) do
stub_request(:get, "https://api.duffel.com/air/aircraft").
with(query: { "after" => "g3QAAAACZAACaWRtAAAAGmFyY18wMDAwOVZNRjh" \
"BZ3BWNXNkTzB4WEIwZAAEbmFtZW0AAAAPQWlyYn" \
"VzIEEzNDAtNTAw" }).
with(query: expected_query_params.merge(
"after" => "g3QAAAACZAACaWRtAAAAGmFyY18wMDAwOVZNRjhBZ3BWNXNkTzB4WEIwZAAEbmFt" \
"ZW0AAAAPQWlyYnVzIEEzNDAtNTAw",
)).
to_return(
body: last_page_response_body,
headers: response_headers,
)
end

it "automatically makes the extra requests to load all the pages" do
it "automatically makes the requests to load all pages, 200 results at a time" do
expect(client.aircraft.all.to_a.length).to eq(100)
expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
Expand All @@ -137,6 +144,21 @@
expect(api_response.request_id).to eq(response_headers["x-request-id"])
expect(api_response.status_code).to eq(200)
end

context "customising the limit per page" do
let(:expected_query_params) do
{ limit: 33 }
end

it "requests the requested number of items per page from the API" do
client.aircraft.
all(params: { limit: 33 }).
to_a

expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
end
end
end

describe "#get" do
Expand Down
44 changes: 33 additions & 11 deletions spec/duffel_api/services/airlines_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,38 @@
end

describe "#all" do
let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airlines").to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let(:first_page_response_body) { load_fixture("airlines/list.json") }

let(:last_page_response_body) do
convert_list_response_to_last_page(first_page_response_body)
end

let(:expected_query_params) do
{ limit: 200 }
end

let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airlines").
with(query: expected_query_params).
to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let!(:second_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airlines").
with(query: { "after" => "g3QAAAACZAACaWRtAAAAGmFybF8wMDAwOVZNRTd" \
"EQUdpSmp3b21odjM1ZAAEbmFtZW0AAAARQWZyaX" \
"FpeWFoIEFpcndheXM=" }).
with(query: expected_query_params.merge(
after: "g3QAAAACZAACaWRtAAAAGmFybF8wMDAwOVZNRTdEQUdpSmp3b21odjM1ZAAEbmFtZW0AA" \
"AARQWZyaXFpeWFoIEFpcndheXM=",
)).
to_return(
body: last_page_response_body,
headers: response_headers,
)
end

it "automatically makes the extra requests to load all the pages" do
it "automatically makes the requests to load all pages, 200 results at a time" do
expect(client.airlines.all.to_a.length).to eq(100)
expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
Expand All @@ -137,6 +144,21 @@
expect(api_response.request_id).to eq(response_headers["x-request-id"])
expect(api_response.status_code).to eq(200)
end

context "customising the limit per page" do
let(:expected_query_params) do
{ limit: 33 }
end

it "requests the requested number of items per page from the API" do
client.airlines.
all(params: { limit: 33 }).
to_a

expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
end
end
end

describe "#get" do
Expand Down
43 changes: 33 additions & 10 deletions spec/duffel_api/services/airports_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,38 @@
end

describe "#all" do
let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airports").to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let(:first_page_response_body) { load_fixture("airports/list.json") }

let(:last_page_response_body) do
convert_list_response_to_last_page(first_page_response_body)
end

let(:expected_query_params) do
{ limit: 200 }
end

let!(:first_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airports").
with(query: expected_query_params).
to_return(
body: first_page_response_body,
headers: response_headers,
)
end

let!(:second_response_stub) do
stub_request(:get, "https://api.duffel.com/air/airports").
with(query: { "after" => "g3QAAAACZAACaWRtAAAACmFycF9hZGxfYXVkAARuYW1lbQAAABBB" \
"ZGVsYWlkZSBBaXJwb3J0" }).
with(query: expected_query_params.merge(
"after" => "g3QAAAACZAACaWRtAAAACmFycF9hZGxfYXVkAARuYW1lbQAAABBBZGVsYWlkZSB" \
"BaXJwb3J0",
)).
to_return(
body: last_page_response_body,
headers: response_headers,
)
end

it "automatically makes the extra requests to load all the pages" do
it "automatically makes the requests to load all pages, 200 results at a time" do
expect(client.airports.all.to_a.length).to eq(100)
expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
Expand Down Expand Up @@ -149,6 +157,21 @@
expect(api_response.request_id).to eq(response_headers["x-request-id"])
expect(api_response.status_code).to eq(200)
end

context "customising the limit per page" do
let(:expected_query_params) do
{ limit: 33 }
end

it "requests the requested number of items per page from the API" do
client.airports.
all(params: { limit: 33 }).
to_a

expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
end
end
end

describe "#get" do
Expand Down
Loading

0 comments on commit c0d3890

Please sign in to comment.