Skip to content

Enable Dynamic URL Generation for #pageable_get via Proc Objects#955

Merged
ryz310 merged 2 commits intomasterfrom
feature/accept-proc-within-paging-option
Dec 7, 2023
Merged

Enable Dynamic URL Generation for #pageable_get via Proc Objects#955
ryz310 merged 2 commits intomasterfrom
feature/accept-proc-within-paging-option

Conversation

@ryz310
Copy link
Owner

@ryz310 ryz310 commented Dec 7, 2023

Summary

This Pull Request introduces an enhancement to the #pageable_get method in our Ruby library. It enables the use of a Proc object for the paging option, allowing more dynamic and flexible generation of request URLs based on the response body.

Background

Previously, the paging option in the #pageable_get method was limited to specifying URLs using JSONPath. This approach was effective but had a limitation: it could not handle cases where the URL provided in the response body was not a full path. This limitation could hinder the method's utility in scenarios where the API responses include relative URLs or require more complex URL construction based on the response.

Changes

The proposed change allows the paging option to accept a Proc object. This enhancement provides the following benefits:

  1. Flexibility: Users can now define custom logic to generate the next request URL dynamically. This is particularly useful when dealing with APIs that return relative URLs or require additional processing to construct the next URL.
  2. Enhanced Control: By using a Proc, users gain finer control over the URL generation process, allowing for more complex scenarios and efficient handling of API responses.
  3. Backward Compatibility: The existing functionality using JSONPath remains intact, ensuring that current implementations are not affected.

Implementation Details

With this update, when a Proc is provided as the paging option, it will be called with the Sawyer::Response object as its argument. This allows the Proc to access the entire response body and headers, enabling it to generate the next URL based on any aspect of the response.

Example Usage

# Example of using a Proc with pageable_get
pget 'api/example', headers:, query:, paging: ->(response) {
  # Custom logic to generate the next URL
}

Conclusion

This enhancement to the #pageable_get method significantly increases its versatility and applicability to a broader range of API interactions. By allowing dynamic URL generation through a Proc, we can accommodate a wider variety of API response patterns, making our library more robust and flexible for users.

@ryz310 ryz310 added the enhancement New feature or request label Dec 7, 2023
@ryz310 ryz310 self-assigned this Dec 7, 2023
@qlty-cloud-legacy
Copy link

Code Climate has analyzed commit 4756005 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 98.6% (90% is the threshold).

This pull request will bring the total coverage in the repository to 99.1% (0.0% change).

View more on Code Climate.

@ryz310 ryz310 merged commit 3b8e8e3 into master Dec 7, 2023
@ryz310 ryz310 deleted the feature/accept-proc-within-paging-option branch December 7, 2023 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant