Class: Motor::Configs::SyncMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/motor/configs/sync_middleware.rb

Constant Summary collapse

KeyNotSpecified =
Class.new(StandardError)
NotAuthenticated =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SyncMiddleware

Returns a new instance of SyncMiddleware.


9
10
11
# File 'lib/motor/configs/sync_middleware.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/motor/configs/sync_middleware.rb', line 13

def call(env)
  if env['PATH_INFO'] == Motor::Configs::SYNC_API_PATH
    authenticate!(env['HTTP_X_AUTHORIZATION'])

    case env['REQUEST_METHOD']
    when 'GET'
      respond_with_configs
    when 'POST'
      input = env['rack.input']
      input.rewind
      sync_configs(input.read)
    else
      @app.call(env)
    end
  else
    @app.call(env)
  end
rescue NotAuthenticated
  [403, {}, ['Invalid synchronization API key']]
rescue KeyNotSpecified
  [404, {}, ['Set `MOTOR_SYNC_API_KEY` environment variable in order to sync configs']]
end