Add generic rule for responses in parser

develop
Guillaume DOTT 2013-10-01 15:01:27 +02:00
parent 001cbacfca
commit a0757a9c69
2 changed files with 19 additions and 5 deletions

View File

@ -17,7 +17,7 @@ module Biju
# RESPONSE
rule(:response) { ((command.maybe >> status) | merror) >> crlf | prompt }
rule(:prompt) { str('> ').as(:prompt) }
rule(:command) { mgl | num | pin | pms | mgf | mgs }
rule(:command) { mgl | num | pin | mgf | mgs | generic_response }
rule(:merror) do
(str('+CME ERROR') | str('+CMS ERROR')).as(:cmd) >> str(': ') >>
@ -32,10 +32,6 @@ module Biju
(str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf)
.repeat.as(:phone_numbers) >> crlf >> crlf
end
rule(:pms) do
str('+CPMS').as(:cmd) >> str(': ') >> str('(').maybe >> array >> str(')').maybe >>
crlf >> crlf
end
rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf
end
@ -45,6 +41,10 @@ module Biju
rule(:mgs) do
str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf
end
rule(:generic_response) do
match('[^:]').repeat.as(:cmd) >> str(': ') >> array >>
crlf >> crlf
end
rule(:array) do
(data >> (comma >> data).repeat).as(:array)

View File

@ -33,6 +33,20 @@ describe Biju::ATParser do
end
context "response" do
it "parses generic response" do
resp = "AT+COPS\r\r\n+COPS: 1,\"two\",(3,4)\r\n\r\nOK\r\n"
result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(resp))
expect(result[:cmd]).to eq('+COPS')
expect(result[:array]).to have(3).fields
expect(result[:array]).to include(1)
expect(result[:array]).to include('two')
expect(result[:array]).to include([3,4])
end
it "parses cmgs prompt" do
mgs = "AT+CMGS=18\r\r\n> "