Class: RqrcodePngBin::App
- Inherits:
-
Object
- Object
- RqrcodePngBin::App
- Defined in:
- lib/rqrcode_png_bin/app.rb
Instance Attribute Summary collapse
-
#border_modules ⇒ Object
readonly
Returns the value of attribute border_modules.
-
#canvas ⇒ Object
readonly
Returns the value of attribute canvas.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#px_per_module ⇒ Object
readonly
Returns the value of attribute px_per_module.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
Instance Method Summary collapse
- #encoded_str(str) ⇒ Object
- #generate_png(str) ⇒ Object
-
#initialize(argv = []) ⇒ App
constructor
A new instance of App.
-
#opts ⇒ Object
- return
-
Hash.
-
#parser ⇒ Object
- return
-
OptionParser.
- #png_opts ⇒ Object
- #run ⇒ Object
-
#str ⇒ Object
- return
-
String.
Constructor Details
#initialize(argv = []) ⇒ App
Returns a new instance of App.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rqrcode_png_bin/app.rb', line 5 def initialize(argv = []) @argv = argv @border_modules = 4 @canvas = nil @file = nil @level = :m @mode = nil @px_per_module = nil @size = 4 @stdin = nil @try_larger = false parser.parse!(@argv) end |
Instance Attribute Details
#border_modules ⇒ Object (readonly)
Returns the value of attribute border_modules.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def border_modules @border_modules end |
#canvas ⇒ Object (readonly)
Returns the value of attribute canvas.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def canvas @canvas end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def file @file end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def level @level end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def mode @mode end |
#px_per_module ⇒ Object (readonly)
Returns the value of attribute px_per_module.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def px_per_module @px_per_module end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def size @size end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
20 21 22 |
# File 'lib/rqrcode_png_bin/app.rb', line 20 def stdin @stdin end |
Instance Method Details
#encoded_str(str) ⇒ Object
53 54 55 |
# File 'lib/rqrcode_png_bin/app.rb', line 53 def encoded_str(str) str.encode('SJIS').force_encoding('ASCII-8BIT') end |
#generate_png(str) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rqrcode_png_bin/app.rb', line 37 def generate_png(str) qr_opts = opts.dup begin RQRCode::QRCode.new(encoded_str(str), qr_opts).as_png(png_opts) rescue RQRCode::QRCodeRunTimeError => e if @try_larger && qr_opts[:size] < 40 qr_opts[:size] = qr_opts[:size] + 1 STDERR.puts "retrying #{str} with options #{qr_opts}" retry else raise e end end end |
#opts ⇒ Object
- return
-
Hash
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rqrcode_png_bin/app.rb', line 72 def opts h = { :size => @size, :level => @level } h[:mode] = @mode if @mode h end |
#parser ⇒ Object
- return
-
OptionParser
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rqrcode_png_bin/app.rb', line 96 def parser OptionParser.new do |opt| opt.version = VERSION opt. = "Usage: rqrcode_png [option] string" opt.on('-b', '--border-modules BORDER') {|v| if v =~ /\A[0-9]+\z/ @border_modules = v.to_i else raise ArgumentError, "option border modules should be integer" end } opt.on('-c', '--canvas CANVAS (ex 200)') {|v| if v =~ /\A[0-9]+\z/ @canvas = v.to_i else raise ArgumentError, "option canvas should match be integer" end } opt.on('-f', '--from-file FILE') {|v| if File.exist?(v) @file = v else raise ArgumentError, "file you specified '#{v}' does not exist" end } opt.on('-l', '--level l|m|q|h (default m)') {|v| = %w(l m q h) if .include?(v) @level = v.to_sym else raise ArgumentError, "option level should be included #{}" end } opt.on('-m', '--mode MODE') {|v| = %w(number alphanumeric byte_8bit) if .include?(v) @mode = v.to_sym else raise ArgumentError, "option mode should be included #{}" end } opt.on('-p', '--pixels-per-module PIXELS') {|v| if v =~ /\A[0-9]+\z/ @px_per_module = v.to_i else raise ArgumentError, "option pixels per module should be integer" end } opt.on('-s', '--size SIZE (default 4)') {|v| re = %r{\A[0-9]+\z} if v =~ re @size = v.to_i else raise ArgumentError, "option size should match #{re}" end } opt.on('-t', '--try-larger') {|v| @try_larger = true } end end |
#png_opts ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/rqrcode_png_bin/app.rb', line 83 def png_opts h = {} h[:size] = canvas if canvas h[:border_modules] = border_modules h[:module_px_size] = px_per_module if px_per_module h end |
#run ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rqrcode_png_bin/app.rb', line 22 def run if file FileReader.new(file).each {|str, dest| FileUtils.mkdir_p(File.dirname(dest)) open(dest, 'wb') {|f| f.puts generate_png(str) } } elsif str STDOUT.puts generate_png(str) else STDERR.puts "rqrcode_png #{VERSION}", '', parser.help end end |
#str ⇒ Object
- return
-
String
60 61 62 63 64 65 66 67 |
# File 'lib/rqrcode_png_bin/app.rb', line 60 def str if @argv.first @argv.first else @stdin ||= $stdin.gets stdin if stdin.size > 0 end end |