Class: Ec::Pg::ContextSwitcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ec/pg/middleware/context_switcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ContextSwitcher

Returns a new instance of ContextSwitcher.



4
5
6
# File 'lib/ec/pg/middleware/context_switcher.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ec/pg/middleware/context_switcher.rb', line 8

def call(env)
  request = ActionDispatch::Request.new(env)

  ignore_paths = Ec::Pg.configuration.context_switch_exclude_paths || []

  if ignore_paths.any? && request.path =~ Regexp.new("^\/(#{ignore_paths.join('|')})")
    @app.call(env)
  else
    context = get_selected_context(request)

    Ec::Pg.switch(shard: context[:shard], schema: context[:schema]) do
      @app.call(env)
    end
  end

rescue => e
  Ec::Pg.configuration.logger.error(ActiveSupport::LogSubscriber.new.send(:color, e.inspect, :red))
  [422, {"Content-Type" => 'application/json'}, [{"message": "Schema Context Not Found"}.to_json]]
end