Class: CyberCoach::ResourcePage

Inherits:
AbstractResource show all
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

Attributes inherited from AbstractResource

#uri

Instance Method Summary collapse

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

#availableObject

:attr: resources The resources.



48
49
50
# File 'lib/cybercoach/resource_page.rb', line 48

def available
  @available
end

#endObject

:attr: resources The resources.



48
49
50
# File 'lib/cybercoach/resource_page.rb', line 48

def end
  @end
end

#resourcesObject

:attr: resources The resources.



48
49
50
# File 'lib/cybercoach/resource_page.rb', line 48

def resources
  @resources
end

#sizeObject

:attr: resources The resources.



48
49
50
# File 'lib/cybercoach/resource_page.rb', line 48

def size
  @size
end

#startObject

:attr: resources The resources.



48
49
50
# File 'lib/cybercoach/resource_page.rb', line 48

def start
  @start
end

#typeObject

: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(options = {})
  if @next.nil?
    raise NoNextPageError.new
  end
  invalidate_options
  options = @options.merge(options)
  response = self.class.get(@next['href'], options)
  if response.success?
    page = self.class.new(@type)
    page.deserialize(response)
    page
  else
    raise HttpError.new(response.response)
  end
end

#plural_nameObject

: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(options = {})
  if @previous.nil?
    raise NoPreviousPageError.new
  end
  invalidate_options
  options = @options.merge(options)
  response = self.class.get(@previous['href'], options)
  if response.success?
    page = self.class.new(@type)
    page.deserialize(response)
    page
  else
    raise HttpError.new(response.response)
  end
end

#resource_base_uriObject

: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_nameObject

: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