Class: ProntoForms::ResourceList

Inherits:
Resource
  • Object
show all
Defined in:
lib/prontoforms/resource_list.rb

Overview

A wrapper for retrieving paged resources.

Instance Attribute Summary collapse

Attributes inherited from Resource

#data

Instance Method Summary collapse

Methods inherited from Resource

property, resource_name, #resource_name

Constructor Details

#initialize(data, query, method, resource, client, parent = nil) ⇒ ResourceList

Initialize the resource list TODO: splat rubocop:disable Metrics/ParameterLists



22
23
24
25
26
27
28
# File 'lib/prontoforms/resource_list.rb', line 22

def initialize(data, query, method, resource, client, parent = nil)
  super(data, client)
  @query = query
  @method = method
  @resource = resource
  @parent = parent
end

Instance Attribute Details

#clientClient (readonly)

Returns API client.

Returns:



15
16
17
# File 'lib/prontoforms/resource_list.rb', line 15

def client
  @client
end

#methodSymbol (readonly)

Returns Method to send to parent object (usually the client).

Returns:

  • (Symbol)

    Method to send to parent object (usually the client)



11
12
13
# File 'lib/prontoforms/resource_list.rb', line 11

def method
  @method
end

#parentResource (readonly)

Returns Parent object (for child resources).

Returns:

  • (Resource)

    Parent object (for child resources)



17
18
19
# File 'lib/prontoforms/resource_list.rb', line 17

def parent
  @parent
end

#queryHash (readonly)

Returns Query parameters for this resource list for e.g. filters.

Returns:

  • (Hash)

    Query parameters for this resource list for e.g. filters



9
10
11
# File 'lib/prontoforms/resource_list.rb', line 9

def query
  @query
end

#resourceClass (readonly)

Returns Resource class.

Returns:

  • (Class)

    Resource class



13
14
15
# File 'lib/prontoforms/resource_list.rb', line 13

def resource
  @resource
end

Instance Method Details

#itemsArray

Retrieve the result set

Returns:

  • (Array)

    Array of resource objects



40
41
42
43
44
# File 'lib/prontoforms/resource_list.rb', line 40

def items
  @data.fetch('pageData').map do |item|
    resource.new(item, client, parent)
  end
end

#nextResourceList

Retrieve the next page of results, using the same number of items per page as the original request.

Returns:

  • (ResourceList)

    A ResourceList with the next set of results



34
35
36
# File 'lib/prontoforms/resource_list.rb', line 34

def next
  client.send(method, query: query.merge({ 'p' => query['p'] + 1 }))
end