Class: IBM::Cloud::SDKHTTP::BaseCollection

Inherits:
Object
  • Object
show all
Includes:
BaseHTTPMixin
Defined in:
lib/ibm/cloud/sdk_http/base_collection.rb

Overview

Container that encapsulates the VPC API. This class is used as a base for collection APIs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseHTTPMixin

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

Constructor Details

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

Returns a new instance of BaseCollection.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 19

def initialize(parent, endpoint, array_key: nil, child_class: nil, child_id: 'id')
  # Setup empty base instance variables.
  @params = {}
  @token = parent.token
  array_key ||= endpoint

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

  @instance ||= child_class
  @instance_id ||= child_id

  @connection = parent.connection

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

  @endpoint = parent.url(endpoint)
  @logger = parent.logger
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



39
40
41
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 39

def connection
  @connection
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



39
40
41
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 39

def endpoint
  @endpoint
end

#loggerObject (readonly)

Returns the value of attribute logger.



39
40
41
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 39

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



39
40
41
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 39

def token
  @token
end

Instance Method Details

#allEnumerator

Get an iterable for the resource collection.

Returns:

  • (Enumerator)

    Use standard each, next idioms.



64
65
66
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 64

def all
  each_resource(url)
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:

  • (IBM::Cloud::SDK::VPC::Response)

    The http response object.



78
79
80
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 78

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.



70
71
72
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 70

def data
  all.to_a
end

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

Retrieve the collection from the cloud.

Returns:

  • (IBM::Cloud::SDK::VPC::Response)

    The http response object.



58
59
60
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 58

def fetch
  @data ||= get
end

#params(limit: nil) ⇒ BaseCollection

In a Child base class add the possible query parameters for the API and return self to make it chainable. When implemented usage would be Collection.params(limit: 2).get

Returns:



44
45
46
47
48
49
50
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 44

def params(limit: nil)
  raise NotImplementedError('Sample only. The params method needs to be customized in child class.')
  # rubocop:disable Lint/UnreachableCode
  @params[:limit] = limit if limit
  self
  # rubocop:enable Lint/UnreachableCode
end

#reset_paramsObject



52
53
54
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 52

def reset_params
  @params.clear
end