Class: DynamicLinks::RedirectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dynamic_links/redirects_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#multi_tenant

Instance Method Details

#showObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/dynamic_links/redirects_controller.rb', line 3

def show
  client = DynamicLinks::Client.find_by({ hostname: request.host })
  unless client
    render plain: 'URL not found', status: :not_found
    return
  end

  multi_tenant(client) do
    short_url = params[:short_url]
    link = ShortenedUrl.find_by(short_url: short_url)

    if link.nil?
      if DynamicLinks.configuration.enable_fallback_mode && DynamicLinks.configuration.firebase_host.present?
        redirect_to "#{DynamicLinks.configuration.firebase_host}/#{short_url}", status: :found, allow_other_host: true
      else
        render plain: 'Not found', status: :not_found
      end
      return
    end

    raise ActiveRecord::RecordNotFound if link.expired?

    redirect_to link.url, status: :found, allow_other_host: true
  end
end