Class: Flexirest::ResultIterator
- Inherits:
-
Object
- Object
- Flexirest::ResultIterator
- Includes:
- Enumerable
- Defined in:
- lib/flexirest/result_iterator.rb
Instance Attribute Summary collapse
-
#_headers ⇒ Object
readonly
Returns the value of attribute _headers.
-
#_status ⇒ Object
Returns the value of attribute _status.
-
#items ⇒ Object
Returns the value of attribute items.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #index(value) ⇒ Object
-
#initialize(response = nil) ⇒ ResultIterator
constructor
A new instance of ResultIterator.
- #join(*args) ⇒ Object
- #last ⇒ Object
- #paginate(options = {}) ⇒ Object
- #parallelise(method = nil) ⇒ Object
- #reverse ⇒ Object
- #shuffle ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #where(criteria = {}) ⇒ Object
Constructor Details
#initialize(response = nil) ⇒ ResultIterator
Returns a new instance of ResultIterator.
8 9 10 11 12 |
# File 'lib/flexirest/result_iterator.rb', line 8 def initialize(response = nil) @_status = response.try :status @_headers = response.try :response_headers @items = [] end |
Instance Attribute Details
#_headers ⇒ Object (readonly)
Returns the value of attribute _headers.
6 7 8 |
# File 'lib/flexirest/result_iterator.rb', line 6 def _headers @_headers end |
#_status ⇒ Object
Returns the value of attribute _status.
5 6 7 |
# File 'lib/flexirest/result_iterator.rb', line 5 def _status @_status end |
#items ⇒ Object
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
14 15 16 |
# File 'lib/flexirest/result_iterator.rb', line 14 def <<(item) @items << item end |
#[](key) ⇒ Object
52 53 54 |
# File 'lib/flexirest/result_iterator.rb', line 52 def [](key) @items[key] end |
#[]=(key, value) ⇒ Object
56 57 58 |
# File 'lib/flexirest/result_iterator.rb', line 56 def []=(key, value) @items[key] = value end |
#delete_if ⇒ Object
65 66 67 68 |
# File 'lib/flexirest/result_iterator.rb', line 65 def delete_if @items = @items.delete_if &Proc.new self end |
#each ⇒ Object
42 43 44 45 46 |
# File 'lib/flexirest/result_iterator.rb', line 42 def each @items.each do |el| yield el end end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/flexirest/result_iterator.rb', line 34 def empty? size == 0 end |
#index(value) ⇒ Object
30 31 32 |
# File 'lib/flexirest/result_iterator.rb', line 30 def index(value) @items.index(value) end |
#join(*args) ⇒ Object
26 27 28 |
# File 'lib/flexirest/result_iterator.rb', line 26 def join(*args) @items.join(*args) end |
#last ⇒ Object
48 49 50 |
# File 'lib/flexirest/result_iterator.rb', line 48 def last @items.last end |
#paginate(options = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/flexirest/result_iterator.rb', line 101 def paginate( = {}) raise WillPaginateNotAvailableException.new unless Object.constants.include?(:WillPaginate) page = [:page] || 1 per_page = [:per_page] || WillPaginate.per_page total = [: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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/flexirest/result_iterator.rb', line 84 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 |
#reverse ⇒ Object
38 39 40 |
# File 'lib/flexirest/result_iterator.rb', line 38 def reverse @reversed_items ||= @items.reverse end |
#shuffle ⇒ Object
60 61 62 63 |
# File 'lib/flexirest/result_iterator.rb', line 60 def shuffle @items = @items.shuffle self end |
#size ⇒ Object
18 19 20 |
# File 'lib/flexirest/result_iterator.rb', line 18 def size @items.size end |
#to_a ⇒ Object
22 23 24 |
# File 'lib/flexirest/result_iterator.rb', line 22 def to_a @items end |
#where(criteria = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flexirest/result_iterator.rb', line 70 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 |