class Brightbox::Api
Attributes
Public Class Methods
Source
# File lib/brightbox-cli/api.rb, line 194 def self.cache_all! @cache = {} all.each do |f| @cache[f.id] = f Brightbox.config.cache_id f.id end end
Source
# File lib/brightbox-cli/api.rb, line 176 def self.cached_get(id) @cache ||= {} value = @cache[id] if value value else Brightbox.config.cache_id id if Brightbox.config.respond_to?(:cache_id) @cache[id] = get(id) end end
Source
# File lib/brightbox-cli/api.rb, line 22 def self.conn @@connection_manager ||= Brightbox::ConnectionManager.new(Brightbox.config.to_fog) @@connection_manager.fetch_connection(require_account?) end
Returns the current connection to the Brightbox
API, creating a new {ConnectionManager} and connection if necessary.
@return [Fog::Brightbox::Compute::Real]
Source
# File lib/brightbox-cli/api.rb, line 110 def self.find(args = :all, options = {}) raise InvalidArguments, "find(nil)" if args.nil? raise InvalidArguments, "find([])" if args.respond_to?(:empty?) && args.empty? options = { :order => :created_at }.merge(options) objects = nil object = nil # get the data from the Api if args == :all objects = all elsif args.is_a? String object = cached_get(args.to_s) raise NotFound, "Couldn't find '#{args}'" if object.nil? elsif args.respond_to? :map objects = args.map do |arg| o = cached_get(arg.to_s) raise NotFound, "Couldn't find '#{arg}'" if o.nil? o end else raise InvalidArguments, "Couldn't find '#{args.class}'" end if objects # wrap in our objects objects.map! { |o| new(o) } # Sort objects.sort! do |a, b| sort_method = options[:order] begin a.send(sort_method) <=> b.send(sort_method) rescue NoMethodError 0 end end objects elsif object new(object) end end
General finder to return instances based on identifiers or all.
@param args [Array<Object>, Object] Search settings. Passing :all
will return all resources. An identifier (String) will return that one and a collection of strings will return those resources.
@param options [Hash]
@return [Array<Object>] A collection of API models @return [Object] A single API model if requested with a single identifier
@raise [Brightbox::Api::InvalidArguments] if args
can not be used to
search for.
Source
# File lib/brightbox-cli/api.rb, line 87 def self.find_all_or_warn(identifiers) if identifiers.empty? find(:all) else find_or_call(identifiers) do |identifier| warn "Could not find anything with ID #{identifier}" end end end
Will return everything unless a subset has been passed in
@param [Array<String>] identifiers series of values to call to call
Source
# File lib/brightbox-cli/api.rb, line 187 def self.find_by_handle(handle) object = find(:all).find { |obj| obj.handle == handle } raise Api::NotFound, "Invalid #{klass_name} #{handle}" if object.nil? object end
Source
# File lib/brightbox-cli/api.rb, line 154 def self.find_or_call(ids, &_block) objects = [] ids.each do |id| objects << find(id) rescue Api::NotFound yield id end objects end
Find each id in the given array. Yield the block with any ids that couldn’t be found
Source
# File lib/brightbox-cli/api.rb, line 39 def self.klass_name name.split("::").last end
@return [String] the ‘name’ of the class
Source
# File lib/brightbox-cli/api.rb, line 43 def initialize(model = nil) if model.is_a? String @id = model elsif model.respond_to?(:attributes) && model.respond_to?(:id) @fog_model = model @id = model.id else raise InvalidArguments, "Can't initialize #{self.class} with #{model.inspect}" end Brightbox.config.cache_id(@id) if Brightbox.config.respond_to?(:cache_id) end
Source
# File lib/brightbox-cli/api.rb, line 33 def self.require_account?; false; end
Returns true
if instances of this class require account details to be used. This changes the type of connection needed and the authentication details.
@return [Boolean]
Public Instance Methods
Source
# File lib/brightbox-cli/api.rb, line 204 def created_on return unless fog_model.created_at fog_model.created_at.strftime("%Y-%m-%d") end
Displays creation date in ISO 8601 Complete date format
Source
# File lib/brightbox-cli/api.rb, line 69 def exists? !fog_model.nil? rescue NotFound false end
Source
# File lib/brightbox-cli/api.rb, line 61 def fog_attributes IndifferentAccessHash.new(deep_symbolize(fog_model.attributes)) end
Returns the transformed attributes from the fog model with a wrapper to allow access using either String or Symbol keys.
Source
# File lib/brightbox-cli/api.rb, line 65 def fog_model @fog_model ||= self.class.find(@id) end
Source
# File lib/brightbox-cli/api.rb, line 164 def method_missing(method_name, *args) raise NoMethodError unless fog_model fog_model.send(method_name, *args) end
Source
# File lib/brightbox-cli/api.rb, line 170 def respond_to_missing?(method_name, _) return false unless fog_model fog_model.respond_to?(method_name) end
Private Instance Methods
Source
# File lib/brightbox-cli/api.rb, line 214 def deep_symbolize(hash) return hash unless hash.respond_to?(:each_with_object) hash.each_with_object({}) do |(k, v), result| result[k.to_sym] = case v when Hash deep_symbolize(v) when Array v.map { |i| i.is_a?(Hash) ? deep_symbolize(i) : i } else v end end end
Recursively converts all keys in a hash to symbols to ensure consistent access.