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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/short_rails/short_urls_controller.rb', line 24

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)
  short_url = ShortUrl.new(:short => short, :url => params[:base_url], :url_short_param => params[:base_url_short_param], :params => url_params)
  if short_url.save
    # return short id
    render :text => short
  else
    head :no_content
  end
end

#parametersObject

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



41
42
43
44
45
# File 'app/controllers/short_rails/short_urls_controller.rb', line 41

def parameters
  short_url = ShortUrl.find_by_short(params[:short])

  render :text => short_url.params.to_json
end

#qrcodeObject

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



49
50
51
52
53
54
55
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
# File 'app/controllers/short_rails/short_urls_controller.rb', line 49

def qrcode
  require 'rqrcode_png'

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

  width = params[:width].to_i
  height = params[:height].to_i
  if width > 2048 || height > 2048
    render :text => "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 :text => "ERROR: Could not generate QR code", :status => :bad_request
    return
  end
  qr_image = qr.to_img

  qr_size = 17 + 4 * version
  # default size is QR code pixels with a border of 1/10 that size
  img_size =(qr_size + qr_size/10 * 2)
  # clamp to min default size
  width = [width, img_size].max
  height = [height, img_size].max

  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_string, :type => 'image/png', :disposition => 'inline', :filename => "#{params[:short]}.png"
end

#showObject

GET ‘:id’ redirect to base URL



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

def show
  short_url = ShortUrl.find_by_short(params[:short])

  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