spyke-kaminari

Kaminari pagination for Spyke models


License
MIT
Install
gem install spyke-kaminari -v 0.0.6

Documentation

Spyke::Kaminari

Makes Spyke models aware of APIs that return pagination headers, like those from the Grape::Kaminari gem. Inspired by @DanielBlanco's Her::Kaminari.

Build Status

Installation

  1. Add to Gemfile:
gem 'spyke-kaminari', '~> 0.0.4'
  1. Add the Faraday middleware:
  Faraday.new(url: 'http://api.example.org') do |faraday|
    # Request middleware
    # ...

    # Response middleware
    faraday.use Spyke::Kaminari::HeaderParser # <-- right here!
    faraday.use JSONParser

    # Adapter middleware
    # ...
  end
  1. Include the scopes in your Spyke models:
class User < Spyke::Base
  include Spyke::Kaminari::Scopes

Usage

Scopes

# Request the second page:
User.page(2)

# Ask for 50 results per page:
User.per_page(50)

# Skip the first 5 records:
User.offset(5)

Helpers

# Iterate through each page:
User.all.each_page.map(&:count)
# => [25, 25, 18]

# Stitch together an array of all records:
users = User.all.each_page.flat_map(&:to_a)
users.count
# => 68

Metadata

User.all.total_count  # => 68
User.all.total_pages  # => 3
User.all.limit_value  # => 25
User.all.current_page # => 1
User.all.next_page    # => 2
User.all.prev_page    # => nil
User.all.offset_value # => 0