Skip to content

Instantly share code, notes, and snippets.

@fujimura
Last active November 29, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimura/c5800fa191db4842b569 to your computer and use it in GitHub Desktop.
Save fujimura/c5800fa191db4842b569 to your computer and use it in GitHub Desktop.
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