Class: ActiveRestClient::ResultIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_rest_client/result_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ ResultIterator

Returns a new instance of ResultIterator.



8
9
10
11
12
# File 'lib/active_rest_client/result_iterator.rb', line 8

def initialize(response = nil)
  @_status  = response.try :status
  @_headers = response.try :response_headers
  @items = []
end

Instance Attribute Details

#_headersObject (readonly)

Returns the value of attribute _headers.



6
7
8
# File 'lib/active_rest_client/result_iterator.rb', line 6

def _headers
  @_headers
end

#_statusObject

Returns the value of attribute _status.



5
6
7
# File 'lib/active_rest_client/result_iterator.rb', line 5

def _status
  @_status
end

#itemsObject (readonly)

Returns the value of attribute items.



6
7
8
# File 'lib/active_rest_client/result_iterator.rb', line 6

def items
  @items
end

Instance Method Details

#<<(item) ⇒ Object



14
15
16
# File 'lib/active_rest_client/result_iterator.rb', line 14

def <<(item)
  @items << item
end

#[](key) ⇒ Object



44
45
46
# File 'lib/active_rest_client/result_iterator.rb', line 44

def [](key)
  @items[key]
end

#eachObject



34
35
36
37
38
# File 'lib/active_rest_client/result_iterator.rb', line 34

def each
  @items.each do |el|
    yield el
  end
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_rest_client/result_iterator.rb', line 26

def empty?
  size == 0
end

#index(value) ⇒ Object



22
23
24
# File 'lib/active_rest_client/result_iterator.rb', line 22

def index(value)
  @items.index(value)
end

#lastObject



40
41
42
# File 'lib/active_rest_client/result_iterator.rb', line 40

def last
  @items.last
end

#paginate(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_rest_client/result_iterator.rb', line 70

def paginate(options = {})
  raise WillPaginateNotAvailableException.new unless Object.constants.include?(:WillPaginate)

  page     = options[:page] || 1
  per_page = options[:per_page] || WillPaginate.per_page
  total    = options[:total_entries] || @items.length

  WillPaginate::Collection.create(page, per_page, total) do |pager|
    pager.replace @items[pager.offset, pager.per_page].to_a
  end
end

#parallelise(method = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_rest_client/result_iterator.rb', line 53

def parallelise(method=nil)
  collected_responses = []
  threads = []
  @items.each do |item|
    threads << Thread.new do
      ret = item.send(method) if method
      ret = yield(item) if block_given?
      Thread.current[:response] = ret
    end
  end
  threads.each do |t|
    t.join
    collected_responses << t[:response]
  end
  collected_responses
end

#reverseObject



30
31
32
# File 'lib/active_rest_client/result_iterator.rb', line 30

def reverse
  @reversed_items ||= @items.reverse
end

#shuffleObject



48
49
50
51
# File 'lib/active_rest_client/result_iterator.rb', line 48

def shuffle
  @items = @items.shuffle
  self
end

#sizeObject



18
19
20
# File 'lib/active_rest_client/result_iterator.rb', line 18

def size
  @items.size
end