Class: Eco::API::Session::Config::Api

Inherits:
Hash
  • Object
show all
Defined in:
lib/eco/api/session/config/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge, #deep_merge!

Constructor Details

#initialize(name, key:, host:, version:, mode: :local, root:, user_key: nil, external_key: nil) ⇒ Api

Returns a new instance of Api.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eco/api/session/config/api.rb', line 34

def initialize(name, key:, host:, version:, mode: :local, root:, user_key: nil, external_key: nil)
  super(nil)
  @root = root
  @apis = {}
  self["name"]         = name
  self["key"]          = key
  self["host"]         = host
  self["version"]      = version
  self["mode"]         = mode
  self["user_key"]     = user_key
  self["external_key"] = external_key
end

Class Method Details

.api_class(version = :v0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eco/api/session/config/api.rb', line 20

def api_class(version = :v0)
  case to_version(version)
  when :v0
    Ecoportal::API::Internal
  when :v1
    Ecoportal::API::V1
  when :v2
    Ecoportal::API::V2
  else
  end
end

.to_version(str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/eco/api/session/config/api.rb', line 9

def to_version(str)
  case str.to_sym
  when :external, :v1
    :v1
  when :v2, :oozes
    :v2
  else # :internal, :v0
    :v0
  end
end

Instance Method Details

#api(version: nil, logger: nil) ⇒ Ecoportal::API::Internal, ...

Obtain an API object of a specific version.

Parameters:

  • version (Symbol) (defaults to: nil)

    any of the available API versions [:v0, :v1, :v2].

  • logger (Logger) (defaults to: nil)

    the logger that will be used with this api instance.

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/eco/api/session/config/api.rb', line 56

def api(version: nil, logger: nil)
  version       = version ? version : self.version
  switch_logger = (logger != @logger)
  @logger       = logger if logger

  if (current = get(version)) && !switch_logger
    return current
  end

  unless api_params?(version)
    raise "The api configuration for '#{name}' is missing data for the api version '#{self.version(version)}'"
  end

  new_api(version).tap do |pi|
    set(version, pi)
  end
end

#configEco::API::Session::Config

Returns the root config.

Returns:



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

def config
  @root.config
end

#external_keyObject



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

def external_key
  self["external_key"] || ([:v1, :v2].include?(version) && key)
end

#get(version) ⇒ Object



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

def get(version)
  @apis[self.version(version)]
end

#hostObject



102
103
104
# File 'lib/eco/api/session/config/api.rb', line 102

def host
  self["host"]
end

#internal_keyObject



98
99
100
# File 'lib/eco/api/session/config/api.rb', line 98

def internal_key
  (version == :v0) && self["key"]
end

#keyObject



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

def key
  self["key"]
end

#local?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/eco/api/session/config/api.rb', line 116

def local?
  mode == :local
end

#loggerObject

if no low level connection messages: use IO::NULL



129
130
131
132
# File 'lib/eco/api/session/config/api.rb', line 129

def logger
  @logger ||= ::Logger.new(IO::NULL)
  log_connection? ? @logger : ::Logger.new(IO::NULL)
end

#modeSymbol

Returns if running on :remote or :local.

Returns:

  • (Symbol)

    if running on :remote or :local



112
113
114
# File 'lib/eco/api/session/config/api.rb', line 112

def mode
  self["mode"]
end

#mode=(mode) ⇒ Object

Parameters:

  • mode (Symbol)

    to define if running on :remote or :local



107
108
109
# File 'lib/eco/api/session/config/api.rb', line 107

def mode=(mode)
  self["mode"] = (mode == :remote)? :remote : :local
end

#nameObject



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

def name
  self["name"]
end

#remote?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/eco/api/session/config/api.rb', line 120

def remote?
  !local?
end

#set(version, api) ⇒ Object



74
75
76
# File 'lib/eco/api/session/config/api.rb', line 74

def set(version, api)
  @apis[self.version(version)] = api
end

#user_keyObject



90
91
92
# File 'lib/eco/api/session/config/api.rb', line 90

def user_key
  self["user_key"] || @root.default_user_key
end

#version(value = nil) ⇒ Object



124
125
126
# File 'lib/eco/api/session/config/api.rb', line 124

def version(value = nil)
  self.class.to_version(value || self["version"])
end