Class: CallSign::CallSign

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

Constant Summary collapse

EXTRACT_REGEX =
/^(?<prefix>[BFGIKMNRW]{1}|[A-Z][A-Z]|[A-Z]{1}[0-9]{1}|[0-9][A-Z]|[A-Z]|[0-9A-Z]{3})(?<separator>[0-9]{1})(?<suffix>[0-9A-Z]{1,5})$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call_sign) ⇒ CallSign

Returns a new instance of CallSign.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/call_sign/call_sign.rb', line 8

def initialize(call_sign)
  components = CallSign.extract(call_sign)

  if components.is_a? Hash
    @call_sign        = components[:text]
    @prefix           = ITUPrefix.parse(components[:prefix])
    @prefix_string    = components[:prefix]
    @separator_string = components[:separator]
    @suffix_string    = components[:suffix]
    @text             = components[:text]
    @valid            = true
  else
    @call_sign        = nil
    @prefix_string    = nil
    @separator_string = nil
    @suffix_string    = nil
    @text             = call_sign
    @valid            = false
  end
end

Instance Attribute Details

#call_signObject (readonly)

Returns the value of attribute call_sign.



6
7
8
# File 'lib/call_sign/call_sign.rb', line 6

def call_sign
  @call_sign
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/call_sign/call_sign.rb', line 6

def prefix
  @prefix
end

#prefix_stringObject (readonly)

Returns the value of attribute prefix_string.



6
7
8
# File 'lib/call_sign/call_sign.rb', line 6

def prefix_string
  @prefix_string
end

#separator_stringObject (readonly)

Returns the value of attribute separator_string.



6
7
8
# File 'lib/call_sign/call_sign.rb', line 6

def separator_string
  @separator_string
end

#suffix_stringObject (readonly)

Returns the value of attribute suffix_string.



6
7
8
# File 'lib/call_sign/call_sign.rb', line 6

def suffix_string
  @suffix_string
end

Class Method Details

.extract(call_sign) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/call_sign/call_sign.rb', line 37

def self.extract(call_sign)
  # Check for exceptions
  # 1-letter prefix
  # 2-letter prefix
  # 3-letter prefix
  # Match regex
  match = EXTRACT_REGEX.match(call_sign)
  return if !match

  { text: match.string, prefix: match[:prefix], separator: match[:separator], suffix: match[:suffix] }
end

.parse(call_sign) ⇒ Object



50
51
52
# File 'lib/call_sign/call_sign.rb', line 50

def self.parse(call_sign)
  CallSign.new(call_sign)
end

Instance Method Details

#countryObject



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

def country
  valid? && prefix && prefix.country ? prefix.country : nil
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/call_sign/call_sign.rb', line 33

def valid?
  @valid
end