Sha256: 91fdd4a7463ec01a95eb44ead5fc9451fd7b52337f468ecb267458b970472cf3

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-0.6.2 lib/eco/api/session_config/apis.rb
eco-helpers-0.6.1 lib/eco/api/session_config/apis.rb
eco-helpers-0.6.0 lib/eco/api/session_config/apis.rb