Class: AppManager::SetCookie

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

Instance Method Summary collapse

Constructor Details

#initialize(app, condition) ⇒ SetCookie

Returns a new instance of SetCookie.



8
9
10
11
# File 'lib/app_manager/set_cookie.rb', line 8

def initialize(app, condition)
  @app = app
  @condition = condition
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
35
36
37
38
39
# File 'lib/app_manager/set_cookie.rb', line 13

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]

    # Set a cookie named 'ShopCircleDiscount' with the extracted values
    lifetime = Time.now + 60 * 60 * 24 * 365
    cookie_value = discount_code

    AppManager.clear_cache #clearing cache
    # Set a 302 response with an external URL and a cookie
    return [
        302,
        {
            'Location' => 'https://admin.shopify.com/admin/apps/'+ENV['SHOPIFY_APP_SLUG']+'/home/plan' ,
            'Set-Cookie' => "ShopCircleDiscount=#{cookie_value}; expires=#{lifetime.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')}; path=/; domain=#{host}; secure; HttpOnly; SameSite=None",
            'Content-Type' => 'text/plain'
        },
        ['Redirecting to external URL with cookie']
    ]
  end

  @app.call(env)
end