Class: Eco::API::Session::Config::Api
- Inherits:
-
Hash
- Object
- Hash
- Eco::API::Session::Config::Api
show all
- Defined in:
- lib/eco/api/session/config/api.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#api(version: nil, logger: nil) ⇒ Ecoportal::API::Internal, ...
Obtain an API
object of a specific version
.
-
#config ⇒ Eco::API::Session::Config
-
#external_key ⇒ Object
-
#get(version) ⇒ Object
-
#host ⇒ Object
-
#initialize(name, key:, host:, version:, mode: :local, root:, user_key: nil, external_key: nil) ⇒ Api
constructor
-
#internal_key ⇒ Object
-
#key ⇒ Object
-
#local? ⇒ Boolean
-
#logger ⇒ Object
if no low level connection messages: use IO::NULL
.
-
#mode ⇒ Symbol
If running on :remote
or :local
.
-
#mode=(mode) ⇒ Object
-
#name ⇒ Object
-
#remote? ⇒ Boolean
-
#set(version, api) ⇒ Object
-
#user_key ⇒ Object
-
#version(value = nil) ⇒ Object
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 :v0
end
end
|
Instance Method Details
Obtain an API
object of a specific version
.
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
|
48
49
50
|
# File 'lib/eco/api/session/config/api.rb', line 48
def config
@root.config
end
|
#external_key ⇒ Object
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
|
#host ⇒ Object
102
103
104
|
# File 'lib/eco/api/session/config/api.rb', line 102
def host
self["host"]
end
|
#internal_key ⇒ Object
98
99
100
|
# File 'lib/eco/api/session/config/api.rb', line 98
def internal_key
(version == :v0) && self["key"]
end
|
#key ⇒ Object
86
87
88
|
# File 'lib/eco/api/session/config/api.rb', line 86
def key
self["key"]
end
|
#local? ⇒ Boolean
116
117
118
|
# File 'lib/eco/api/session/config/api.rb', line 116
def local?
mode == :local
end
|
#logger ⇒ Object
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
|
#mode ⇒ Symbol
Returns 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
107
108
109
|
# File 'lib/eco/api/session/config/api.rb', line 107
def mode=(mode)
self["mode"] = (mode == :remote)? :remote : :local
end
|
#name ⇒ Object
82
83
84
|
# File 'lib/eco/api/session/config/api.rb', line 82
def name
self["name"]
end
|
#remote? ⇒ 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_key ⇒ Object
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
|