class CQR::QRFormatter

Attributes

invert[RW]
invert?[RW]
margin[RW]

Public Class Methods

new(string, margin = 0, invert = true) click to toggle source
# File lib/cqr/qr_formatter.rb, line 8
def initialize(string, margin = 0, invert = true)
  self.margin = margin
  self.invert = invert
  create_qr_code(string)
end

Public Instance Methods

to_s() click to toggle source
# File lib/cqr/qr_formatter.rb, line 14
def to_s
  lines = []

  lines << horizontal_margin

  qrstring.lines.map do |line|
    lines << "#{prefix}#{vertical_margin}#{line.chomp}#{vertical_margin}"
  end

  lines << horizontal_margin

  lines.join "\n"
end

Private Instance Methods

create_qr_code(string) click to toggle source
# File lib/cqr/qr_formatter.rb, line 44
def create_qr_code(string)
  size = 1
  begin
    @qrcode = RQRCode::QRCode.new(string, size: size)
  rescue RQRCode::QRCodeRunTimeError
    size += 1
    retry
  end
end
horizontal_margin() click to toggle source
# File lib/cqr/qr_formatter.rb, line 38
def horizontal_margin
  width = qrstring.lines.first.chomp.size
  lines = margin.times.map{ "#{prefix}#{" "*(width+2*margin)}" }
  lines.join "\n"
end
prefix() click to toggle source
# File lib/cqr/qr_formatter.rb, line 54
def prefix
  prefix = (invert? ? "\e[7m" : "")
end
qrstring() click to toggle source
# File lib/cqr/qr_formatter.rb, line 30
def qrstring
  @qrstring ||= @qrcode.to_s(true: "\u2588", false: " ")
end
vertical_margin() click to toggle source
# File lib/cqr/qr_formatter.rb, line 34
def vertical_margin
  " " * margin
end