class Tinybucket::Iterator

Iterator

This iterator iterate items by sending request ot Bitbucket Cloud REST API.

Public Class Methods

new(api_client, method, *args) click to toggle source

Constructor

@param api_client [Tinybucket::Api::Base] an instance of Api Client class. @param method [Symbol] method name to invoke api_client method. @param args [Array<Object>] arguments to pass method call of

api_client as arguments.
# File lib/tinybucket/iterator.rb, line 14
def initialize(api_client, method, *args)
  @client = api_client
  @method = method
  @args = args

  @attrs = {}
  @values = []
  @pos = nil
end

Public Instance Methods

next() click to toggle source
# File lib/tinybucket/iterator.rb, line 24
def next
  next_value
end
size() click to toggle source

Get collection size.

@note This method return {developer.atlassian.com/bitbucket/api/2/reference/meta/pagination

size attribute of object collection wrapper}.
So this method may return nil.

@see developer.atlassian.com/bitbucket/api/2/reference/meta/pagination

Paging through object collections

@return [Fixnum, NillClas] collection size.

# File lib/tinybucket/iterator.rb, line 38
def size
  load_next if next?
  @attrs[:size]
end

Private Instance Methods

load_next() click to toggle source
# File lib/tinybucket/iterator.rb, line 57
def load_next
  @pos = 0 if @pos.nil?

  page = @client.send(@method, *next_params)

  @attrs = page.attrs
  @values.concat(page.items)
end
next?() click to toggle source
# File lib/tinybucket/iterator.rb, line 53
def next?
  @pos.nil? || (@values.size == @pos && @attrs[:next])
end
next_params() click to toggle source
# File lib/tinybucket/iterator.rb, line 66
def next_params
  params =
    if @attrs.empty?
      {}
    else
      query = URI.parse(@attrs[:next]).query
      Hash[*query.split(/&|=/).map { |v| CGI.unescape(v) }]
    end

  @args[-1].merge!(params)
  @args
end
next_value() click to toggle source
# File lib/tinybucket/iterator.rb, line 45
def next_value
  load_next if next?

  @values.fetch(@pos).tap { @pos += 1 }
rescue IndexError
  raise StopIteration
end