Class: RQRCodeCore::QRCode
- Inherits:
-
Object
- Object
- RQRCodeCore::QRCode
- Defined in:
- lib/rqrcode_core/qrcode/qr_code.rb
Overview
Creation
QRCode objects expect only one required constructor parameter and an optional hash of any other. Here’s a few examples:
qr = RQRCodeCore::QRCode.new('hello world')
qr = RQRCodeCore::QRCode.new('hello world', size: 1, level: :m, mode: :alphanumeric)
Instance Attribute Summary collapse
-
#module_count ⇒ Object
readonly
Returns the value of attribute module_count.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#checked?(row, col) ⇒ Boolean
checked?
is called with acol
androw
parameter. -
#error_correction_level ⇒ Object
Return a symbol for current error connection level.
-
#initialize(string, *args) ⇒ QRCode
constructor
Expects a string to be parsed in, other args are optional.
-
#inspect ⇒ Object
Public overide as default inspect is very verbose.
-
#mode ⇒ Object
Return a symbol in QRMODE.keys for current mode used.
-
#to_s(*args) ⇒ Object
This is a public method that returns the QR Code you have generated as a string.
Constructor Details
#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 = RQRCodeCore::QRCode.new('hello world', size: 1, level: :m, mode: :alphanumeric)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 172 def initialize( string, *args ) if !string.is_a? String raise QRCodeArgumentError, "The passed data is #{string.class}, not String" end = args. level = ([:level] || :h).to_sym if !QRERRORCORRECTLEVEL.has_key?(level) raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`" end @data = string mode = QRMODE_NAME[([:mode] || '').to_sym] # If mode is not explicitely given choose mode according to data type mode ||= case when RQRCodeCore::QRNumeric.valid_data?(@data) QRMODE_NAME[:number] when QRAlphanumeric.valid_data?(@data) QRMODE_NAME[:alphanumeric] else QRMODE_NAME[:byte_8bit] end max_size_array = QRMAXDIGITS[level][mode] size = [: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 |
Instance Attribute Details
#module_count ⇒ Object (readonly)
Returns the value of attribute module_count.
152 153 154 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 152 def module_count @module_count end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
152 153 154 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 152 def modules @modules end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
152 153 154 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 152 def version @version end |
Instance Method Details
#checked?(row, col) ⇒ Boolean
checked?
is called with a col
and row
parameter. This will return true or false based on whether that coordinate exists in the matrix returned. It would normally be called while iterating through modules
. A simple example would be:
instance.checked?( 10, 10 ) => true
230 231 232 233 234 235 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 230 def checked?( row, col ) if !row.between?(0, @module_count - 1) || !col.between?(0, @module_count - 1) raise QRCodeRunTimeError, "Invalid row/column pair: #{row}, #{col}" end @modules[row][col] end |
#error_correction_level ⇒ Object
Return a symbol for current error connection level
289 290 291 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 289 def error_correction_level QRERRORCORRECTLEVEL.invert[@error_correct_level] end |
#inspect ⇒ Object
Public overide as default inspect is very verbose
RQRCodeCore::QRCode.new('my string to generate', size: 4, level: :h)
=> QRCodeCore: @data='my string to generate', @error_correct_level=2, @version=4, @module_count=33
284 285 286 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 284 def inspect "QRCodeCore: @data='#{@data}', @error_correct_level=#{@error_correct_level}, @version=#{@version}, @module_count=#{@module_count}" end |
#mode ⇒ Object
Return a symbol in QRMODE.keys for current mode used
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 294 def mode case @data_list when QRNumeric :mode_number when QRAlphanumeric :mode_alpha_numk else :mode_8bit_byte end end |
#to_s(*args) ⇒ Object
This is a public method that returns the QR Code you have generated as a string. It will not be able to be read in this format by a QR Code reader, but will give you an idea if the final outout. It takes two optional args :true
and :false
which are there for you to choose how the output looks. Here’s an example of it’s use:
instance.to_s =>
xxxxxxx x x x x x xx xxxxxxx
x x xxx xxxxxx xxx x x
x xxx x xxxxx x xx x xxx x
instance.to_s( dark: 'E', light: 'Q' ) =>
EEEEEEEQEQQEQEQQQEQEQQEEQQEEEEEEE
EQQQQQEQQEEEQQEEEEEEQEEEQQEQQQQQE
EQEEEQEQQEEEEEQEQQQQQQQEEQEQEEEQE
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rqrcode_core/qrcode/qr_code.rb', line 255 def to_s( *args ) = args. dark = [:dark] || [:true] || 'x' light = [:light] || [:false] || ' ' quiet_zone_size = [:quiet_zone_size] || 0 rows = [] @modules.each do |row| cols = light * quiet_zone_size row.each do |col| cols += (col ? dark : light) end rows << cols end quiet_zone_size.times do rows.unshift(light * (rows.first.length / light.size)) rows << light * (rows.first.length / light.size) end rows.join("\n") end |