class Rack::Qrcode

Constants

VERSION

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/qrcode.rb, line 7
def initialize(app, options = {})
  @app  = app
  @path = options[:path] || '/qrcode'
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/qrcode.rb, line 12
def call(env)
  return @app.call(env) unless env['PATH_INFO'] == @path

  params = CGI.parse(env["QUERY_STRING"])
  if params['text'].empty?
    return [400, {'Content-Type' => 'text/html'}, ["Bad Request"]]
  end
  text   = params['text'].first
  size   = params['size'].empty?  ?  4 : params['size'].first.to_i
  level  = params['level'].empty? ? :h : params['level'].first.to_sym
  width  = params['width'].empty?  ? 200 : params['width'].first.to_i
  height = params['height'].empty? ? 200 : params['height'].first.to_i
  qr  = RQRCode::QRCode.new(text, size: size, level: level)
  png = qr.to_img
  body = png.resize(width, height).to_blob
  headers = {
    "Content-Length" => body.bytesize.to_s,
    "Content-Type"   => "image/png",
    "Last-Modified"  => Time.now.httpdate
  }

  [200, headers, [body]]
end