Skip to content

Commit 99d65fb

Browse files
authored
[feat] Add paginated children (#291)
- Add `all_children` and `get_next_page_of_children` functions - Add unit tests, cassettes
1 parent f2a98db commit 99d65fb

File tree

6 files changed

+204
-2
lines changed

6 files changed

+204
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Add `all_children` and `get_next_page_of_children` in `user` service
6+
37
## v6.0.0 (2023-12-06)
48

59
- Removes `with_carbon_offset` parameter from `create`, `buy` and `regenerate_rates` methods in the shipment service since now EasyPost offers carbon neutral shipments by default for free

lib/easypost/services/base.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ def initialize(client)
1010

1111
protected
1212

13-
def get_all_helper(endpoint, cls, params, filters = nil)
14-
response = @client.make_request(:get, endpoint, params)
13+
def get_all_helper(endpoint, cls, params, filters = nil, beta = false)
14+
response = @client.make_request(
15+
:get, endpoint, params,
16+
beta ? 'beta' : EasyPost::InternalUtilities::Constants::API_VERSION,
17+
)
1518

1619
response[EasyPost::InternalUtilities::Constants::FILTERS_KEY] = filters unless filters.nil?
1720

lib/easypost/services/user.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,21 @@ def update_brand(id, params = {})
7878

7979
EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Brand)
8080
end
81+
82+
# Retrieve all child Users.
83+
def all_children(params = {})
84+
filters = { key: 'children' }
85+
86+
get_all_helper('users/children', EasyPost::Models::User, params, filters)
87+
end
88+
89+
# Get the next page of child users.
90+
def get_next_page_of_children(collection, page_size = nil)
91+
raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection)
92+
93+
params = { before_id: collection.children.last.id }
94+
params[:page_size] = page_size unless page_size.nil?
95+
96+
all_children(params)
97+
end
8198
end

spec/cassettes/user/EasyPost_Services_User_all_retrieves_all_child_users.yml

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

spec/cassettes/user/EasyPost_Services_User_get_next_page_retrieves_the_next_page_of_a_collection.yml

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

spec/user_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,44 @@
7979
expect(brand.color).to eq(color)
8080
end
8181
end
82+
83+
describe '.all' do
84+
it 'retrieves all child users' do
85+
children = client.user.all_children(
86+
page_size: Fixture.page_size,
87+
)
88+
expect(children[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash)
89+
90+
child_user_array = children.children
91+
expect(child_user_array.count).to be <= Fixture.page_size
92+
expect(children.has_more).not_to be_nil
93+
expect(child_user_array).to all(be_an_instance_of(EasyPost::Models::User))
94+
end
95+
end
96+
97+
describe '.get_next_page' do
98+
it 'retrieves the next page of a collection' do
99+
first_page = client.user.all_children(
100+
page_size: Fixture.page_size,
101+
)
102+
103+
begin
104+
next_page = client.user.get_next_page_of_children(first_page)
105+
106+
first_page_first_id = first_page.children.first.id
107+
next_page_first_id = next_page.children.first.id
108+
109+
# Did we actually get a new page?
110+
expect(first_page_first_id).not_to eq(next_page_first_id)
111+
112+
# Verify that filters are being passed along for internal reference
113+
expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq(
114+
next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY],
115+
)
116+
rescue EasyPost::Errors::EndOfPaginationError => e
117+
# If we get an error, make sure it's because there are no more pages.
118+
expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES)
119+
end
120+
end
121+
end
82122
end

0 commit comments

Comments
 (0)