Class: ISTC

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

Defined Under Namespace

Classes: Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ISTC

Returns a new instance of ISTC.



11
12
13
# File 'lib/istc.rb', line 11

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

Class Method Details

.complete(num) ⇒ Object

Purely for generating new ISTC numbers



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/istc.rb', line 52

def self.complete(num)
  num = num.to_s
  return nil unless num.length == 15 && num.match(/[\dA-Fa-f]{11}/)

  arr = (0..14).to_a.collect do |i|
    if i == 0 || i % 4 == 0
      num[i,1].hex * 11
    elsif i % 4 == 1
      num[i,1].hex * 9
    elsif i % 4 == 2
      num[i,1].hex * 3
    else
      num[i,1].hex * 1
    end
  end
  sum = arr.inject { |sum, n| sum + n }
  remainder = sum % 16
  check = remainder.to_s(16).upcase

  "#{num}#{check}"
end

.valid?(istc) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/istc.rb', line 45

def self.valid?(istc)
  istc = istc.to_s
  istc.length == 16 && istc == ISTC.complete(istc[0,15])
end

Instance Method Details

#agencyObject



19
20
21
22
# File 'lib/istc.rb', line 19

def agency
  return nil unless valid?
  @number[0,3]
end

#checkObject



34
35
36
37
# File 'lib/istc.rb', line 34

def check
  return nil unless valid?
  @number[15,1]
end

#to_sObject

output a hyphenated version of this ISTC



40
41
42
43
# File 'lib/istc.rb', line 40

def to_s
  return nil unless valid?
  "#{agency}-#{year}-#{work}-#{check}"
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/istc.rb', line 15

def valid?
  ISTC.valid? @number
end

#workObject



29
30
31
32
# File 'lib/istc.rb', line 29

def work
  return nil unless valid?
  @number[7,8]
end

#yearObject



24
25
26
27
# File 'lib/istc.rb', line 24

def year
  return nil unless valid?
  @number[3,4]
end