class QrcodeGenerator::Runner

Attributes

destSpace[R]
options[R]
workspace[R]

Public Class Methods

new(workspace, options) click to toggle source
# File lib/qrcode-generator/runner.rb, line 12
def initialize(workspace, options)
  @workspace = workspace
  @options = options

  time = Time.now

  dayStr = time.strftime('%m-%d')
  clockStr = time.strftime('%H.%M.%S')
  @destSpace = File.join(workspace, "qrcode #{dayStr} #{clockStr}")
end

Public Instance Methods

build_qrcode(url) click to toggle source
# File lib/qrcode-generator/runner.rb, line 89
def build_qrcode(url)
  qr = RQRCode::QRCode.new(url, size: options[:size], level: options[:level])
  border = options[: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) click to toggle source
# File lib/qrcode-generator/runner.rb, line 74
def draw_qrcode_image(url, width)
  qr_width = 17 + 4 * options[:size] + 2 * options[: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 options[:logo]
    logo_size = width/(options[:logo_denominator]*1.0)
    build_logo(img, logo_size)
  end

  img
end
parse_filename_tag() click to toggle source
# File lib/qrcode-generator/runner.rb, line 56
def parse_filename_tag
  options[:tag] ? "(#{options[:tag]})" : ""
end
run() click to toggle source
# 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)

  filename_tag = parse_filename_tag

  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) + filename_tag + ".png"

    to_qrcode_file(link, filename)
  end
end
sanitize_filename(basename) click to toggle source
# File lib/qrcode-generator/runner.rb, line 60
def sanitize_filename(basename)

  basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
end
to_qrcode_base64(blob) click to toggle source
# File lib/qrcode-generator/runner.rb, line 125
def to_qrcode_base64(blob)
  "data:image/png;base64,#{Base64.encode64(blob)}"
end
to_qrcode_blob(url,width) click to toggle source
# File lib/qrcode-generator/runner.rb, line 115
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) click to toggle source
# File lib/qrcode-generator/runner.rb, line 65
def to_qrcode_file(url,filename)
  
  img = draw_qrcode_image(url, options[:width])

  img.format = "PNG"

  img.write(File.join(destSpace, filename))
end