class Helium::Cursor

Public Class Methods

new(opts = {}) click to toggle source
# File lib/helium/cursor.rb, line 5
def initialize(opts = {})
  @client = opts.fetch(:client)
  @path   = opts.fetch(:path)
  @klass  = opts.fetch(:klass)
  @params = opts.fetch(:params, {})

  @collection = []
  @next_link  = nil
  @is_last    = false
end

Public Instance Methods

each(start = 0) { |element| ... } click to toggle source
# File lib/helium/cursor.rb, line 16
def each(start = 0)
  return to_enum(:each, start) unless block_given?

  Array(@collection[start..-1]).each do |element|
    yield(element)
  end

  unless last?
    start = [@collection.size, start].max

    fetch_next_page

    each(start, &Proc.new)
  end
end
to_json(*options) click to toggle source
# File lib/helium/cursor.rb, line 32
def to_json(*options)
  self.map(&:as_json).to_json(*options)
end

Private Instance Methods

fetch_next_page() click to toggle source
# File lib/helium/cursor.rb, line 38
def fetch_next_page
  if @next_link
    response = @client.get(@next_link)
  else
    response = @client.get(@path, params: @params)
  end

  json_results  = JSON.parse(response.body)
  data          = json_results["data"]
  links         = json_results["links"]

  @next_link    = links["prev"]
  @is_last      = @next_link.nil?
  @collection  += data.map{ |el| @klass.new(client: @client, params: el) }
end
last?() click to toggle source
# File lib/helium/cursor.rb, line 54
def last?
  @is_last
end