Class: Flexirest::ResultIterator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ ResultIterator

Returns a new instance of ResultIterator.



10
11
12
13
14
# File 'lib/flexirest/result_iterator.rb', line 10

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/flexirest/result_iterator.rb', line 6

def _headers
  @_headers
end

#_parentObject

Returns the value of attribute _parent.



7
8
9
# File 'lib/flexirest/result_iterator.rb', line 7

def _parent
  @_parent
end

#_parent_attribute_nameObject

Returns the value of attribute _parent_attribute_name.



8
9
10
# File 'lib/flexirest/result_iterator.rb', line 8

def _parent_attribute_name
  @_parent_attribute_name
end

#_statusObject

Returns the value of attribute _status.



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

def _status
  @_status
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

Instance Method Details

#<<(item) ⇒ Object



16
17
18
# File 'lib/flexirest/result_iterator.rb', line 16

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

#[](key) ⇒ Object



58
59
60
# File 'lib/flexirest/result_iterator.rb', line 58

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

#[]=(key, value) ⇒ Object



62
63
64
# File 'lib/flexirest/result_iterator.rb', line 62

def []=(key, value)
  @items[key] = value
end

#_clean!Object



20
21
22
# File 'lib/flexirest/result_iterator.rb', line 20

def _clean!
  @items.map(&:_clean!)
end

#delete_if(&block) ⇒ Object



71
72
73
74
# File 'lib/flexirest/result_iterator.rb', line 71

def delete_if(&block)
  @items = @items.delete_if &block
  self
end

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  size == 0
end

#indexObject



36
37
38
# File 'lib/flexirest/result_iterator.rb', line 36

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

#join(*args) ⇒ Object



32
33
34
# File 'lib/flexirest/result_iterator.rb', line 32

def join(*args)
  @items.join(*args)
end

#lastObject



54
55
56
# File 'lib/flexirest/result_iterator.rb', line 54

def last
  @items.last
end

#paginate(options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/flexirest/result_iterator.rb', line 107

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/flexirest/result_iterator.rb', line 90

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



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

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

#shuffleObject



66
67
68
69
# File 'lib/flexirest/result_iterator.rb', line 66

def shuffle
  @items = @items.shuffle
  self
end

#sizeObject



24
25
26
# File 'lib/flexirest/result_iterator.rb', line 24

def size
  @items.size
end

#to_aObject



28
29
30
# File 'lib/flexirest/result_iterator.rb', line 28

def to_a
  @items
end

#where(criteria = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flexirest/result_iterator.rb', line 76

def where(criteria={})
  @items.select do |object|
    select = true
    criteria.each do |k, v|
      if v.is_a?(Regexp)
        select = false if !object[k][v]
      else
        select = false if object[k] != v
      end
    end
    select
  end
end