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.



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

def initialize(argv = [])
  @argv   = argv

  @canvas = nil
  @file   = nil
  @level  = :m
  @size   = 4
  @stdin  = nil

  parser.parse!(@argv)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



16
17
18
# File 'lib/rqrcode_png_bin/app.rb', line 16

def canvas
  @canvas
end

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/rqrcode_png_bin/app.rb', line 16

def file
  @file
end

#levelObject (readonly)

Returns the value of attribute level.



16
17
18
# File 'lib/rqrcode_png_bin/app.rb', line 16

def level
  @level
end

#sizeObject (readonly)

Returns the value of attribute size.



16
17
18
# File 'lib/rqrcode_png_bin/app.rb', line 16

def size
  @size
end

#stdinObject (readonly)

Returns the value of attribute stdin.



16
17
18
# File 'lib/rqrcode_png_bin/app.rb', line 16

def stdin
  @stdin
end

Instance Method Details

#encoded_str(str) ⇒ Object



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

def encoded_str(str)
  str.encode('SJIS').force_encoding('ASCII-8BIT')
end

#generate_png(str) ⇒ Object



32
33
34
35
36
37
# File 'lib/rqrcode_png_bin/app.rb', line 32

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

  png
end

#optsObject

return

Hash



58
59
60
61
62
63
# File 'lib/rqrcode_png_bin/app.rb', line 58

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

#parserObject

return

OptionParser



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rqrcode_png_bin/app.rb', line 68

def parser
  OptionParser.new do |opt|
    opt.version = VERSION
    opt.banner  = "Usage: rqrcode_png [option] string"

    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
    }
    opt.on('-f', '--from-file FILE') {|v|
      if File.exist?(v)
        @file = v
      else
        raise ArgumentError, "file you specified '#{v}' does not exist"
      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('-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
    }
  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rqrcode_png_bin/app.rb', line 18

def run
  if file
    FileReader.new(file).each {|str, dest|
      open(dest, 'wb') {|f|
        f.puts generate_png(str)
      }
    }
  elsif str
    STDOUT.puts generate_png(str)
  else
    STDERR.puts "rqrcode_png #{VERSION}", '', parser.help
  end
end

#strObject

return

String



46
47
48
49
50
51
52
53
# File 'lib/rqrcode_png_bin/app.rb', line 46

def str
  if @argv.first
    @argv.first
  else
    @stdin ||= $stdin.gets
    stdin if stdin.size > 0
  end
end