module Eco module API class Session class Config class Apis module EnviroSpaces class << self def included(base) target_class = Eco::API::Session::Config::Apis msg = "To be included in #{target_class}. " msg << "Included in #{base}" raise msg unless base <= target_class super end end include Session::Config::Apis::SpaceHelpers def active_space active_api&.space || space_option end def enviros(space) apis(space).keys end # APIs of a particualr `space` def apis(space = space_option) api_space(space) end # Any APIs (of any space or in a particular `space`) def apis?(space = nil) return apis(space).any? unless space.nil? spaces.each_key.any? do |space| apis?(space) end end def api?(name, space: space_option) apis(space).key?(name) end # Deprecated def defined?(name, space: space_option) apis(space).key?(name) end def any_defined?(*names, space: space_option) [names].flatten.any? do |name| api?(name, space: space) end end # @return [Array] the spaces where the enviro `name` # has an api defined def enviro_spaces(name) spaces.keys.select do |space| api?(name, space: space) end end def enviro_spaces_str(name) return '' unless enviro_spaces?(name) ":#{enviro_spaces(name).join(', :')}" end def enviro_spaces?(name) enviro_spaces(name).any? end def missing_api_message(name, space: space_option) space ||= space_option msg = "Missing credentials for " msg << "#{full_name(name, space: space)} Api env." if enviro_spaces?(name) msg << "\n Available space(s) for '#{name}' enviro: " msg << enviro_spaces_str(name) msg << "\n" end msg end private def spaces self['spaces'] ||= {} end def api_space(space = space_option) space ||= space_option spaces[space.to_sym] ||= {} end end end end end end end