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

Methods inherited from Hash

#deep_merge, #deep_merge!

Constructor Details

#initialize(name, root:, key:, host:, version:, space: :default, mode: :local, user_key: nil, external_key: nil, email: nil, pass: nil, org_id: nil) ⇒ Api

Returns a new instance of Api.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/eco/api/session/config/api.rb', line 59

def initialize(
  name,
  root:,
  key:,
  host:,
  version:,
  space:        :default,
  mode:         :local,
  user_key:     nil,
  external_key: nil,
  email:        nil,
  pass:         nil,
  org_id:       nil
)
  super(nil)
  @root                = root
  @apis                = {}
  self['name']         = name
  self['space']        = to_space(space)
  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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eco/api/session/config/api.rb', line 41

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

.description(api) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/eco/api/session/config/api.rb', line 14

def description(api)
  msg = "Expecting #{self}. Given: #{api.class}"
  raise ArgumentError, msg unless api.is_a?(self)

  full_name(api.name, space: api.space)
end

.full_name(name, space: :default) ⇒ Object



21
22
23
24
25
26
# File 'lib/eco/api/session/config/api.rb', line 21

def full_name(name, space: :default)
  space = to_space(space)

  str_space = space == :default ? '' : " (space: :#{space})"
  "'#{name}'#{str_space}"
end

.to_space(value) ⇒ Object



7
8
9
10
11
12
# File 'lib/eco/api/session/config/api.rb', line 7

def to_space(value)
  value = value.to_s.strip.gsub(/[- ]/, '_').downcase
  return :default if value.empty?

  value.to_sym
end

.to_version(str) ⇒ Object



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

def to_version(str)
  case str.to_sym
  when :external, :v1
    :v1
  when :v2, :oozes
    :v2
  when :graphql
    :graphql
  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:

  • (Ecoportal::API::Internal, Ecoportal::API::V2, Ecoportal::API::V1)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/eco/api/session/config/api.rb', line 98

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

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

  unless api_params?(version)
    msg  = "The api configuration for #{description} "
    msg << "is missing data for the api version '#{self.version(version)}'"
    raise ArgumentError, msg
  end

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

#configEco::API::Session::Config

Returns the root config.

Returns:



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

def config
  @root.config
end

#descriptionObject



134
135
136
# File 'lib/eco/api/session/config/api.rb', line 134

def description
  self.class.description(self)
end

#emailObject



163
164
165
# File 'lib/eco/api/session/config/api.rb', line 163

def email
  self['email'] || @root.default_email
end

#external_keyObject



151
152
153
# File 'lib/eco/api/session/config/api.rb', line 151

def external_key
  self['external_key'] || (%i[v1 v2].include?(version) && key)
end

#get(version) ⇒ Object



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

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

#hostObject



171
172
173
# File 'lib/eco/api/session/config/api.rb', line 171

def host
  self['host']
end

#internal_keyObject



155
156
157
# File 'lib/eco/api/session/config/api.rb', line 155

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

#keyObject



143
144
145
# File 'lib/eco/api/session/config/api.rb', line 143

def key
  self['key']
end

#local?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/eco/api/session/config/api.rb', line 185

def local?
  mode == :local
end

#loggerObject

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



203
204
205
206
# File 'lib/eco/api/session/config/api.rb', line 203

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



181
182
183
# File 'lib/eco/api/session/config/api.rb', line 181

def mode
  self['mode']
end

#mode=(mode) ⇒ Object

Parameters:

  • mode (Symbol)

    to define if running on :remote or :local



176
177
178
# File 'lib/eco/api/session/config/api.rb', line 176

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

#nameObject



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

def name
  self['name']
end

#one_off?Boolean

@todo: deletage to apis.one_off?

Returns:

  • (Boolean)


139
140
141
# File 'lib/eco/api/session/config/api.rb', line 139

def one_off?
  name.is_a?(Symbol)
end

#org_idObject



159
160
161
# File 'lib/eco/api/session/config/api.rb', line 159

def org_id
  self['org_id']
end

#passObject



167
168
169
# File 'lib/eco/api/session/config/api.rb', line 167

def pass
  self['pass'] || @root.default_pass
end

#remote?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/eco/api/session/config/api.rb', line 189

def remote?
  !local?
end

#set(version, api) ⇒ Object



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

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

#spaceObject



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

def space
  self['space'] ||= :default
  to_space(self['space'])
end

#user_keyObject



147
148
149
# File 'lib/eco/api/session/config/api.rb', line 147

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

#version(value = nil) ⇒ Object



198
199
200
# File 'lib/eco/api/session/config/api.rb', line 198

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

#version_available?(version) ⇒ Boolean

Returns whether or not version is available.

Returns:

  • (Boolean)

    whether or not version is available



194
195
196
# File 'lib/eco/api/session/config/api.rb', line 194

def version_available?(version)
  api_params?(version)
end