-
-
Save fujimura/c5800fa191db4842b569 to your computer and use it in GitHub Desktop.
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
class Client | |
def initialize | |
@all_entries = [*1..30].map { |i| "Hello this is #{i}" } | |
end | |
def get(option) | |
from = option[:from_id] || 0 | |
@all_entries[from..(from + 5)] | |
end | |
end | |
def get_all(client) | |
result = [] | |
from_id = 0 | |
response = client.get(from_id: from_id) | |
result.push *response | |
until response.empty? | |
from_id += response.count | |
response = client.get(from_id: from_id) | |
result.push *response | |
end | |
result | |
end | |
class PostEnumerator | |
include Enumerable | |
def initialize(client) | |
@client = client | |
@cursor = 0 | |
end | |
def each(&block) | |
response = @client.get(from_id: @cursor) | |
return if response.empty? | |
response.each &block | |
@cursor += response.count | |
each &block | |
end | |
end | |
def get_all_refactored(client) | |
PostEnumerator.new(client).to_a | |
end | |
client = Client.new | |
puts get_all(client) | |
puts get_all_refactored(client) | |
puts get_all(client) == get_all_refactored(client) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment