Class: NT54::Parser

Inherits:
Object
  • Object
show all
Includes:
Machinery
Defined in:
lib/nt54/parser.rb,
lib/nt54/parser/visitor.rb,
lib/nt54/parser/machinery.rb,
lib/nt54/parser/visitors/dialtone.rb,
lib/nt54/parser/visitors/wait_for_prefix.rb,
lib/nt54/parser/visitors/wait_for_area_code.rb,
lib/nt54/parser/visitors/wait_for_local_number.rb,
lib/nt54/parser/visitors/wait_for_local_prefix.rb,
lib/nt54/parser/visitors/wait_for_country_code_end.rb,
lib/nt54/parser/visitors/wait_for_country_code_start.rb,
lib/nt54/parser/visitors/wait_for_area_code_completion.rb,
lib/nt54/parser/visitors/wait_for_mobile_prefix_completion.rb,
lib/nt54/parser/visitors/wait_for_special_number_completion.rb,
lib/nt54/parser/visitors/wait_for_mobile_prefix_or_area_code.rb,
lib/nt54/parser/visitors/wait_for_area_code_or_mobile_prefix_completion.rb

Defined Under Namespace

Modules: Machinery, Visitors Classes: Visitor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



82
83
84
85
# File 'lib/nt54/parser.rb', line 82

def initialize
  @number  = PhoneNumber.new
  @visitor = Parser::Visitors::Dialtone.new(@number)
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



9
10
11
# File 'lib/nt54/parser.rb', line 9

def number
  @number
end

Class Method Details

.debug(sequence) ⇒ Object



69
70
71
72
73
# File 'lib/nt54/parser.rb', line 69

def self.debug(sequence)
  NT54.log       = Logger.new(STDOUT)
  NT54.log.level = Logger::DEBUG
  pp parse(sequence)
end

.parse(sequence) ⇒ Object



62
63
64
65
66
67
# File 'lib/nt54/parser.rb', line 62

def self.parse(sequence)
  parser = new
  sequence = sequence.to_s.gsub(/[^0-9+]/, '')
  sequence.split("").map {|x| parser.dial(x)}
  parser.number.fix!
end

.valid?(sequence) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/nt54/parser.rb', line 75

def self.valid?(sequence)
  number = parse(sequence)
  number.valid?
rescue RuntimeError
  false
end

Instance Method Details

#dial(keypress) ⇒ Object



87
88
89
# File 'lib/nt54/parser.rb', line 87

def dial(keypress)
  respond_to @visitor.accept(keypress)
end