Clean Biju::PDU.decode method

develop
Guillaume DOTT 2013-10-01 15:08:29 +02:00
parent 4e8463e0dc
commit a3a9c43d8b
3 changed files with 39 additions and 19 deletions

View File

@ -36,27 +36,45 @@ module Biju
end end
def self.decode(string) def self.decode(string)
res = { octets = string.scan(/../)
smsc_length: string[0..1],
smsc_type: string[2..3],
smsc_number: string[4..15],
address_length: string[18..19].to_i(16), smsc_length = octets.shift.hex
address_type: string[20..21], smsc_number = octets.shift(smsc_length)
protocol_identifier: string[34..35], first_octet = FirstOctet.new(octets.shift.hex)
data_coding_scheme: string[36..37],
timestamp: Timestamp.new(string[38, 14]).to_datetime, address_length = octets.shift.hex
user_data_length: string[52..53], address_type = octets.shift.hex
sender_number = PhoneNumber.new(
octets.shift(
(address_length.odd? ? address_length.succ : address_length) / 2).join,
type_of_address: address_type)
protocol_identifier = octets.shift
data_coding_scheme = octets.shift
timestamp = Timestamp.new(octets.shift(7).join).to_datetime
user_data_length = octets.shift.hex
user_data = UserData.new(message: octets.join,
encoding: data_coding_scheme,
length: user_data_length)
{
smsc_length: smsc_length,
smsc_number: smsc_number,
first_octet: first_octet,
address_length: address_length,
address_type: address_type,
sender_number: sender_number,
protocol_identifier: protocol_identifier,
data_coding_scheme: data_coding_scheme,
timestamp: timestamp,
user_data_length: user_data_length,
user_data: user_data
} }
res[:sender_number] = PhoneNumber.new(
string[22, res[:address_length] + (res[:address_length].odd? ? 1 : 0)],
type_of_address: res[:address_type])
res[:user_data] = UserData.new(message: string[54..-1],
encoding: res[:data_coding_scheme],
length: res[:user_data_length].hex)
res
end end
end end
end end

View File

@ -22,7 +22,7 @@ module Biju
def length def length
# If the last character is 0xF, remove this one from length # If the last character is 0xF, remove this one from length
number.length - (number[-2, 2].hex >> 4 == 15 ? 1 : 0) number.length - (number[-2].hex == 15 ? 1 : 0)
end end
end end
end end

View File

@ -9,6 +9,8 @@ module Biju
} }
def initialize(type_of_address, options = {}) def initialize(type_of_address, options = {})
type_of_address = :international if type_of_address.nil?
unless type_of_address.is_a?(Symbol) unless type_of_address.is_a?(Symbol)
type_of_address = type_of_address.hex if type_of_address.is_a?(String) type_of_address = type_of_address.hex if type_of_address.is_a?(String)
type_of_address = TYPE_OF_ADDRESS.key(type_of_address) type_of_address = TYPE_OF_ADDRESS.key(type_of_address)