Module: Samwise::Util

Defined in:
lib/samwise/util.rb

Class Method Summary collapse

Class Method Details

.duns_contains_forbidden_characters?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/samwise/util.rb', line 35

def self.duns_contains_forbidden_characters?(duns: nil)
  duns.split('').each do |character|
    return false if self.string_is_numeric?(string: character)
  end

  false
end

.duns_is_eight_digits?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/samwise/util.rb', line 55

def self.duns_is_eight_digits?(duns: nil)
  duns.length == 8
end

.duns_is_nine_digits?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/samwise/util.rb', line 59

def self.duns_is_nine_digits?(duns: nil)
  duns.length == 9
end

.duns_is_properly_formatted?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/samwise/util.rb', line 4

def self.duns_is_properly_formatted?(duns: nil)
  return false if duns.nil?

  duns = duns.gsub('-', '')

  return true if duns_contains_forbidden_characters?(duns: duns)

  return true if duns_is_seven_digits?(duns: duns)
  return true if duns_is_eight_digits?(duns: duns)
  return true if duns_is_nine_digits?(duns: duns)
  return true if duns_is_thirteen_digits?(duns: duns)

  return false
end

.duns_is_seven_digits?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/samwise/util.rb', line 51

def self.duns_is_seven_digits?(duns: nil)
  duns.length == 7
end

.duns_is_thirteen_digits?(duns: nil) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/samwise/util.rb', line 63

def self.duns_is_thirteen_digits?(duns: nil)
  duns.length == 13
end

.format_duns(duns: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/samwise/util.rb', line 19

def self.format_duns(duns: nil)
  raise Samwise::Error::InvalidFormat unless duns_is_properly_formatted?(duns: duns)

  duns = duns.gsub('-', '')

  if duns_is_nine_digits?(duns: duns)
    duns = "#{duns}0000"
  elsif duns_is_eight_digits?(duns: duns)
    duns = "0#{duns}0000"
  elsif duns_is_seven_digits?(duns: duns)
    duns = "00#{duns}0000"
  end

  duns
end

.string_is_numeric?(string: nil) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/samwise/util.rb', line 43

def self.string_is_numeric?(string: nil)
  string.split('').each do |character|
    return false unless character.to_i.to_s == character
  end

  return true
end