class IBM::Cloud::SDKHTTP::BaseCollection
Container that encapsulates the VPC API. This class is used as a base for collection APIs. @param parent [Object] The parent instance in the API chain. @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word. @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used. @param child_class [Object] The Object to be used when instanciating the single instance for this class.
Attributes
Public Class Methods
# 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
Public Instance Methods
Get an iterable for the resource collection. @return [Enumerator] Use standard each, next idioms.
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 64 def all each_resource(url) end
A generic post method to create a resource on the collection. @param payload [Hash] A hash of parameters to send to the server. @param payload_type [String] One of the following options json, form, or body. @return [IBM::Cloud::SDK::VPC::Response] The http response object.
# 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
Fetch all data and return in an array. @return [Array] Hashes of the returned data.
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 70 def data all.to_a end
Retrieve the collection from the cloud. @return [IBM::Cloud::SDK::VPC::Response] The http response object.
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 58 def fetch @data ||= get end
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 @return [BaseCollection] The instanticated class.
# 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
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 52 def reset_params @params.clear end
Private Instance Methods
Create a generator that removes the need for pagination. @param url [String] Full URL to send to server. @return [Enumerator] Object to page through results. @yield [BaseInstance] An instance of the instance class. @yield [Hash] When no BaseInstance
set.
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 97 def each_resource(url, &block) raise NotImplementedError('Sample only. The each_resource method needs to be customized in child class.') # rubocop:disable Lint/UnreachableCode # Sample implementation based on VPC. return enum_for(:each_resource, url) unless block_given? return unless url response = get(path: url) resources = response.fetch(@array_key.to_sym) resources&.each { |value| yield hash_instance(value) } # VPC has a next key that holds the next URL. return unless response.key?(:next) # The next data structure is a hash with a href member. next_url = response.dig(:next, :href) return unless next_url each_resource(next_url, &block) # rubocop:enable Lint/UnreachableCode end
Return a wrapped instance if set. @param value [Hash] The hash returned from server.
# File lib/ibm/cloud/sdk_http/base_collection.rb, line 86 def hash_instance(value) return @instance.new(self, data: value, id_key: @instance_id) if @instance value end