Method: RQRCode::QRCode#initialize

Defined in:
lib/rqrcode/qrcode/qr_code.rb

#initialize(string, *args) ⇒ QRCode

Expects a string to be parsed in, other args are optional

# string - the string you wish to encode
# size   - the size of the qrcode (default 4)
# level  - the error correction level, can be:
   * Level :l 7%  of code can be restored
   * Level :m 15% of code can be restored
   * Level :q 25% of code can be restored
   * Level :h 30% of code can be restored (default :h)
# mode   - the mode of the qrcode (defaults to alphanumeric or byte_8bit, depending on the input data):
   * :number
   * :alphanumeric
   * :byte_8bit
   * :kanji

qr = RQRCode::QRCode.new('hello world', :size => 1, :level => :m, :mode => :alphanumeric )


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rqrcode/qrcode/qr_code.rb', line 117

def initialize( string, *args )
  if !string.is_a? String
    raise QRCodeArgumentError, "The passed data is #{string.class}, not String"
  end

  options               = args.extract_options!
  level                 = (options[:level] || :h).to_sym

  if !QRERRORCORRECTLEVEL.has_key?(level)
    raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
  end

  @data                 = string

  mode                  = QRMODE_NAME[(options[:mode] || '').to_sym]
  mode                  ||= QRAlphanumeric.valid_data?( @data ) ? :mode_alpha_numk : :mode_8bit_byte  # deprecate?

  max_size_array        = QRMAXDIGITS[level][mode]
  size                  = options[:size] || smallest_size_for(string, max_size_array)
  
  if size > QRUtil.max_size
    raise QRCodeArgumentError, "Given size greater than maximum possible size of #{QRUtil.max_size}"
  end

  @error_correct_level  = QRERRORCORRECTLEVEL[level]
  @version              = size
  @module_count         = @version * 4 + QRPOSITIONPATTERNLENGTH
  @modules              = Array.new( @module_count )
  @data_list            =
    case mode
    when :mode_number
      QRNumeric.new( @data )
    when :mode_alpha_numk
      QRAlphanumeric.new( @data )
    else
      QR8bitByte.new( @data )
    end

  @data_cache           = nil
  self.make
end