Add count on repeat when at least one occurence is required

develop
Guillaume DOTT 2013-10-03 11:14:28 +02:00
parent 3656fd6d82
commit 926d7ec544
4 changed files with 18 additions and 14 deletions

View File

@ -26,11 +26,11 @@ module Biju
rule(:mgl) do
(str('+CMGL').as(:cmd) >> str(': ') >> infos >> crlf >> message >> crlf)
.repeat.as(:sms) >> crlf
.repeat(1).as(:sms) >> crlf
end
rule(:num) do
(str('+CNUM').as(:cmd) >> str(': ') >> array >> crlf)
.repeat.as(:phone_numbers) >> crlf >> crlf
.repeat(1).as(:phone_numbers) >> crlf >> crlf
end
rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf
@ -42,7 +42,7 @@ module Biju
str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf
end
rule(:generic_response) do
match('[^:]').repeat.as(:cmd) >> str(': ') >> array >>
match('[^:]').repeat(1).as(:cmd) >> str(': ') >> array >>
crlf >> crlf
end
@ -52,7 +52,7 @@ module Biju
rule(:data) { (str('(') >> array >> str(')')) | info }
rule(:infos) { (info >> (comma >> info).repeat).as(:infos) }
rule(:info) { datetime | string | int | empty_string }
rule(:message) { match('[0-9A-Fa-f]').repeat.as(:message) }
rule(:message) { match('[0-9A-Fa-f]').repeat(1).as(:message) }
# MISC
rule(:status) { (ok | error).as(:status) }
@ -97,7 +97,9 @@ module Biju
rule(int: simple(:int)) { int.to_i }
rule(boolean: simple(:boolean)) { boolean.to_i > 0 }
rule(string: simple(:string)) { string.to_s }
rule(datetime: simple(:datetime)) { DateTime.strptime(datetime.to_s, "%y/%m/%d,%T%Z") }
rule(datetime: simple(:datetime)) do
DateTime.strptime(datetime.to_s, '%y/%m/%d,%T%Z')
end
rule(array: subtree(:array)) { array }
rule(status: simple(:status)) { { status: status } }

View File

@ -24,13 +24,13 @@ module Biju
'%02x' % first_octet.binary,
# TP-Message-Reference
'00',
"%02x" % phone_number.length,
"%02x" % phone_number.type_of_address.hex,
'%02x' % phone_number.length,
'%02x' % phone_number.type_of_address.hex,
phone_number.number,
# TP-PID: Protocol identifier
'00',
"%02x" % user_data.encoding.hex,
"%02x" % user_data.length,
'%02x' % user_data.encoding.hex,
'%02x' % user_data.length,
user_data.message
].join
end

View File

@ -9,7 +9,9 @@ module Biju
def self.encode(string)
[
string.encode('UCS-2BE').chars.map { |char| "%04x" % char.ord }.join,
string.encode('UCS-2BE').chars.map do |char|
'%04x' % char.ord
end.join,
length: string.length * 2,
]
end