Class: V1::ShortLinksController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/dynamic_links/v1/short_links_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/dynamic_links/v1/short_links_controller.rb', line 5

def create
  url = params.require(:url)
  client = DynamicLinks::Client.find_by({ api_key: params.require(:api_key) })

  unless client
    render json: { error: 'Invalid API key' }, status: :unauthorized
    return
  end

  multi_tenant(client) do
    render json: DynamicLinks.generate_short_url(url, client), status: :created
  end
rescue DynamicLinks::InvalidURIError
  render json: { error: 'Invalid URL' }, status: :bad_request
rescue => e
  DynamicLinks::Logger.log_error(e)
  render json: { error: 'An error occurred while processing your request' }, status: :internal_server_error
end

#expandObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/dynamic_links/v1/short_links_controller.rb', line 24

def expand
  api_key = params.require(:api_key)
  client = DynamicLinks::Client.find_by({ api_key: api_key })

  unless client
    render json: { error: 'Invalid API key' }, status: :unauthorized
    return
  end

  multi_tenant(client) do
    short_link = params.require(:short_url)
    full_url = DynamicLinks.resolve_short_url(short_link)

    if full_url
      render json: { full_url: full_url }, status: :ok
    else
      render json: { error: 'Short link not found' }, status: :not_found
    end
  end
rescue => e
  DynamicLinks::Logger.log_error(e)
  render json: { error: 'An error occurred while processing your request' }, status: :internal_server_error
end