Class: Lexile::Api::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lexile/api/page.rb

Overview

Represents a page of data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, api_model, uri, query_params = {}) ⇒ Lexile::Api::Page

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Request a page of data and store the results in this instance

Examples:

page = Page.new '/v2.1/book'

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lexile/api/page.rb', line 12

def initialize(client, api_model, uri, query_params = {})
  @client          = client
  @api_model       = api_model
  @uri             = uri
  @query_params    = query_params

  response_json = @client.get( uri, @query_params )
  @all = api_model.parse(response_json)

  raise Lexile::CannotProcessResponse.new('[:meta] is not present in response') unless response_json.has_key?('meta')

  @limit        = response_json['meta']['limit']
  @next         = response_json['meta']['next']
  #puts "NEXT URI IS ==>#{@next}<=="
  @offset       = response_json['meta']['offset']
  @previous     = response_json['meta']['previous']
  @total_count  = response_json['meta']['total_count']
end

Instance Attribute Details

#allArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all elements in page

Examples:

all_elems = page.all

Returns:

  • (Array)

    List of all elements



57
58
59
# File 'lib/lexile/api/page.rb', line 57

def all
  @all
end

Instance Method Details

#each(&blk) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterate over all elements in the page

Examples:

page.each { |elem| puts elem }

Returns:

  • (Array)

    List of all elements



48
49
50
# File 'lib/lexile/api/page.rb', line 48

def each(&blk)
  @all.each(&blk)
end

#last(num = nil) ⇒ Lexile::Model, Lexile::Api::Page

Retrieve the last element or n elements in the resource

Examples:

books = Lexile.books.first(20)
last_elem = books.last
last_elems = books.last 5

Parameters:

  • num (nil, Integer) (defaults to: nil)

    If nil, last elem; else, num elems to fetch

Returns:



68
69
70
71
# File 'lib/lexile/api/page.rb', line 68

def last(num = nil)
  return @all.last num if num
  @all.last
end

#nextLexile::Api::Page?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets next page if one is present, nil otherwise

Examples:

next_page = page.next
unless next_page.nil?
  next_page.each do |elem| puts elem; end

Returns:



38
39
40
41
# File 'lib/lexile/api/page.rb', line 38

def next
  return nil if @next.nil?
  Page.new( @client, @api_model, @next, {}  )
end