Class: QrcodeGenerator::Runner
- Inherits:
-
Object
- Object
- QrcodeGenerator::Runner
- Defined in:
- lib/qrcode-generator/runner.rb
Instance Attribute Summary collapse
-
#destSpace ⇒ Object
readonly
Returns the value of attribute destSpace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Instance Method Summary collapse
- #build_logo(img, size) ⇒ Object
- #build_qrcode(url) ⇒ Object
- #draw_qrcode_image(url, width) ⇒ Object
-
#initialize(workspace, options) ⇒ Runner
constructor
A new instance of Runner.
- #run ⇒ Object
- #sanitize_filename(basename) ⇒ Object
- #to_qrcode_base64(blob) ⇒ Object
- #to_qrcode_blob(url, width) ⇒ Object
- #to_qrcode_file(url, filename) ⇒ Object
Constructor Details
#initialize(workspace, options) ⇒ Runner
Returns a new instance of Runner.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/qrcode-generator/runner.rb', line 12 def initialize(workspace, ) @workspace = workspace = time = Time.now dayStr = time.strftime('%m-%d') clockStr = time.strftime('%H:%M:%S') @destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}") end |
Instance Attribute Details
#destSpace ⇒ Object (readonly)
Returns the value of attribute destSpace.
10 11 12 |
# File 'lib/qrcode-generator/runner.rb', line 10 def destSpace @destSpace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/qrcode-generator/runner.rb', line 10 def end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
10 11 12 |
# File 'lib/qrcode-generator/runner.rb', line 10 def workspace @workspace end |
Instance Method Details
#build_logo(img, size) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/qrcode-generator/runner.rb', line 96 def build_logo(img,size) logo_filepath = File.join(workspace, "logo.png") if File.exist?(logo_filepath) logo = Magick::Image.read(logo_filepath)[0] logo = logo.resize(size,size) img.composite!(logo, Magick::CenterGravity, Magick::OverCompositeOp) end img end |
#build_qrcode(url) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/qrcode-generator/runner.rb', line 83 def build_qrcode(url) qr = RQRCode::QRCode.new(url, size: [:size], level: [:level]) border = [:border] qrcode = Magick::Draw.new qr.modules.each_index do |x| qr.modules.each_index do |y| qrcode.fill( qr.dark?(x,y) ? 'black' : 'white' ) qrcode.point(y+border,x+border) end end qrcode end |
#draw_qrcode_image(url, width) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/qrcode-generator/runner.rb', line 68 def draw_qrcode_image(url, width) qr_width = 17 + 4 * [:size] + 2 * [:border] width = width < qr_width ? qr_width : width img = Magick::Image.new(qr_width,qr_width) build_qrcode(url).draw(img) img = img.resize(width,width,Magick::PointFilter) if [:logo] logo_size = width/([:logo_denominator]*1.0) build_logo(img, logo_size) end img end |
#run ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/qrcode-generator/runner.rb', line 23 def run txtPath = File.join(workspace, "links.txt") if !File.file?(txtPath) puts "ERROR: Could not find file 'links.txt' in current directory" return end FileUtils.mkdir(destSpace) if !File.directory?(destSpace) File.readlines(txtPath).each do |line| line = line.strip.chomp next if line.empty? line = line.gsub("\t", " ") #tab to space if reverse_point = line.reverse.index(/[ ,]/) point = line.length - reverse_point link = line[point..-1] title = line[0, point].strip.chomp(",").strip title = link if title.empty? else link = line title = link end filename = sanitize_filename(title) + ".png" to_qrcode_file(link, filename) end end |
#sanitize_filename(basename) ⇒ Object
54 55 56 57 |
# File 'lib/qrcode-generator/runner.rb', line 54 def sanitize_filename(basename) basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_') end |
#to_qrcode_base64(blob) ⇒ Object
119 120 121 |
# File 'lib/qrcode-generator/runner.rb', line 119 def to_qrcode_base64(blob) "data:image/png;base64,#{Base64.encode64(blob)}" end |
#to_qrcode_blob(url, width) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/qrcode-generator/runner.rb', line 109 def to_qrcode_blob(url,width) img = draw_qrcode_image(url, width) blob = img.to_blob { self.format = 'PNG' } blob end |
#to_qrcode_file(url, filename) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/qrcode-generator/runner.rb', line 59 def to_qrcode_file(url,filename) img = draw_qrcode_image(url, [:width]) img.format = "PNG" img.write(File.join(destSpace, filename)) end |