Sha256: 6dece2c4318a96070158a1892defee5c4102de0e8ec1434de6091f6cfbf31068

Contents?: true

Size: 1.65 KB

Versions: 24

Compression:

Stored size: 1.65 KB

Contents

module Flexirest
  class ResultIterator
    include Enumerable

    attr_accessor :_status
    attr_reader :items, :_headers

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

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

    def size
      @items.size
    end

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

    def empty?
      size == 0
    end

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

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

    def last
      @items.last
    end

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

    def shuffle
      @items = @items.shuffle
      self
    end

    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

    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

  end

  class WillPaginateNotAvailableException < StandardError ; end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
flexirest-1.3.4 lib/flexirest/result_iterator.rb
flexirest-1.3.3 lib/flexirest/result_iterator.rb
flexirest-1.3.2 lib/flexirest/result_iterator.rb
flexirest-1.3.1 lib/flexirest/result_iterator.rb
flexirest-1.2.19 lib/flexirest/result_iterator.rb
flexirest-1.2.18 lib/flexirest/result_iterator.rb
flexirest-1.2.17 lib/flexirest/result_iterator.rb
flexirest-1.2.16 lib/flexirest/result_iterator.rb
flexirest-1.2.15 lib/flexirest/result_iterator.rb
flexirest-1.2.14 lib/flexirest/result_iterator.rb
flexirest-1.2.13 lib/flexirest/result_iterator.rb
flexirest-1.2.12 lib/flexirest/result_iterator.rb
flexirest-1.2.11 lib/flexirest/result_iterator.rb
flexirest-1.2.10 lib/flexirest/result_iterator.rb
flexirest-1.2.9 lib/flexirest/result_iterator.rb
flexirest-1.2.8 lib/flexirest/result_iterator.rb
flexirest-1.2.7 lib/flexirest/result_iterator.rb
flexirest-1.2.6 lib/flexirest/result_iterator.rb
flexirest-1.2.5 lib/flexirest/result_iterator.rb
flexirest-1.2.4 lib/flexirest/result_iterator.rb