Class: Eco::API::Session::Config::Apis

Inherits:
BaseConfig show all
Defined in:
lib/eco/api/session/config/apis.rb

Instance Attribute Summary

Attributes inherited from BaseConfig

#config

Instance Method Summary collapse

Methods inherited from BaseConfig

#clone, #initialize

Methods inherited from Hash

#deep_merge, #deep_merge!

Constructor Details

This class inherits a constructor from Eco::API::Session::Config::BaseConfig

Instance Method Details

#active_apiObject



57
58
59
# File 'lib/eco/api/session/config/apis.rb', line 57

def active_api
  self["active-api"]
end

#active_nameObject



47
48
49
# File 'lib/eco/api/session/config/apis.rb', line 47

def active_name
  self["active-name"]
end

#active_name=(name) ⇒ Object



40
41
42
43
44
45
# File 'lib/eco/api/session/config/apis.rb', line 40

def active_name=(name)
  raise "'#{name}' Api environment not defined" if !apis[name]
  self["active-name"] = name
  self["active-api"]  = apis[name]
  self
end

#active_root_nameObject

the active environment



52
53
54
55
# File 'lib/eco/api/session/config/apis.rb', line 52

def active_root_name
  active_name
  #active_name.split("-").first

end

#add(name, key:, host:, version: :internal, mode: :local) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/eco/api/session/config/apis.rb', line 29

def add(name, key:, host:, version: :internal, mode: :local)
  apis[name] = Session::Config::Api.new(
    key: key,
    host: host,
    version: version,
    mode: mode,
    root: self
  )
  self
end

#any_defined?(*names) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/eco/api/session/config/apis.rb', line 23

def any_defined?(*names)
  [names].flatten.any? do |name|
    self.defined?(name)
  end
end

#api(logger = ::Logger.new(IO::NULL), force_new: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/eco/api/session/config/apis.rb', line 69

def api(logger = ::Logger.new(IO::NULL), force_new: false)
  key     = active_api.key
  host    = active_api.host
  mode    = active_api.mode
  version = active_api.version
  return @api if (!force_new) && @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

#apisObject



7
8
9
# File 'lib/eco/api/session/config/apis.rb', line 7

def apis
  self["apis"] ||= {}
end

#apis?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/eco/api/session/config/apis.rb', line 11

def apis?
  apis.keys.length > 0
end

#defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/eco/api/session/config/apis.rb', line 19

def defined?(name)
  apis.key?(name)
end

#envirosObject



15
16
17
# File 'lib/eco/api/session/config/apis.rb', line 15

def enviros
  apis.keys
end

#new_api(logger = ::Logger.new(IO::NULL)) ⇒ Object



83
84
85
# File 'lib/eco/api/session/config/apis.rb', line 83

def new_api(logger = ::Logger.new(IO::NULL))
  api(logger, force_new: true)
end

#service_up?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'lib/eco/api/session/config/apis.rb', line 61

def service_up?
  api_klass = (active_api.version == :external)? Ecoportal::API::External : Ecoportal::API::Internal
  @api_test ||= api_klass.new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL))
  status = @api_test.client.get("/policy_groups").status
  # 401 Unauthorized "Permission denied. API key may be invalid."

  status == 401
end