Class: SAN

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

Defined Under Namespace

Classes: Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ SAN

Returns a new instance of SAN.



13
14
15
# File 'lib/san.rb', line 13

def initialize(str)
  @number = str.to_s
end

Class Method Details

.complete(six_digit_san) ⇒ Object

Purely for generating new SAN numbers



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/san.rb', line 49

def self.complete(six_digit_san)
  six_digit_san = six_digit_san.to_s
  return nil unless six_digit_san.length == 6 && six_digit_san.match(/\d{6}/)
  
  #sum = (0..5).to_a.sum { |i| six_digit_san[i,1].to_i * (7-i) }
  arr = (0..5).to_a.collect { |i| six_digit_san[i,1].to_i * (7-i) }
  sum = arr.inject { |sum, n| sum + n }
  check = 11 - (sum % 11)
  
  check = 0   if check == 11
  check = 'X' if check == 10
  
  six_digit_san + check.to_s
end

.valid?(san) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/san.rb', line 43

def self.valid?(san)
  san = san.to_s
  san.length == 7 && san == SAN.complete(san[0,6])
end

Instance Method Details

#to_uk_glnObject

convert this SAN into a UK based Global Location Number.

see:

- http://en.wikipedia.org/wiki/Global_Location_Number
- http://www.bisg.org/conferences/UConnnect_2007.pdf


38
39
40
41
# File 'lib/san.rb', line 38

def to_uk_gln
  return nil unless valid?
  EAN13.complete("503067#{@number[0,6]}")
end

#to_us_glnObject

convert this SAN into a US based Global Location Number.

see:

- http://en.wikipedia.org/wiki/Global_Location_Number
- http://www.bisg.org/conferences/UConnnect_2007.pdf


27
28
29
30
# File 'lib/san.rb', line 27

def to_us_gln
  return nil unless valid?
  EAN13.complete("079999#{@number[0,6]}")
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/san.rb', line 17

def valid?
  SAN.valid? @number
end