Class: UserEngage::ResourceCollection

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/user_engage/resource_collection.rb

Instance Method Summary collapse

Methods inherited from BaseModel

#resource_name

Instance Method Details

#eachObject

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



62
63
64
65
66
67
68
69
70
71
# 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!Object

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



33
34
35
36
37
38
39
40
41
42
# 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!Object

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



47
48
49
50
51
52
53
54
55
# 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!Object

Public: Transforms the results Hash to base_class instances



24
25
26
27
28
# File 'lib/user_engage/resource_collection.rb', line 24

def transform_results!
  @attributes[:results] = results.collect do |result|
    base_class.new(result)
  end
end