Class: Rack::Qrcode

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/qrcode.rb,
lib/rack/qrcode/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Qrcode

Returns a new instance of Qrcode.



7
8
9
10
# File 'lib/rack/qrcode.rb', line 7

def initialize(app, options = {})
  @app  = app
  @path = options[:path] || '/qrcode'
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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"   => "imgae/png",
    "Last-Modified"  => Time.now.httpdate
  }

  [200, headers, [body]]
end