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



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

def active_api
  self["active-api"]
end

#active_nameObject



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

def active_name
  self["active-name"]
end

#active_name=(name) ⇒ Object



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

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



59
60
61
# File 'lib/eco/api/session/config/apis.rb', line 59

def active_root_name
  active_name
end

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



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

def add(name, key:, host:, version: :internal, mode: :local, user_key: nil, external_key: nil)
  apis[name] = Session::Config::Api.new(
    name,
    key:      key,
    host:     host,
    version:  version,
    mode:     mode,
    root:     self,
    user_key: user_key,
    external_key: external_key
  )
  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), version: nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/eco/api/session/config/apis.rb', line 70

def api(logger = ::Logger.new(IO::NULL), version: nil)
  unless active_api
    raise "There's no 'active_api'. Use apis.active_name='api_enviro_name' to set the active api"
  end
  active_api.api(version: version, logger: logger)
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

#default_user_keyObject



81
82
83
# File 'lib/eco/api/session/config/apis.rb', line 81

def default_user_key
  self["user_key"]
end

#default_user_key=(key) ⇒ Object



77
78
79
# File 'lib/eco/api/session/config/apis.rb', line 77

def default_user_key=(key)
  self["user_key"] = key
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

#one_offObject

Method to support CLI one-off API requests



86
87
88
89
90
91
# File 'lib/eco/api/session/config/apis.rb', line 86

def one_off
  if one_off?
    add(one_off_org, key: one_off_key, host: "#{one_off_enviro}.ecoportal.com")
    return one_off_org
  end
end

#service_up?Boolean

Returns:

  • (Boolean)


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

def service_up?
  @api_test ||= Session::Config::Api.api_class(active_api.version).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