uploadcare / uploadcare-ruby

Ruby API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
https://uploadcare.com
MIT License
39 stars 28 forks source link

Reimplemented FileList and GroupList to support API v0.5 pagination #31

Closed vizvamitra closed 7 years ago

vizvamitra commented 7 years ago

This change will break gem's interface, so we should 🎉bump the major version🎉 later. Also I think that this change needs an upgrade notes section which I'll introduce later on in a separate PR.

vizvamitra commented 7 years ago

@dmitry-mukhin I've tried to make the inmplementation more understandable. What do you think about it?

def each
  return enum_for(:each) unless block_given?

  resource_enumerator.each { |object| yield object }

  self
end

private

def get_next_page
  return nil if fully_loaded?

  next_page = build_data(api.get(@data.meta['next']))

  @data = OpenStruct.new(
    meta: next_page.meta,
    objects: objects + next_page.objects
  )

  next_page
end

def resource_enumerator
  Enumerator.new do |yielder|
    objects.each { |obj| yielder << obj }

    while next_page = get_next_page do
      next_page.objects.each { |obj| yielder << obj }
    end
  end
end