Sha256: 41808b00488863b4d586d9f24a452dd76615b9a5ee656257ec47048af2fcc641

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Taleo
  # Pages through results of a search query
  class Cursor
    attr_reader :pagination, :results, :resource, :klass, :client

    def initialize(pagination, results, resource, klass, client)
      @pagination = pagination
      @results = results
      @resource = resource
      @klass = klass
      @client = client
    end

    def items
      results.map do |i|
        klass.new(i.fetch(resource), client)
      end
    end

    def has_next?
      pagination.key?('next')
    end

    def has_previous?
      pagination.key?('previous')
    end

    def next_page
      raise Taleo::Error.new('No further results') unless has_next?

      res = client.connection.get do |req|
        req.url pagination.fetch('next')
      end

      new_cursor(res)
    end

    def previous_page
      raise Taleo::Error.new('No prior results') unless has_previous?

      res = client.connection.get do |req|
        req.url pagination.fetch('previous')
      end

      new_cursor(res)
    end

    private

    def new_cursor(res)
      data = JSON.parse(res.body)
      pagination = data.dig('response', 'pagination')
      results = data.dig('response', 'searchResults')
      Cursor.new(pagination, results, resource, klass, client)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
taleo-0.4.0 lib/taleo/cursor.rb
taleo-0.3.0 lib/taleo/cursor.rb
taleo-0.2.0 lib/taleo/cursor.rb
taleo-0.1.1 lib/taleo/cursor.rb