qr4r 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +2 -2
- data/README.md +17 -3
- data/lib/qr4r/version.rb +1 -1
- data/lib/qr4r.rb +60 -8
- data/test/qr4r_test.rb +79 -5
- metadata +45 -73
- data/Gemfile.lock +0 -20
data/.rvmrc
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
rvm use --create 1.8.7-p334
|
1
|
+
rvm use --create 1.9.2-p290@qr4r
|
2
|
+
#rvm use --create 1.8.7-p334
|
3
3
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# QR Encoding via Ruby - with PNG output
|
2
2
|
|
3
|
-
Leveraging
|
3
|
+
Leveraging [rqrcode](http://whomwah.github.com/rqrcode/) and [mojo_magick](http://github.com/2rye/mojo_magick), we've built a very thin gem that generates QR codes in a png file
|
4
4
|
|
5
5
|
# Include in your project
|
6
6
|
|
@@ -12,9 +12,23 @@ In your code, add
|
|
12
12
|
|
13
13
|
require 'qr4r'
|
14
14
|
|
15
|
-
To use it
|
16
|
-
|
15
|
+
To use it:
|
16
|
+
|
17
|
+
Qr4r::encode(input_string, output_file_path, size = 3)
|
18
|
+
|
19
|
+
*input_string* and *output_file_path* should be strings. Size should be an integer from 1 - 10. The final output size is square and will be 33 * size pixels. e.g. if size = 4, the image will be 132x132. Default size is 3 (images are 99x99 pixels).
|
20
|
+
|
21
|
+
NOTE: strings to be encoded are currently limited to 34 characters
|
22
|
+
To encode the string 'qr codes are the new hotness' like this:
|
23
|
+
|
17
24
|
string_to_encode = 'qr codes are the new hotness'
|
18
25
|
Qr4r::encode(string_to_encode, 'qrcode.out.png')
|
19
26
|
|
27
|
+
Not happy with the default size (99px x 99px)? Adjust the size with the 3rd argument to encode
|
28
|
+
|
29
|
+
# the following produces an image who's size is 165px x 165px
|
30
|
+
string_to_encode = 'big qr codes are the new hotness'
|
31
|
+
Qr4r::encode(string_to_encode, 'qrcode.out.png', 5)
|
32
|
+
|
33
|
+
|
20
34
|
|
data/lib/qr4r/version.rb
CHANGED
data/lib/qr4r.rb
CHANGED
@@ -1,9 +1,40 @@
|
|
1
1
|
require 'rqrcode'
|
2
2
|
require 'mojo_magick'
|
3
3
|
module Qr4r
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
|
5
|
+
# params we use
|
6
|
+
# pixel_size - size of each dot, default = 3
|
7
|
+
# params we pass to QRCode include :size and :level
|
8
|
+
# size - the size of the qrcode (default 4)
|
9
|
+
# level - the error correction level, can be:
|
10
|
+
# * Level :l 7% of code can be restored
|
11
|
+
# * Level :m 15% of code can be restored
|
12
|
+
# * Level :q 25% of code can be restored
|
13
|
+
# * Level :h 30% of code can be restored
|
14
|
+
# note: if size is not included and 4 appears to be too small for the included string, we'll make it bigger
|
15
|
+
# if you include size, we'll use it, which may lead to an error if the string is too long
|
16
|
+
# Limitations are as follows:
|
17
|
+
# size = 1, max string length = 7
|
18
|
+
# size = 2, max string length = 14
|
19
|
+
# size = 3, max string length = 24
|
20
|
+
# size = 4, max string length = 34
|
21
|
+
# size = 5, max string length = 44
|
22
|
+
# size = 6, max string length = 58
|
23
|
+
# size = 7, max string length = 64
|
24
|
+
# size = 8, max string length = 84
|
25
|
+
# size = 9, max string length = 98
|
26
|
+
# size = 10, max string length = 119
|
27
|
+
|
28
|
+
def self.encode(str, outfile, *rest)
|
29
|
+
opts = rest[0] if rest && rest.length > 0
|
30
|
+
opts = opts || {}
|
31
|
+
if !opts[:size]
|
32
|
+
opts.merge!({:size => compute_size(str)})
|
33
|
+
end
|
34
|
+
if !opts[:pixel_size]
|
35
|
+
opts.merge!({:pixel_size => 3})
|
36
|
+
end
|
37
|
+
qr = RQRCode::QRCode.new(str, opts)
|
7
38
|
data = []
|
8
39
|
qr.modules.each_index do |x|
|
9
40
|
qr.modules.each_index do |y|
|
@@ -19,11 +50,32 @@ module Qr4r
|
|
19
50
|
c.blob(d, :format => :rgb, :depth => 8, :size => ("%dx%d" % [qr.modules.size, qr.modules.size]))
|
20
51
|
c.file outfile
|
21
52
|
end
|
22
|
-
|
23
|
-
c
|
24
|
-
|
25
|
-
|
53
|
+
if opts[:pixel_size]
|
54
|
+
MojoMagick::convert do |c|
|
55
|
+
wd = qr.modules.size * opts[:pixel_size]
|
56
|
+
c.file outfile
|
57
|
+
c.scale "%dx%d" % [ wd, wd ]
|
58
|
+
c.file outfile
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
SIZE_RESTRICTIONS = [0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119]
|
65
|
+
|
66
|
+
def self.compute_size(str)
|
67
|
+
slen = str.size
|
68
|
+
size = 4
|
69
|
+
ii = 0
|
70
|
+
while ii < SIZE_RESTRICTIONS.length do
|
71
|
+
if slen < SIZE_RESTRICTIONS[ii]
|
72
|
+
break
|
73
|
+
end
|
74
|
+
ii+=1
|
75
|
+
end
|
76
|
+
if ii > 10
|
77
|
+
raise "Your string is too big for this encoder. It should be less than #{SIZE_RESTRICTIONS.last} characters"
|
26
78
|
end
|
79
|
+
return ii
|
27
80
|
end
|
28
|
-
|
29
81
|
end
|
data/test/qr4r_test.rb
CHANGED
@@ -10,19 +10,93 @@ class Qr4rTest < Test::Unit::TestCase
|
|
10
10
|
# assert that it worked
|
11
11
|
assert File.exists?(f.path)
|
12
12
|
r = MojoMagick::get_image_size(f.path)
|
13
|
-
assert r[:height] ==
|
14
|
-
assert r[:width] ==
|
13
|
+
assert r[:height] == 25 * 3
|
14
|
+
assert r[:width] == 25 * 3
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_encode_with_size
|
18
18
|
# do something
|
19
19
|
f = Tempfile.new(['qr4r','.png'])
|
20
|
-
Qr4r::encode('whatever yo', f.path, 4)
|
20
|
+
Qr4r::encode('whatever yo', f.path, :size => 4)
|
21
21
|
# assert that it worked
|
22
22
|
assert File.exists?(f)
|
23
23
|
r = MojoMagick::get_image_size(f.path)
|
24
|
-
assert r[:height] ==
|
25
|
-
assert r[:width] ==
|
24
|
+
assert r[:height] == 33 * 3
|
25
|
+
assert r[:width] == 33 * 3
|
26
26
|
end
|
27
27
|
|
28
|
+
def test_encode_with_pixel_size
|
29
|
+
# do something
|
30
|
+
f = Tempfile.new(['qr4r','.png'])
|
31
|
+
Qr4r::encode('whatever yo', f.path, :pixel_size => 5)
|
32
|
+
# assert that it worked
|
33
|
+
assert File.exists?(f)
|
34
|
+
r = MojoMagick::get_image_size(f.path)
|
35
|
+
assert r[:height] == 25 * 5
|
36
|
+
assert r[:width] == 25 * 5
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_encode_with_size_and_level
|
40
|
+
# do something
|
41
|
+
f = Tempfile.new(['qr4r','.png'])
|
42
|
+
Qr4r::encode('whatever yo', f.path, :size => 4, :level => :m)
|
43
|
+
# assert that it worked
|
44
|
+
assert File.exists?(f)
|
45
|
+
r = MojoMagick::get_image_size(f.path)
|
46
|
+
assert r[:height] == 33 * 3
|
47
|
+
assert r[:width] == 33 * 3
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_a_long_string_with_size_thats_too_small
|
51
|
+
caught = false
|
52
|
+
begin
|
53
|
+
f = Tempfile.new(['qr4r','.png'])
|
54
|
+
Qr4r::encode('this string should also be encodable. don\'t ya think', f.path, :size => 4)
|
55
|
+
rescue
|
56
|
+
caught = true
|
57
|
+
end
|
58
|
+
assert caught, 'Expected an error'
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_a_long_string_with_size_thats_right
|
62
|
+
f = Tempfile.new(['qr4r','.png'])
|
63
|
+
Qr4r::encode('this string should also be encodable. don\'t ya think', f.path, :size => 10)
|
64
|
+
assert File.exists?(f)
|
65
|
+
r = MojoMagick::get_image_size(f.path)
|
66
|
+
assert r[:height] == 57 * 3
|
67
|
+
assert r[:width] == 57 * 3
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_a_long_string_without_size
|
71
|
+
f = Tempfile.new(['qr4r','.png'])
|
72
|
+
Qr4r::encode('this string should also be encodable. don\'t ya think', f.path)
|
73
|
+
assert File.exists?(f)
|
74
|
+
r = MojoMagick::get_image_size(f.path)
|
75
|
+
assert r[:height] = 41 * 3
|
76
|
+
assert r[:width] = 41 * 3
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_compute_size
|
81
|
+
test_sizes = [ 7, 14, 24, 34, 44, 58, 64, 84, 98]
|
82
|
+
test_sizes.each_with_index do |sz, idx|
|
83
|
+
str = 'a'*(sz-1)
|
84
|
+
assert Qr4r.send(:compute_size, str) == idx+1
|
85
|
+
str = 'a'*(sz)
|
86
|
+
assert Qr4r.send(:compute_size, str) == idx+2
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_compute_size_too_big
|
91
|
+
str = 'a'*120
|
92
|
+
caught = false
|
93
|
+
begin
|
94
|
+
Qr4r.send(:compute_size, str)
|
95
|
+
rescue Exception => ex
|
96
|
+
caught = true
|
97
|
+
end
|
98
|
+
assert caught, "Expected exception"
|
99
|
+
|
100
|
+
end
|
101
|
+
|
28
102
|
end
|
metadata
CHANGED
@@ -1,77 +1,58 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: qr4r
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 0
|
10
|
-
version: 0.2.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Jon Rogers
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-03-18 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: rqrcode
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2153649300 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :runtime
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: mojo_magick
|
36
23
|
prerelease: false
|
37
|
-
|
24
|
+
version_requirements: *2153649300
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mojo_magick
|
27
|
+
requirement: &2153648880 !ruby/object:Gem::Requirement
|
38
28
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
46
33
|
type: :runtime
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: rake
|
50
34
|
prerelease: false
|
51
|
-
|
35
|
+
version_requirements: *2153648880
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2153648460 !ruby/object:Gem::Requirement
|
52
39
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
60
44
|
type: :development
|
61
|
-
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2153648460
|
62
47
|
description: QR PNG Generator for Ruby. Leveraging RQRCode and MojoMagick modules
|
63
|
-
email:
|
48
|
+
email:
|
64
49
|
- r@2rye.com
|
65
50
|
executables: []
|
66
|
-
|
67
51
|
extensions: []
|
68
|
-
|
69
52
|
extra_rdoc_files: []
|
70
|
-
|
71
|
-
files:
|
53
|
+
files:
|
72
54
|
- .rvmrc
|
73
55
|
- Gemfile
|
74
|
-
- Gemfile.lock
|
75
56
|
- README.md
|
76
57
|
- Rakefile
|
77
58
|
- init.rb
|
@@ -83,36 +64,27 @@ files:
|
|
83
64
|
- test/qr4r_test.rb
|
84
65
|
homepage: http://github.com/2rye/qr4r
|
85
66
|
licenses: []
|
86
|
-
|
87
67
|
post_install_message:
|
88
68
|
rdoc_options: []
|
89
|
-
|
90
|
-
require_paths:
|
69
|
+
require_paths:
|
91
70
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
72
|
none: false
|
94
|
-
requirements:
|
95
|
-
- -
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
|
99
|
-
- 0
|
100
|
-
version: "0"
|
101
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
78
|
none: false
|
103
|
-
requirements:
|
104
|
-
- -
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
|
107
|
-
segments:
|
108
|
-
- 0
|
109
|
-
version: "0"
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
110
83
|
requirements: []
|
111
|
-
|
112
84
|
rubyforge_project: qr4r
|
113
85
|
rubygems_version: 1.8.15
|
114
86
|
signing_key:
|
115
87
|
specification_version: 3
|
116
|
-
summary: qr4r-0.
|
117
|
-
test_files:
|
88
|
+
summary: qr4r-0.3.0
|
89
|
+
test_files:
|
118
90
|
- test/qr4r_test.rb
|
data/Gemfile.lock
DELETED