Class: IBM::Cloud::SDKHTTP::BaseCollection
- Inherits:
-
Object
- Object
- IBM::Cloud::SDKHTTP::BaseCollection
- 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.
Direct Known Subclasses
IBM::Cloud::SDK::TAG::TagCollection, IBM::Cloud::SDK::VPC::VPCCollection
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#all ⇒ Enumerator
Get an iterable for the resource collection.
-
#create(payload, payload_type = 'json') ⇒ IBM::Cloud::SDK::VPC::Response
A generic post method to create a resource on the collection.
-
#data ⇒ Array
Fetch all data and return in an array.
-
#fetch ⇒ IBM::Cloud::SDK::VPC::Response
Retrieve the collection from the cloud.
-
#initialize(parent, endpoint, array_key: nil, child_class: nil, child_id: 'id') ⇒ BaseCollection
constructor
A new instance of BaseCollection.
-
#params(limit: nil) ⇒ BaseCollection
In a Child base class add the possible query parameters for the API and return self to make it chainable.
- #reset_params ⇒ Object
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
#connection ⇒ Object (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 |
#endpoint ⇒ Object (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 |
#logger ⇒ Object (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 |
#token ⇒ Object (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
#all ⇒ Enumerator
Get an iterable for the resource collection.
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.
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 |
#data ⇒ Array
Fetch all data and return in an array.
70 71 72 |
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 70 def data all.to_a end |
#fetch ⇒ IBM::Cloud::SDK::VPC::Response
Retrieve the collection from the cloud.
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
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_params ⇒ Object
52 53 54 |
# File 'lib/ibm/cloud/sdk_http/base_collection.rb', line 52 def reset_params @params.clear end |