prawn-qr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prawn-qr.gemspec
4
+ gemspec
5
+
6
+ gem 'guard'
7
+ gem 'guard-minitest'
8
+ gem 'growl'
9
+ gem 'qrio', git: 'https://github.com/rubysolo/qrio.git'
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^spec/(.*)_spec\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
4
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Josep Jaume
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # prawn-qr
2
+
3
+ Prawn-qr is a prawn extension for creating QR Codes inside of a prawn document thanks to [rqrcode](https://github.com/whomwah/rqrcode).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'prawn-qr'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ ```Ruby
18
+ require 'prawn-qr'
19
+
20
+ Prawn::Document.generate "test.pdf" do
21
+ qrcode "http://codegram.com"
22
+ end
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "spec/**/*_spec.rb"
7
+ end
8
+
9
+ task default: [:test]
@@ -0,0 +1,23 @@
1
+ require "prawn-qr/version"
2
+ require "prawn-qr/qrcode"
3
+ require 'prawn'
4
+
5
+ module Prawn
6
+ module Qr
7
+ # Public: Generates a QR code centered on the inside of the current
8
+ # boundaries.
9
+ #
10
+ # content - The text content of the QR to be generated
11
+ #
12
+ # Examples
13
+ #
14
+ # Prawn::Document.generate "test.pdf" do
15
+ # qrcode "http://codegram.com"
16
+ # end
17
+ def qrcode(content)
18
+ QRCode.new(self, content).draw
19
+ end
20
+ end
21
+ end
22
+
23
+ Prawn::Document.extensions << Prawn::Qr
@@ -0,0 +1,72 @@
1
+ require 'rqrcode'
2
+
3
+ module Prawn
4
+ module Qr
5
+ class QRCode
6
+ attr_accessor :document, :content
7
+
8
+ def initialize(document, content)
9
+ @document = document
10
+ @content = content
11
+ end
12
+
13
+ def qrcode
14
+ return @qrcode if @qrcode
15
+ size = 1
16
+ while(!@qrcode)
17
+ begin
18
+ @qrcode = RQRCode::QRCode.new(content, size: size)
19
+ rescue RQRCode::QRCodeRunTimeError
20
+ size += 1
21
+ end
22
+ end
23
+ @qrcode = @qrcode.modules
24
+ end
25
+
26
+ def draw
27
+ qrcode.reverse.each_with_index do |row, y|
28
+ y += 1
29
+ row.each_with_index do |dark, x|
30
+ document.fill_color = (dark ? '000000' : 'ffffff')
31
+ document.fill_rectangle(
32
+ [x * cell_size + horizontal_offset, y * cell_size + vertical_offset],
33
+ cell_size, cell_size
34
+ )
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def cell_size
42
+ @cell_size ||= [cell_height, cell_width].min
43
+ end
44
+
45
+ def cell_height
46
+ return @cell_height if @cell_height
47
+ qr = qrcode
48
+ height = qr.length
49
+ @cell_height = document.bounds.height.to_f / height.to_f
50
+ end
51
+
52
+ def cell_width
53
+ return @cell_width if @cell_width
54
+ qr = qrcode
55
+ width = qr.first.length
56
+ @cell_width = document.bounds.width.to_f / width.to_f
57
+ end
58
+
59
+ def vertical_offset
60
+ @vertical_offset ||= (
61
+ document.bounds.height - qrcode.length * cell_size
62
+ ).to_f / 2
63
+ end
64
+
65
+ def horizontal_offset
66
+ @horizontal_offset ||= (
67
+ document.bounds.width - qrcode.length * cell_size
68
+ ).to_f / 2
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module Prawn
2
+ module Qr
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/prawn-qr/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Josep Jaume"]
6
+ gem.email = ["josepjaume@gmail.com"]
7
+ gem.description = %q{Prawn-qr is a prawn extension for creating QR Codes inside of a prawn document}
8
+ gem.summary = gem.description
9
+ gem.homepage = "http://github.com/josepjaume/prawn-qr"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "prawn-qr"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Prawn::Qr::VERSION
17
+
18
+ gem.add_runtime_dependency 'rqrcode'
19
+ gem.add_runtime_dependency 'prawn'
20
+ gem.add_development_dependency 'qrio'
21
+ gem.add_development_dependency 'mini_magick'
22
+ gem.add_development_dependency 'mocha'
23
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../spec_helper'
2
+ require 'prawn-qr/qrcode'
3
+ require 'prawn'
4
+ require 'fileutils'
5
+ require 'mini_magick'
6
+ require 'qrio'
7
+
8
+ module Prawn::Qr
9
+ describe QRCode do
10
+ let(:document){ ::Prawn::Document.new(margin: 0) }
11
+
12
+ describe "qrcode" do
13
+ it "will generate a qr code for different sizes" do
14
+ QRCode.new(document, "X").qrcode.length.must_equal 21
15
+ QRCode.new(document, "X" * 50).qrcode.length.must_equal 41
16
+ QRCode.new(document, "X" * 200).qrcode.length.must_equal 81
17
+ end
18
+ end
19
+
20
+ describe "draw" do
21
+ it "generates a valid qr code" do
22
+ QRCode.new(document, "BobRoss").draw
23
+ FileUtils.mkdir_p 'tmp'
24
+ tmp_path = File.expand_path('tmp')
25
+ pdf = File.join(tmp_path, 'test.pdf')
26
+ document.render_file(pdf)
27
+ qr = File.join(tmp_path, 'test.png')
28
+ `convert #{pdf} #{qr}`
29
+ data = Qrio::Qr.load(qr)
30
+ data.qr.text.must_equal "BobRoss"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'spec_helper'
2
+ require 'prawn-qr'
3
+ require 'mocha'
4
+ require 'prawn-qr/qrcode'
5
+
6
+ describe Prawn::Qr do
7
+ describe "qrcode" do
8
+ it "should be available for prawn documents" do
9
+ Prawn::Document.new.must_respond_to :qrcode
10
+ end
11
+
12
+ it "must generate a qrcode" do
13
+ document = Prawn::Document.new
14
+ Prawn::Qr::QRCode.expects(:new).with(document, "BobRoss").returns(
15
+ stub(draw: true)
16
+ )
17
+ document.qrcode("BobRoss").must_equal true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-qr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josep Jaume
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rqrcode
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: prawn
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: qrio
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mini_magick
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: mocha
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Prawn-qr is a prawn extension for creating QR Codes inside of a prawn
95
+ document
96
+ email:
97
+ - josepjaume@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - Guardfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - lib/prawn-qr.rb
109
+ - lib/prawn-qr/qrcode.rb
110
+ - lib/prawn-qr/version.rb
111
+ - prawn-qr.gemspec
112
+ - spec/prawn-qr/qrcode_spec.rb
113
+ - spec/prawn-qr_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: http://github.com/josepjaume/prawn-qr
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.21
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Prawn-qr is a prawn extension for creating QR Codes inside of a prawn document
139
+ test_files:
140
+ - spec/prawn-qr/qrcode_spec.rb
141
+ - spec/prawn-qr_spec.rb
142
+ - spec/spec_helper.rb