Skip to content
Permalink
Browse files

Refactored a little bit of code and stripped some whitespace for JPEG…

…/GIF merge
  • Loading branch information...
samvincent committed Dec 4, 2012
1 parent fb5d2ca commit 0995573061ab2e159c02faa07f6d1397c66b6042
@@ -6,4 +6,5 @@ test/dummy/log/*.log
test/dummy/tmp/
Gemfile.lock
*.gem
nbproject
nbproject
.DS_Store
@@ -1,3 +1,6 @@
== 0.1.6 2012-12-04
* major enhancements
* Added support for JPEG and GIF rendering - ([@shawnpyle][])
== 0.1.5 2012-02-10
* major enhancements
* Added :level option
@@ -1,16 +1,16 @@
# Render QR codes easily from your Rails 3 application

This gem supports rendering either SVG or PNG format.
This gem supports rendering either SVG or PNG, JPEG, and GIF formats.

SVG, because of it's vector nature, will scale easily when intended for print. Offering QR endpoints enables others to integrate with your service in possibly interesting ways.

## Installation

Add the following to your +Gemfile+.
Add the following to your `Gemfile`.

gem 'rqrcode-rails3'

If you want to use the PNG format, you will have to have *ImageMagick* installed on your system.
If you want to use the PNG, JPEG or GIF format, you will have to have **ImageMagick** installed on your system.
You will also want to add the **mini_magick** gem to your application's `Gemfile`.

gem 'mini_magick'
@@ -22,9 +22,9 @@ In your controller actions, you could return a QR code that links to the current
```ruby
respond_to do |format|
format.html
format.svg { render :qrcode => request.url, :level => :l, :unit => 10 }
format.png { render :qrcode => request.url }
format.gif { render :qrcode => request.url }
format.svg { render :qrcode => request.url, :level => :l, :unit => 10 }
format.png { render :qrcode => request.url }
format.gif { render :qrcode => request.url }
format.jpeg { render :qrcode => request.url }
end
```
@@ -4,30 +4,30 @@
require 'rqrcode-rails3/renderers/svg.rb'

module RQRCode
Mime::Type.register "image/svg+xml", :svg unless Mime::Type.lookup_by_extension(:svg)
Mime::Type.register "image/png", :png unless Mime::Type.lookup_by_extension(:png)
Mime::Type.register "image/jpeg", :png unless Mime::Type.lookup_by_extension(:jpeg)
Mime::Type.register "image/gif", :png unless Mime::Type.lookup_by_extension(:gif)
Mime::Type.register "image/svg+xml", :svg unless Mime::Type.lookup_by_extension(:svg)
Mime::Type.register "image/png", :png unless Mime::Type.lookup_by_extension(:png)
Mime::Type.register "image/jpeg", :jpeg unless Mime::Type.lookup_by_extension(:jpeg)
Mime::Type.register "image/gif", :gif unless Mime::Type.lookup_by_extension(:gif)

extend SizeCalculator

ActionController::Renderers.add :qrcode do |string, options|
format = self.request.format.symbol
size = options[:size] || RQRCode.minimum_qr_size_from_string(string)
level = options[:level] || :h

qrcode = RQRCode::QRCode.new(string, :size => size, :level => level)
svg = RQRCode::Renderers::SVG::render(qrcode, options)

data = \
if format && [:png,:jpeg,:gif].include?(format)
if format && format == :svg
svg
else
image = MiniMagick::Image.read(svg) { |i| i.format "svg" }
image.format format
image.to_blob
else
svg
end

self.response_body = render_to_string(:text => data, :template => nil)
end
end
@@ -1,13 +1,17 @@
class HomeController < ApplicationController
def index
respond_to do |format|
options = {:qrcode => "http://helloworld.com", :size => 4}

format.html
format.svg { render options }
format.png { render options }
format.jpeg { render options }
format.gif { render options }
format.html
format.svg { render options }
format.png { render options }
format.jpeg { render options }
format.gif { render options }
end
end
end

private

def options
{:qrcode => "http://helloworld.com", :size => 4}
end
end
@@ -1,5 +1,5 @@
<p>Hey, you can download some cool QR Codes for this page by clicking the link below:</p>
<p><%= link_to "SVG", home_path("svg") %></p>
<p><%= link_to "PNG", home_path("png") %></p>
<p><%= link_to "SVG", home_path("svg") %></p>
<p><%= link_to "PNG", home_path("png") %></p>
<p><%= link_to "JPEG", home_path("jpeg") %></p>
<p><%= link_to "GIF", home_path("gif") %></p>
<p><%= link_to "GIF", home_path("gif") %></p>
@@ -4,36 +4,36 @@ class NavigationTest < ActiveSupport::IntegrationCase
test "truth" do
assert_kind_of Dummy::Application, Rails.application
end
test 'svg request returns a SVG file' do

test 'svg request returns an SVG file' do
visit home_path
click_link 'SVG'
assert_equal 'image/svg+xml; charset=utf-8', headers['Content-Type']
assert_equal File.read('test/support/data/qrcode.svg'), page.source
end

test 'png request returns a PNG file' do
image_test('png')
image_test('png')
end

test 'jpeg request returns a JPEG file' do
image_test('jpeg')
image_test('jpeg')
end

test 'gif request returns a GIF file' do
image_test('gif')
image_test('gif')
end

protected

def headers
page.response_headers
end
def image_test(format)

def image_test(format)
visit home_path
click_link format.upcase
assert_equal "image/#{format}; charset=utf-8", headers['Content-Type']
assert_equal "image/#{format}; charset=utf-8", headers['Content-Type']
assert_equal File.read("test/support/data/qrcode.#{format}")[0,4], page.source.force_encoding("UTF-8")[0,4]
end
end
end
@@ -1,5 +1,5 @@
# Define a bare test case to use with Capybara
class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
include Capybara::DSL
include Capybara
include Rails.application.routes.url_helpers
end

0 comments on commit 0995573

Please sign in to comment.
You can’t perform that action at this time.