Class: IceCubeSelectMiddleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ IceCubeSelectMiddleware

Returns a new instance of IceCubeSelectMiddleware.



4
5
6
# File 'lib/middleware/ice_cube_select_middleware.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
# File 'lib/middleware/ice_cube_select_middleware.rb', line 8

def call(env)
  regexp = /^\/ice_cube_select\/translate\/(.*)/
  if env["PATH_INFO"]&.match?(regexp)
    I18n.locale = env["PATH_INFO"].scan(regexp).first.first
    request = Rack::Request.new(env)
    params = request.params.to_h.transform_keys(&:to_sym)

    if params && params[:rule_type]
      rule = IceCubeSelect.dirty_hash_to_rule(params)
      [200, {"content-type" => "text/html"}, [rule.to_s]]
    else
      [200, {"content-type" => "text/html"}, [""]]
    end
  else
    @app.call(env)
  end
end