From a3a9c43d8b877d20a6b50bff107debf8f473f8d3 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Tue, 1 Oct 2013 15:08:29 +0200 Subject: [PATCH] Clean Biju::PDU.decode method --- lib/biju/pdu.rb | 54 ++++++++++++++++++++++----------- lib/biju/pdu/phone_number.rb | 2 +- lib/biju/pdu/type_of_address.rb | 2 ++ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/lib/biju/pdu.rb b/lib/biju/pdu.rb index e0c1faf..15b40d4 100644 --- a/lib/biju/pdu.rb +++ b/lib/biju/pdu.rb @@ -36,27 +36,45 @@ module Biju end def self.decode(string) - res = { - smsc_length: string[0..1], - smsc_type: string[2..3], - smsc_number: string[4..15], + octets = string.scan(/../) - address_length: string[18..19].to_i(16), - address_type: string[20..21], + smsc_length = octets.shift.hex + smsc_number = octets.shift(smsc_length) - protocol_identifier: string[34..35], - data_coding_scheme: string[36..37], - timestamp: Timestamp.new(string[38, 14]).to_datetime, - user_data_length: string[52..53], + first_octet = FirstOctet.new(octets.shift.hex) + + address_length = octets.shift.hex + 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 diff --git a/lib/biju/pdu/phone_number.rb b/lib/biju/pdu/phone_number.rb index f8f5254..4794af6 100644 --- a/lib/biju/pdu/phone_number.rb +++ b/lib/biju/pdu/phone_number.rb @@ -22,7 +22,7 @@ module Biju def 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 diff --git a/lib/biju/pdu/type_of_address.rb b/lib/biju/pdu/type_of_address.rb index d0d7723..6b7d730 100644 --- a/lib/biju/pdu/type_of_address.rb +++ b/lib/biju/pdu/type_of_address.rb @@ -9,6 +9,8 @@ module Biju } def initialize(type_of_address, options = {}) + type_of_address = :international if type_of_address.nil? + 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.key(type_of_address)