module Eco module API class SessionConfig class Apis < Hash def initialize(root:) super(nil) @root = root end def apis self["apis"] ||= {} self["apis"] end def apis? apis.keys.length > 0 end def defined?(name) apis.key?(name) end def any_defined?(*names) [names].flatten.any? do |name| self.defined?(name) end end def add(name, key:, host:, version: :internal, mode: :local) apis[name] = SessionConfig::Api.new( key: key, host: host, version: version, mode: mode, root: self ) self end def active_name=(name) raise "'#{name}' Api environment not defined" if !apis[name] self["active-name"] = name self["active-api"] = apis[name] self end def active_name self["active-name"] end # you can create multiple apis under same root_name (same org) def active_root_name active_name.split("-").first end def active_api self["active-api"] end def api(logger = ::Logger.new(IO::NULL)) key = active_api.key host = active_api.host mode = active_api.mode version = active_api.version return @api if @api && key == @key && host == @host && @mode == mode && version == @version api_klass = (version == :external)? Ecoportal::API::External : Ecoportal::API::Internal @api = api_klass.new(key, host: host, logger: logger) @key = key; @host = host; @mode = mode; @version = version @api end end end end end