Skip to content

Instantly share code, notes, and snippets.

@lexrus
Created August 5, 2013 08:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexrus/6154257 to your computer and use it in GitHub Desktop.
Save lexrus/6154257 to your computer and use it in GitHub Desktop.
A tiny Sinatra app turns URL to QRCode.
require 'sinatra'
require 'pngqr'
require 'slim'
require 'stylus'
require 'stylus/tilt'
set :site_name => 'QRCode.rb'
set :server => %w[thin]
set :port => 9494
#set :dump_errors => true
get '/' do
site_name = settings.site_name
slim <<HTML
doctype html
head
title #{site_name}
styl:
body
text-align center
width 200
input
font 16px Helvetica, Arial, sans-serif
padding 0.5em
body
h1 #{site_name}
form action="encode"
input.url name="q" placeholder="URL"
input type="submit"
HTML
end
get '/encode' do
url = request['q']
if url.nil? or url.empty?
status 401
else
Pngqr.encode(url, '/tmp/qr.png')
png = File::read('/tmp/qr.png')
headers 'Content-Type' => 'image/png'
body png
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment