class CyberCoach::ResourcePage

A ResourcePage can be used to navigate through many resources of a type.

Attributes

available[RW]

The resources available.

end[RW]

The end index.

resources[RW]

:attr: resources The resources.

size[RW]

The size.

start[RW]

The start index.

type[RW]

The class of the resource to page.

Public Class Methods

new(hash = {}) click to toggle source

Create a ResourcePage of the specified type.

hash

A hash of values to set on instance variables. :type is required.

Calls superclass method CyberCoach::AbstractResource::new
# File lib/cybercoach/resource_page.rb, line 54
def initialize(hash = {})
  unless hash.key? :type
    fail ArgumentError, 'requires a :type entry in the hash'
  end
  super(hash)
end

Protected Instance Methods

initializable_with() click to toggle source
# File lib/cybercoach/resource_page.rb, line 199
def initializable_with
  super + [:type, :start, :end, :size]
end

CRUD

↑ top

Public Instance Methods

next(options = {}, invalidate = true) click to toggle source

Returns the next page. Raises NoNextPageError if the is none.

options

A hash of options to send with the request.

invalidate

Invalidates it when true, skips invalidation when false.

# File lib/cybercoach/resource_page.rb, line 69
def next(options = {}, invalidate = true)
  if @next.nil?
    fail NoNextPageError
  end
  if invalidate
    self.invalidate
  end
  options = @options.merge(options)
  response = self.class.get(@next['href'], options)
  if response.success?
    page = self.class.new(type: @type)
    page.deserialize(response)
    page
  else
    fail HttpError, response.response
  end
end
previous(options = {}, invalidate = true) click to toggle source

Returns the previous page. Raises NoPreviousPageError if the is none.

options

A hash of options to send with the request.

invalidate

Invalidates it when true, skips invalidation when false.

# File lib/cybercoach/resource_page.rb, line 95
def previous(options = {}, invalidate = true)
  if @previous.nil?
    fail NoPreviousPageError
  end
  if invalidate
    self.invalidate
  end
  options = @options.merge(options)
  response = self.class.get(@previous['href'], options)
  if response.success?
    page = self.class.new(type: @type)
    page.deserialize(response)
    page
  else
    fail HttpError, response.response
  end
end

Configuration

↑ top

Public Instance Methods

plural_name() click to toggle source

Return the plural name of the type.

# File lib/cybercoach/resource_page.rb, line 163
def plural_name
  @type.new.plural_name
end
resource_base_uri() click to toggle source

Return the resource’s base URI of the type.

# File lib/cybercoach/resource_page.rb, line 172
def resource_base_uri
  @type.new.resource_base_uri
end
singular_name() click to toggle source

Return the singular name of the type.

# File lib/cybercoach/resource_page.rb, line 154
def singular_name
  @type.new.singular_name
end

Invalidation

↑ top

Protected Instance Methods

invalidate_options() click to toggle source

Sets the start and size attributes as query parameters.

# File lib/cybercoach/resource_page.rb, line 183
def invalidate_options
  @options[:query] = {
    start: @start,
    size: @size
  }
end
invalidate_uri() click to toggle source

Sets the URI to the types’ base URI.

# File lib/cybercoach/resource_page.rb, line 195
def invalidate_uri
  @uri = resource_base_uri
end

Serialization

↑ top

Public Instance Methods

from_serializable(serializable) click to toggle source

Creates itself from a serializable representation, which only contains simple data types.

serializable

A hash with the keys:

  • uri

    The URI.

  • start

    The start index.

  • end

    The end index.

  • size

    The size.

  • available

    The resources available.

  • links

    Links as hashes with the keys:

    • description

      May be ‘next’ or ‘previous’.

    • href

      The URI of the referenced page.

  • @plural_name

    Items mapped to the plural name of the @type.

# File lib/cybercoach/resource_page.rb, line 129
def from_serializable(serializable)
  super(serializable)
  @start = serializable['start']
  @end = serializable['end']
  @size = serializable['size']
  @available = serializable['available']
  if serializable['links'].nil?
    @next = nil
    @previous = nil
  else
    @next = serializable['links'].find { |link| link['description'] == 'next' }
    @previous = serializable['links'].find { |link| link['description'] == 'previous' }
  end
  @resources = serializable[plural_name].map do |resource_serializable|
    resource = @type.new
    resource.from_serializable(resource_serializable)
    resource
  end
end