Class: Maestrano::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/maestrano.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/maestrano.rb', line 133

def initialize
  @environment = 'production'

  # App config
  @app = OpenStruct.new({
    host: 'http://localhost:3000'
  })

  # API Config
  @api = OpenStruct.new({
    id: nil,
    key: nil,
    token: nil,
    version: nil,
    verify_ssl_certs: false,
    lang: nil, # set in post_initialize
    lang_version: nil # set in post_initialize
  })

  # SSO Config
  @sso = OpenStruct.new({
    enabled: true,
    slo_enabled: true,
    creation_mode: 'real',
    init_path: '/maestrano/auth/saml/init',
    consume_path: '/maestrano/auth/saml/consume',
    name_id_format: nil, # set in post_initialize
    idm: nil
  })

  # WebHooks Config
  @webhook = OpenStruct.new({
    account: OpenStruct.new({
      groups_path: '/maestrano/account/groups/:id',
      group_users_path: '/maestrano/account/groups/:group_id/users/:id',
    }),
    connec: OpenStruct.new({
      notifications_path: '/maestrano/connec/notifications',
      subscriptions: {}
    })
  })

  # Connec! Config
  @connec = OpenStruct.new({
    enabled: true
  })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Handle legacy parameter assignment



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/maestrano.rb', line 220

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^((?:sso|app|api|user)_.*)=$/
    new_meth = self.legacy_param_to_new($1) + '='
    props = new_meth.split('.')
    last_prop = props.pop
    obj = props.inject(self,:send)
    obj.send(last_prop, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



131
132
133
# File 'lib/maestrano.rb', line 131

def api
  @api
end

#appObject

Returns the value of attribute app.



131
132
133
# File 'lib/maestrano.rb', line 131

def app
  @app
end

#connecObject

Returns the value of attribute connec.



131
132
133
# File 'lib/maestrano.rb', line 131

def connec
  @connec
end

#environmentObject

Returns the value of attribute environment.



131
132
133
# File 'lib/maestrano.rb', line 131

def environment
  @environment
end

#ssoObject

Returns the value of attribute sso.



131
132
133
# File 'lib/maestrano.rb', line 131

def sso
  @sso
end

#webhookObject

Returns the value of attribute webhook.



131
132
133
# File 'lib/maestrano.rb', line 131

def webhook
  @webhook
end

Instance Method Details

#legacy_param_to_new(parameter) ⇒ Object

Transform legacy parameters into new parameter style Dummy mapping



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/maestrano.rb', line 198

def legacy_param_to_new(parameter)
  case parameter.to_s
  when 'user_creation_mode'
    return 'sso.creation_mode'
  when 'verify_ssl_certs'
    return 'api.verify_ssl_certs'
  when 'app_id'
    return 'api.id'
  when /^app_(.*)/i
    return "app.#{$1}"
  when /^api_(.*)/i
    return "api.#{$1}"
  when /^sso_app_(.*)/i
    return "sso.#{$1}"
  when /^sso_(.*)/i
    return "sso.#{$1}"
  else
    return parameter.to_s
  end
end

#param(parameter) ⇒ Object

Get configuration parameter value



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/maestrano.rb', line 233

def param(parameter)
  real_param = self.legacy_param_to_new(parameter)
  props = real_param.split('.')

  # Either respond to param directly or via properties chaining (e.g: webhook.account.groups_path)
  if self.respond_to?(real_param) || props.inject(self) { |result,elem| result && result.respond_to?(elem) ? result.send(elem) || elem : false }
    last_prop = props.pop
    obj = props.inject(self,:send)
    obj.send(last_prop)
  elsif EVT_CONFIG.has_key?(@environment.to_s) && EVT_CONFIG[@environment.to_s].has_key?(real_param.to_s)
    EVT_CONFIG[@environment.to_s][real_param.to_s]
  else
    raise ArgumentError, "No such configuration parameter: '#{parameter}'"
  end
end

#post_initializeObject

Force or default certain parameters Used after configure block



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/maestrano.rb', line 183

def post_initialize
  self.api.token = "#{self.api.id}:#{self.api.key}"
  self.api.version = Maestrano::VERSION
  self.api.lang = 'ruby'
  self.api.lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
  self.sso.idm ||= self.app.host
  self.sso.name_id_format ||= Maestrano::Saml::Settings::NAMEID_PERSISTENT
  self.sso.slo_enabled &&= self.sso.enabled
  self.connec.base_path ||= '/api/v2'
  self.connec.timeout ||= 60
end