Class: CyberCoach::ResourcePage
- Inherits:
-
AbstractResource
- Object
- AbstractResource
- CyberCoach::ResourcePage
- Defined in:
- lib/cybercoach/resource_page.rb
Overview
A ResourcePage can be used to navigate through many resources of a type.
Defined Under Namespace
Classes: NoNextPageError, NoPreviousPageError
Instance Attribute Summary collapse
-
#available ⇒ Object
:attr: resources The resources.
-
#end ⇒ Object
:attr: resources The resources.
-
#resources ⇒ Object
:attr: resources The resources.
-
#size ⇒ Object
:attr: resources The resources.
-
#start ⇒ Object
:attr: resources The resources.
-
#type ⇒ Object
:attr: resources The resources.
Attributes inherited from AbstractResource
Instance Method Summary collapse
-
#from_serializable(serializable) ⇒ Object
:category: Serialization.
-
#initialize(type) ⇒ ResourcePage
constructor
Create a ResourcePage of the specified type.
-
#next(options = {}) ⇒ Object
:category: CRUD.
-
#plural_name ⇒ Object
:category: Configuration.
-
#previous(options = {}) ⇒ Object
:category: CRUD.
-
#resource_base_uri ⇒ Object
:category: Configuration.
-
#singular_name ⇒ Object
:category: Configuration.
Methods inherited from AbstractResource
#deserialize, #read, #serialize, #to_serializable
Constructor Details
#initialize(type) ⇒ ResourcePage
Create a ResourcePage of the specified type.
- type
-
The class of resource to page.
54 55 56 57 |
# File 'lib/cybercoach/resource_page.rb', line 54 def initialize(type) super() @type = type end |
Instance Attribute Details
#available ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def available @available end |
#end ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def end @end end |
#resources ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def resources @resources end |
#size ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def size @size end |
#start ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def start @start end |
#type ⇒ Object
:attr: resources The resources.
48 49 50 |
# File 'lib/cybercoach/resource_page.rb', line 48 def type @type end |
Instance Method Details
#from_serializable(serializable) ⇒ Object
:category: Serialization
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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cybercoach/resource_page.rb', line 121 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 |
#next(options = {}) ⇒ Object
:category: CRUD
Returns the next page. Raises NoNextPageError if the is none.
- options
-
A hash of options to send with the request.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cybercoach/resource_page.rb', line 66 def next( = {}) if @next.nil? raise NoNextPageError.new end = .merge() response = self.class.get(@next['href'], ) if response.success? page = self.class.new(@type) page.deserialize(response) page else raise HttpError.new(response.response) end end |
#plural_name ⇒ Object
:category: Configuration
Return the plural name of the type.
155 156 157 |
# File 'lib/cybercoach/resource_page.rb', line 155 def plural_name @type.new.plural_name end |
#previous(options = {}) ⇒ Object
:category: CRUD
Returns the previous page. Raises NoPreviousPageError if the is none.
- options
-
A hash of options to send with the request.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cybercoach/resource_page.rb', line 89 def previous( = {}) if @previous.nil? raise NoPreviousPageError.new end = .merge() response = self.class.get(@previous['href'], ) if response.success? page = self.class.new(@type) page.deserialize(response) page else raise HttpError.new(response.response) end end |
#resource_base_uri ⇒ Object
:category: Configuration
Return the resource’s base URI of the type.
164 165 166 |
# File 'lib/cybercoach/resource_page.rb', line 164 def resource_base_uri @type.new.resource_base_uri end |
#singular_name ⇒ Object
:category: Configuration
Return the singular name of the type.
146 147 148 |
# File 'lib/cybercoach/resource_page.rb', line 146 def singular_name @type.new.singular_name end |