class UserEngage::ResourceCollection

Public Instance Methods

each() { |result| ... } click to toggle source

Public: Iterates through whole collection and yield with each element. Goes to next page and continues to iterate, if a next page is still available

# File lib/user_engage/resource_collection.rb, line 62
def each
  loop do
    results.each do |result|
      yield(result)
    end
    go_next!
  end
rescue UserEngage::NoNextPageAvailableException
  true
end
go_next!() click to toggle source

Public: Request next pages resources and increase the current_page. Throws an UserEngage::NoNextPageAvailable if no next page is available

# File lib/user_engage/resource_collection.rb, line 33
def go_next!
  check_page_availability!(:next)

  response = UserEngage.client.get(self.next)
  update_page_related_attributes!(response)

  @attributes[:current_page] += 1

  self
end
go_previous!() click to toggle source

Public: Request previous pages resources and increase the current_page. Throws an UserEngage::NoPreviousPageAvailable if no next page is available

# File lib/user_engage/resource_collection.rb, line 47
def go_previous!
  check_page_availability!(:previous)

  response = UserEngage.client.get(previous)
  update_page_related_attributes!(response)

  @attributes[:current_page] -= 1
  self
end
transform_results!() click to toggle source

Public: Transforms the results Hash to base_class instances

# File lib/user_engage/resource_collection.rb, line 24
def transform_results!
  @attributes[:results] = results.collect do |result|
    base_class.new(result)
  end
end

Private Instance Methods

check_page_availability!(type) click to toggle source

Private: Checks the given type of page availability. If not present, it raises an UserEngage::NoNextPageAvailable or NoPreviousPageAvailable

# File lib/user_engage/resource_collection.rb, line 82
def check_page_availability!(type)
  return unless @attributes[type.to_sym].nil?

  raise(
    type.eql?(:next) ?
      UserEngage::NoNextPageAvailableException :
      UserEngage::NoPreviousPageAvailableException,
    "No #{type} page available"
  )
end