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



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

def active_api
  self["active-api"]
end

#active_nameObject



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

def active_name
  self["active-name"]
end

#active_name=(name) ⇒ Object



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

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



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

def active_root_name
  active_name
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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,
  email: nil, pass: nil, org_id: 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,
    email:    email,
    pass:     pass,
    org_id:   org_id
  )
  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



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

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



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

def default_user_key
  self["user_key"]
end

#default_user_key=(key) ⇒ Object



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

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



91
92
93
94
95
96
# File 'lib/eco/api/session/config/apis.rb', line 91

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)


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

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