Class: RqrcodePngBin::App

Inherits:
Object
  • Object
show all
Defined in:
lib/rqrcode_png_bin/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ App

Returns a new instance of App.



6
7
8
9
10
11
12
13
# File 'lib/rqrcode_png_bin/app.rb', line 6

def initialize(argv = [])
  @argv   = argv
  @size   = 4
  @level  = :m
  @canvas = nil

  parser.parse!(@argv)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



14
15
16
# File 'lib/rqrcode_png_bin/app.rb', line 14

def canvas
  @canvas
end

#levelObject (readonly)

Returns the value of attribute level.



14
15
16
# File 'lib/rqrcode_png_bin/app.rb', line 14

def level
  @level
end

#sizeObject (readonly)

Returns the value of attribute size.



14
15
16
# File 'lib/rqrcode_png_bin/app.rb', line 14

def size
  @size
end

Instance Method Details

#optsObject

return

Hash



37
38
39
40
41
42
# File 'lib/rqrcode_png_bin/app.rb', line 37

def opts
  h = {
    :size  => @size,
    :level => @level
  }
end

#parserObject

return

OptionParser



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rqrcode_png_bin/app.rb', line 47

def parser
  OptionParser.new do |opt|
    opt.version = VERSION
    opt.banner  = "Usage: rqrcode_png [option] string"
    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('-l', '--level LEVEL (default m)') {|v|
      options = %w(l m q h)

      if options.include?(v)
        @level = v.to_sym
      else
        raise ArgumentError, "option level should be included #{options}"
      end
    }
    opt.on('-c', '--canvas CANVAS (ex 200x200)') {|v|
      re = %r{\A([0-9]+)x([0-9]+)\z}

      if v =~ re
        @canvas = [$1.to_i, $2.to_i]
      else
        raise ArgumentError, "option canvas should match #{re}"
      end
    }
  end
end

#runObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/rqrcode_png_bin/app.rb', line 16

def run
  if str
    png = RQRCode::QRCode.new(str, opts).to_img
    png = png.resize(*canvas) if canvas

    STDOUT.puts png
  else
    STDERR.puts "rqrcode_png #{VERSION}", nil, parser.help
  end
end

#strObject

return

String



30
31
32
# File 'lib/rqrcode_png_bin/app.rb', line 30

def str
  str = @argv.first
end