Skip to content

Commit

Permalink
[feat] Add paginated children (#291)
Browse files Browse the repository at this point in the history
- Add `all_children` and `get_next_page_of_children` functions
- Add unit tests, cassettes
  • Loading branch information
nwithan8 authored Jan 4, 2024
1 parent f2a98db commit 99d65fb
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Add `all_children` and `get_next_page_of_children` in `user` service

## v6.0.0 (2023-12-06)

- 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
Expand Down
7 changes: 5 additions & 2 deletions lib/easypost/services/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def initialize(client)

protected

def get_all_helper(endpoint, cls, params, filters = nil)
response = @client.make_request(:get, endpoint, params)
def get_all_helper(endpoint, cls, params, filters = nil, beta = false)
response = @client.make_request(
:get, endpoint, params,
beta ? 'beta' : EasyPost::InternalUtilities::Constants::API_VERSION,
)

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

Expand Down
17 changes: 17 additions & 0 deletions lib/easypost/services/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ def update_brand(id, params = {})

EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Brand)
end

# Retrieve all child Users.
def all_children(params = {})
filters = { key: 'children' }

get_all_helper('users/children', EasyPost::Models::User, params, filters)
end

# Get the next page of child users.
def get_next_page_of_children(collection, page_size = nil)
raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection)

params = { before_id: collection.children.last.id }
params[:page_size] = page_size unless page_size.nil?

all_children(params)
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,44 @@
expect(brand.color).to eq(color)
end
end

describe '.all' do
it 'retrieves all child users' do
children = client.user.all_children(
page_size: Fixture.page_size,
)
expect(children[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash)

child_user_array = children.children
expect(child_user_array.count).to be <= Fixture.page_size
expect(children.has_more).not_to be_nil
expect(child_user_array).to all(be_an_instance_of(EasyPost::Models::User))
end
end

describe '.get_next_page' do
it 'retrieves the next page of a collection' do
first_page = client.user.all_children(
page_size: Fixture.page_size,
)

begin
next_page = client.user.get_next_page_of_children(first_page)

first_page_first_id = first_page.children.first.id
next_page_first_id = next_page.children.first.id

# Did we actually get a new page?
expect(first_page_first_id).not_to eq(next_page_first_id)

# Verify that filters are being passed along for internal reference
expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq(
next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY],
)
rescue EasyPost::Errors::EndOfPaginationError => e
# If we get an error, make sure it's because there are no more pages.
expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES)
end
end
end
end

0 comments on commit 99d65fb

Please sign in to comment.