class CyberCoach::ResourcePage
A ResourcePage
can be used to navigate through many resources of a type.
Attributes
The resources available.
The end index.
:attr: resources The resources.
The size.
The start index.
The class of the resource to page.
Public Class Methods
Create a ResourcePage
of the specified type.
- hash
-
A hash of values to set on instance variables. :type is required.
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
CyberCoach::AbstractResource#initializable_with
# File lib/cybercoach/resource_page.rb, line 199 def initializable_with super + [:type, :start, :end, :size] end
CRUD
↑ topPublic Instance Methods
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
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
↑ topPublic Instance Methods
Return the plural name of the type.
# File lib/cybercoach/resource_page.rb, line 163 def plural_name @type.new.plural_name end
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
Return the singular name of the type.
# File lib/cybercoach/resource_page.rb, line 154 def singular_name @type.new.singular_name end
Invalidation
↑ topProtected Instance Methods
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
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
↑ topPublic Instance Methods
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.
CyberCoach::AbstractResource#from_serializable
# 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