Class: ShortRails::ShortUrlsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/short_rails/short_urls_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST ‘/’ create short URL



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/short_rails/short_urls_controller.rb', line 27

def create
  begin
    short = SecureRandom.base64(6).tr("+/=", "xyz").downcase
  end while ShortUrl.where(short: short).exists?

  url_params = params.except(:action, :controller, :base_url, :base_url_short_param).permit!.to_hash
  short_url = ShortUrl.new(short: short, url: params[:base_url], params: url_params, url_short_param: params[:base_url_short_param])
  if short_url.save
    # return short id
    render plain: short
  else
    head :bad_request
  end
end

#parametersObject

GET ‘:id/params’ get parameters for full URL



44
45
46
47
48
49
50
51
52
# File 'app/controllers/short_rails/short_urls_controller.rb', line 44

def parameters
  short_url = ShortUrl.find_by_short(params[:short])
  if short_url.nil?
    head :not_found
    return
  end

  render json: short_url.params
end

#qrcodeObject

GET ‘:id/qrcode’ get QR code containing this short URL



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/short_rails/short_urls_controller.rb', line 56

def qrcode
  require 'rqrcode'

  if ShortUrl.find_by_short(params[:short]).nil?
    render plain: "ERROR: Could not generate QR code", status: :not_found
    return
  end

  width = params[:width].to_i
  height = params[:height].to_i
  if width > 2048 || height > 2048
    render plain: "ERROR: Requested image size is too large", status: :bad_request
    return
  end

  # create QR code with minimal version
  qr = nil
  version = 3
  while qr.nil? && version < 40
    begin
      qr = RQRCode::QRCode.new(short_url_url, size: version, level: :h)
    rescue
      # current version has not enough capacity, try next version
      version += 1
    end
  end
  if qr.nil?
    render plain: "ERROR: Could not generate QR code", status: :bad_request
    return
  end

  qr_size = 17 + 4 * version
  # min size is QR code pixels with a border of 4 QR code pixels
  border_modules = 4
  min_size = (qr_size + border_modules * 2)
  if params[:width].nil? && params[:height].nil?
    # default size with 3 image pixels per QR code pixel
    img_size = 3 * min_size
    width = img_size
    height = img_size
  else
    # clamp to min default size
    width = [width, min_size].max
    height = [height, min_size].max
    img_size = [width, height].min
  end

  qr_image = qr.as_png(
    size: img_size,
    border_modules: border_modules
  )

  if width > img_size || height > img_size
    # calculate image size to fit scaled QR code to requested size
    size = [width, height].min
    img_size = (size / img_size) * img_size
    qr_image = qr_image.resize(img_size, img_size)

    if width > img_size || height > img_size
      # center QR code in image of requested size
      image = ::ChunkyPNG::Image.new(width, height, ::ChunkyPNG::Color::WHITE)
      offset_x = (width - img_size) / 2
      offset_y = (height - img_size) / 2
      qr_image = image.replace(qr_image, offset_x, offset_y)
    end
  end

  send_data qr_image.to_s, type: 'image/png', disposition: 'inline', filename: "#{params[:short]}.png"
end

#showObject

GET ‘:id’ redirect to base URL



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/short_rails/short_urls_controller.rb', line 7

def show
  short_url = ShortUrl.find_by_short(params[:short])
  if short_url.nil?
    head :not_found
    return
  end

  if short_url.url_short_param.nil?
    # redirect to full URL
    query_params = short_url.params
  else
    # redirect to base URL with short param
    query_params = {short_url.url_short_param => short_url.short}
  end

  redirect_to "#{short_url.url}?#{query_params.to_query}"
end