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.



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

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

  (class << self; include ChildMixin; end) if child_class

  super(parent, endpoint)
end

Instance Method Details

#allEnumerator

Get an iterable for the resource collection.

Returns:

  • (Enumerator)

    Use standard each, next idioms.



54
55
56
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 54

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.



72
73
74
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 72

def count
  fetch.json&.fetch(:total_count, nil)
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:



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

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.



60
61
62
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 60

def data
  all.to_a
end

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

Retrieve the collection from the cloud.

Returns:



48
49
50
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 48

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

#has_count?Boolean

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 66

def has_count?
  fetch.json&.key?(:total_count)
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.



38
39
40
41
42
43
44
# File 'lib/ibm/cloud/sdk/vpc/base_collection.rb', line 38

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