Class: AppManager::SetLocalStorage

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

Instance Method Summary collapse

Constructor Details

#initialize(app, condition, destination_url) ⇒ SetLocalStorage

Returns a new instance of SetLocalStorage.



6
7
8
9
10
# File 'lib/app_manager/set_local_storage.rb', line 6

def initialize(app, condition,destination_url)
  @app = app
  @condition = condition
  @destination_url = destination_url
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/app_manager/set_local_storage.rb', line 12

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

  if @condition.call(request)
    url = URI.parse(request.url)
    host = url.host
    discount_code = request.path.split('/')[2] rescue ''
    AppManager.clear_cache #clearing cache
    # Set a 302 response with an external URL and a cookie
    return [
        302,
        {
            'Location' => "#{@destination_url}?discount_code=#{discount_code}" ,
            'Content-Type' => 'text/plain'
        },
        ['Redirecting to external URL with cookie']
    ]
  end

  @app.call(env)
end