Permalink
Browse files

hello world

  • Loading branch information...
0 parents commit 32cebb032d08c4406f6026530b7714dc30e17e09 @andrew committed Dec 31, 2011
Showing with 78 additions and 0 deletions.
  1. +4 −0 Gemfile
  2. +23 −0 Gemfile.lock
  3. +8 −0 config.ru
  4. +40 −0 server.rb
  5. +3 −0 views/index.erb
4 Gemfile
@@ -0,0 +1,4 @@
+source :rubygems
+
+gem 'sinatra'
+gem 'rqrcode_png'
23 Gemfile.lock
@@ -0,0 +1,23 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ chunky_png (1.2.5)
+ rack (1.4.0)
+ rack-protection (1.2.0)
+ rack
+ rqrcode (0.4.2)
+ rqrcode_png (0.1.1)
+ chunky_png
+ rqrcode
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rqrcode_png
+ sinatra
8 config.ru
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'bundler'
+
+Bundler.require
+
+require './server'
+
+run QR
40 server.rb
@@ -0,0 +1,40 @@
+class QR < Sinatra::Base
+ dir = File.dirname(File.expand_path(__FILE__))
+
+ helpers do
+ QR_CHAR_SIZE_VS_SIZE = [7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137, 155, 177, 194]
+
+ def minimum_qr_size_from_string(string)
+ QR_CHAR_SIZE_VS_SIZE.each_with_index do |size, index|
+ return (index + 1) if string.size < size
+ end
+
+ # If it's particularly big, we'll try and create codes until it accepts
+ i = QR_CHAR_SIZE_VS_SIZE.size
+ begin
+ i += 1
+ RQRCode::QRCode.new(string, :size => i)
+ return i
+ rescue RQRCode::QRCodeRunTimeError
+ retry
+ end
+ end
+
+ def qr_png(string)
+ size = minimum_qr_size_from_string(string)
+ qr = RQRCode::QRCode.new(string, :size => size, :level => :h )
+ png = qr.to_img
+ png.resize(200, 200)
+ end
+ end
+
+ get '/' do
+ if params[:qr]
+ png = qr_png(params[:qr])
+ content_type 'image/png', :charset => 'utf-8'
+ png.to_blob
+ else
+ erb :index
+ end
+ end
+end
3 views/index.erb
@@ -0,0 +1,3 @@
+<h1><%= request.url %>?qr=hello+world</h1>
+<img src='/?qr=hello+world'>
+<a href="http://github.com/andrew/qr"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://a248.e.akamai.net/assets.github.com/img/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub"></a>

0 comments on commit 32cebb0

Please sign in to comment.