Module: QrcodeHelper

Defined in:
app/helpers/qrcode_helper.rb

Instance Method Summary collapse

Instance Method Details

#qrcode(data = '', options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/qrcode_helper.rb', line 2

def qrcode(data = '', options = {})
  # Google API: https://google-developers.appspot.com/chart/infographics/docs/qr_codes
  width                  = options.delete(:width) { '200' }
  output_encoding        = options.delete(:output_encoding)
  error_correction_level = options.delete(:error_correction_level)
  margin                 = options.delete(:margin)

  qrcode_url = 'https://chart.googleapis.com/chart?cht=qr'
  qrcode_url += "&chl=#{data}"
  qrcode_url += "&chs=#{width}x#{width}"
  qrcode_url += "&choe=#{output_encoding}" if output_encoding
  if error_correction_level && margin
    qrcode_url += "&chld=#{error_correction_level}|#{margin}"
  else
    qrcode_url += "&chld=#{error_correction_level}" if error_correction_level
    qrcode_url += "&chld=|#{margin}" if margin
  end
  image_tag(qrcode_url, alt: data)
end