Class: QrcodeGenerator::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/qrcode-generator/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#destSpaceObject (readonly)

Returns the value of attribute destSpace.



10
11
12
# File 'lib/qrcode-generator/runner.rb', line 10

def destSpace
  @destSpace
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/qrcode-generator/runner.rb', line 10

def options
  @options
end

#workspaceObject (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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/qrcode-generator/runner.rb', line 102

def (img,size)
  logo_filepath = File.join(workspace, "logo.png")

  if File.exist?(logo_filepath)
     = Magick::Image.read(logo_filepath)[0]
     = .resize(size,size)

    img.composite!(, Magick::CenterGravity, Magick::OverCompositeOp)
  end
  
  img
end

#build_qrcode(url) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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)
    (img, logo_size)
  end

  img
end

#parse_filename_tagObject



56
57
58
# File 'lib/qrcode-generator/runner.rb', line 56

def parse_filename_tag
  options[:tag] ? "(#{options[:tag]})" : ""
end

#runObject



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
53
54
# 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) ⇒ Object



60
61
62
63
# File 'lib/qrcode-generator/runner.rb', line 60

def sanitize_filename(basename)

  basename.strip.gsub(/[\:\*\"\<\>\|]/, '').gsub(/[\\\/\.\?]/, '_')
end

#to_qrcode_base64(blob) ⇒ Object



125
126
127
# 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) ⇒ Object



115
116
117
118
119
120
121
122
123
# 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) ⇒ Object



65
66
67
68
69
70
71
72
# 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