Class: IBM::Cloud::SDK::BaseCollection

Inherits:
BaseVPC
  • Object
show all
Defined in:
lib/ibm/cloud/sdk/vpc/base_collection.rb

Overview

Container that encapsulates the VPC API.

Instance Attribute Summary

Attributes inherited from BaseVPC

#connection, #endpoint, #logger

Instance Method Summary collapse

Methods inherited from BaseVPC

#adhoc, #delete, #get, #patch, #post, #put, #url

Constructor Details

#initialize(parent, endpoint, array_key: nil, child_class: nil) ⇒ BaseCollection

This class is used as a base for collection APIs.

Parameters:

  • parent (Object)

    The parent instance in the API chain.

  • endpoint (string)

    A path from the parent to the desired endpoint. In most cases is should be 1 word.

  • array_key (string) (defaults to: nil)

    The key that the API response holds the endpoint data. When nil the endpoint will be used.

  • child_class (Object) (defaults to: nil)

    The Object to be used when instanciating the single instance for this class.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 14

def initialize(parent, endpoint, array_key: nil, child_class: nil)
  # Setup empty base instance variables.
  @params = nil

  array_key ||= endpoint

  # Set the array key and child class.
  @array_key ||= array_key
  @instance ||= child_class

  super(parent, endpoint)
end

Instance Method Details

#allEnumerator

Get an iterable for the resource collection.

Returns:

  • (Enumerator)

    Use standard each, next idioms.



50
51
52
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 50

def all
  each_resource(url)
end

#countInteger

Get the total count if it exists in the response. Returns nil otherwise.

Returns:

  • (Integer)

    The total count reuturned by the server.



68
69
70
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 68

def count
  fetch.json&.fetch(:total_count)
end

#create(payload, payload_type = 'json') ⇒ IBM::Cloud::SDK::VPC::Response

A generic post method to create a resource on the collection.

Parameters:

  • payload (Hash)

    A hash of parameters to send to the server.

  • payload_type (String) (defaults to: 'json')

    One of the following options json, form, or body.

Returns:



76
77
78
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 76

def create(payload, payload_type = 'json')
  adhoc(method: 'post', payload_type: payload_type, payload: payload)
end

#dataArray

Fetch all data and return in an array.

Returns:

  • (Array)

    Hashes of the returned data.



56
57
58
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 56

def data
  all.to_a
end

#fetchIBM::Cloud::SDK::VPC::Response

Retrieve the collection from the cloud.

Returns:



44
45
46
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 44

def fetch
  @data ||= get(params: @params)
end

#has_count?Boolean

Determine if the collection has a total_count key in its response.

Returns:

  • (Boolean)


62
63
64
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 62

def has_count?
  fetch.json&.key?(:total_count)
end

#instance(id) ⇒ Object

Access a specific instance by either id or name depending on API.



81
82
83
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 81

def instance(id)
  @instance.new(self, id)
end

#params(start: nil, limit: nil, resource_group: nil) ⇒ BaseCollection

A chainable method to set query filters on the collection.

Examples:

vpc.images.params(limit: 1).all


Parameters:

  • start (String) (defaults to: nil)

    A server-supplied token determining what resource to start the page on.

  • limit (Integer) (defaults to: nil)

    The number of resources to return on a page allowed values are between 1 and 100

  • resource_group (String) (defaults to: nil)

    Filters the collection to resources within one of the resource groups identified in a comma-separated list of resource group identifiers

Returns:

  • (BaseCollection)

    This class with the param instance variable set.



34
35
36
37
38
39
40
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 34

def params(start: nil, limit: nil, resource_group: nil)
  @params = {}
  @params[:start] = start if start
  @params[:limit] = limit if limit
  @params[:resource_group] = resource_group if resource_group
  self
end