-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Delighted::Person.list with auto pagination
- Loading branch information
1 parent
843fe0c
commit 56267b9
Showing
10 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
## Unreleased | ||
|
||
Features: | ||
|
||
- Add `Delighted::Person.list` | ||
|
||
## 1.8.0 (2018-05-22) | ||
|
||
Features: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Delighted | ||
class ListResource | ||
def initialize(klass, path, opts, client) | ||
@class = klass | ||
@path = path | ||
@opts = opts | ||
@client = client | ||
@iteration_count = 0 | ||
end | ||
|
||
def auto_paging_each(auto_handle_rate_limits: true) | ||
loop do | ||
begin | ||
# Get next (or first) page | ||
if @iteration_count == 0 | ||
data = @client.request_get(@path, params: @opts) | ||
else | ||
data = @client.request_get(@next_link, full_url: true) | ||
end | ||
rescue Delighted::RateLimitedError => e | ||
if auto_handle_rate_limits | ||
sleep e.response.headers['Retry-After'].to_i | ||
retry | ||
else | ||
raise | ||
end | ||
end | ||
|
||
@iteration_count += 1 | ||
@next_link = data[:response].next_link | ||
|
||
data[:json].map do |attributes| | ||
yield Object.const_get(@class).new(attributes) | ||
end | ||
|
||
break if @next_link.nil? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Delighted | ||
module Operations | ||
module List | ||
def self.included(klass) | ||
klass.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def list(opts = {}, client = Delighted.shared_client) | ||
ListResource.new(self.name, path, Utils.serialize_values(opts), client) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ class Person < Resource | |
|
||
include Operations::Create | ||
include Operations::Delete | ||
include Operations::List | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Delighted | ||
VERSION = "1.8.0" | ||
VERSION = "1.9.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters