Class: Economic::Ocr

Inherits:
Object
  • Object
show all
Defined in:
lib/economic-ocr.rb,
lib/economic-ocr/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.calculate(number) ⇒ Object



6
7
8
9
10
11
# File 'lib/economic-ocr.rb', line 6

def self.calculate(number)
  return unless number
  value = "#{ number }0" # E-conomic wants us to add a 0 for some reason.
  value += ((value.length + 2) % 10).to_s # 2 for length and check.
  (value + luhn_checksum(value).to_s).to_i
end

.id_from_ocr(ocr) ⇒ Object



13
14
15
16
# File 'lib/economic-ocr.rb', line 13

def self.id_from_ocr(ocr)
  return unless ocr
  ocr.to_s[0..-4].to_i
end

.id_from_ocr!(ocr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/economic-ocr.rb', line 18

def self.id_from_ocr!(ocr)
  ocr = ocr.to_i
  number = id_from_ocr(ocr)

  if calculate(number) == ocr
    number
  else
    raise InvalidOcr, "Invalid OCR: #{ocr}"
  end
end