Class: Eco::API::Session::Config::Api
- Inherits:
-
Hash
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
-
#email ⇒ Object
-
#external_key ⇒ Object
-
#get(version) ⇒ Object
-
#host ⇒ Object
-
#initialize(name, root:, key:, host:, version:, mode: :local, user_key: nil, external_key: nil, email: nil, pass: nil, org_id: 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
-
#one_off? ⇒ Boolean
-
#org_id ⇒ Object
-
#pass ⇒ Object
-
#remote? ⇒ Boolean
-
#set(version, api) ⇒ Object
-
#user_key ⇒ Object
-
#version(value = nil) ⇒ Object
-
#version_available?(version) ⇒ Boolean
Whether or not version
is available.
Methods inherited from Hash
#deep_merge, #deep_merge!
Constructor Details
#initialize(name, root:, key:, host:, version:, mode: :local, user_key: nil, external_key: nil, email: nil, pass: nil, org_id: nil) ⇒ Api
Returns a new instance of Api.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/eco/api/session/config/api.rb', line 38
def initialize(name, root:, key:, host:, version:,
mode: :local, user_key: nil, external_key: nil,
email: nil, pass: nil, org_id: 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
self["email"] = email || ENV['USER_EMAIL']
self["pass"] = pass || ENV['USER_PASS']
self["org_id"] = org_id
end
|
Class Method Details
.api_class(version = :v0) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# 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
require 'ecoportal/api-v2'
Ecoportal::API::V2
when :graphql
require 'ecoportal/api-graphql'
Ecoportal::API::GraphQL
else
puts "Unknown api version '#{version}'"
end
end
|
.to_version(str) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/eco/api/session/config/api.rb', line 7
def to_version(str)
case str.to_sym
when :external, :v1
:v1
when :v2, :oozes
:v2
when :graphql
:graphql
else :v0
end
end
|
Instance Method Details
#api(version: nil, logger: nil) ⇒ Ecoportal::API::Internal, ...
Obtain an API
object of a specific version
.
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/eco/api/session/config/api.rb', line 65
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
|
57
58
59
|
# File 'lib/eco/api/session/config/api.rb', line 57
def config
@root.config
end
|
119
120
121
|
# File 'lib/eco/api/session/config/api.rb', line 119
def email
self["email"] || @root.default_email
end
|
#external_key ⇒ Object
107
108
109
|
# File 'lib/eco/api/session/config/api.rb', line 107
def external_key
self["external_key"] || (%i[v1 v2].include?(version) && key)
end
|
#get(version) ⇒ Object
87
88
89
|
# File 'lib/eco/api/session/config/api.rb', line 87
def get(version)
@apis[self.version(version)]
end
|
127
128
129
|
# File 'lib/eco/api/session/config/api.rb', line 127
def host
self["host"]
end
|
#internal_key ⇒ Object
111
112
113
|
# File 'lib/eco/api/session/config/api.rb', line 111
def internal_key
(version == :v0) && self["key"]
end
|
99
100
101
|
# File 'lib/eco/api/session/config/api.rb', line 99
def key
self["key"]
end
|
#local? ⇒ Boolean
141
142
143
|
# File 'lib/eco/api/session/config/api.rb', line 141
def local?
mode == :local
end
|
if no low level connection messages: use IO::NULL
159
160
161
162
|
# File 'lib/eco/api/session/config/api.rb', line 159
def logger
@logger ||= ::Logger.new(IO::NULL)
log_connection? ? @logger : ::Logger.new(IO::NULL)
end
|
#mode ⇒ Symbol
Returns if running on :remote
or :local
.
137
138
139
|
# File 'lib/eco/api/session/config/api.rb', line 137
def mode
self["mode"]
end
|
#mode=(mode) ⇒ Object
132
133
134
|
# File 'lib/eco/api/session/config/api.rb', line 132
def mode=(mode)
self["mode"] = mode == :remote ? :remote : :local
end
|
91
92
93
|
# File 'lib/eco/api/session/config/api.rb', line 91
def name
self["name"]
end
|
#one_off? ⇒ Boolean
95
96
97
|
# File 'lib/eco/api/session/config/api.rb', line 95
def one_off?
name.is_a?(Symbol)
end
|
115
116
117
|
# File 'lib/eco/api/session/config/api.rb', line 115
def org_id
self["org_id"]
end
|
123
124
125
|
# File 'lib/eco/api/session/config/api.rb', line 123
def pass
self["pass"] || @root.default_pass
end
|
#remote? ⇒ Boolean
145
146
147
|
# File 'lib/eco/api/session/config/api.rb', line 145
def remote?
!local?
end
|
#set(version, api) ⇒ Object
83
84
85
|
# File 'lib/eco/api/session/config/api.rb', line 83
def set(version, api)
@apis[self.version(version)] = api
end
|
103
104
105
|
# File 'lib/eco/api/session/config/api.rb', line 103
def user_key
self["user_key"] || @root.default_user_key
end
|
#version(value = nil) ⇒ Object
154
155
156
|
# File 'lib/eco/api/session/config/api.rb', line 154
def version(value = nil)
self.class.to_version(value || self["version"])
end
|
#version_available?(version) ⇒ Boolean
Returns whether or not version
is available.
150
151
152
|
# File 'lib/eco/api/session/config/api.rb', line 150
def version_available?(version)
api_params?(version)
end
|