From e1e7be5d9851a479619b9e6c2657f1519207c7cb Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 6 Nov 2013 17:26:26 +0100 Subject: [PATCH] Add compatibility with ruby 1.9.3 --- lib/biju/modem.rb | 5 ++++- lib/biju/pdu.rb | 4 +++- lib/biju/pdu/encoding/gsm7bit.rb | 10 ++++++++-- lib/biju/pdu/encoding/ucs2.rb | 4 +++- lib/biju/pdu/phone_number.rb | 8 ++++++-- lib/biju/pdu/user_data.rb | 17 ++++++++++------- spec/biju/pdu/data_coding_scheme_spec.rb | 1 + spec/biju/pdu/encoding/gsm7bit_spec.rb | 1 + spec/biju/pdu/encoding/ucs2_spec.rb | 1 + 9 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/biju/modem.rb b/lib/biju/modem.rb index 8312688..8d54b46 100644 --- a/lib/biju/modem.rb +++ b/lib/biju/modem.rb @@ -26,7 +26,10 @@ module Biju wait(length: 0, timeout: 0) end - def wait(length: 0, timeout: 10) + def wait(options = {}) + length = options[:length] || 0 + timeout = options[:timeout] || 10 + buffer = '' Timeout.timeout(timeout) do while IO.select([connection], [], [], 0.25) || buffer.length < length diff --git a/lib/biju/pdu.rb b/lib/biju/pdu.rb index d6a7ba3..56d63e3 100644 --- a/lib/biju/pdu.rb +++ b/lib/biju/pdu.rb @@ -11,7 +11,9 @@ require 'biju/pdu/type_of_address' module Biju module PDU - def self.encode(phone_number, message, type_of_address: :international) + def self.encode(phone_number, message, options = {}) + type_of_address = options[:type_of_address] || :international + phone_number = PhoneNumber.encode(phone_number) user_data = UserData.encode(message) first_octet = FirstOctet.new.message_type_indicator!(:sms_submit) diff --git a/lib/biju/pdu/encoding/gsm7bit.rb b/lib/biju/pdu/encoding/gsm7bit.rb index 242c612..7a3543a 100644 --- a/lib/biju/pdu/encoding/gsm7bit.rb +++ b/lib/biju/pdu/encoding/gsm7bit.rb @@ -1,3 +1,5 @@ +# encoding: UTF-8 + module Biju module PDU module Encoding @@ -28,7 +30,9 @@ module Biju 0x65 => '€', } - def self.decode(string, length: 0) + def self.decode(string, options = {}) + length = options[:length] || 0 + res = '' next_char = 0 current_length = 0 @@ -98,7 +102,9 @@ module Biju end end - def self.get_septet(char, escape: false) + def self.get_septet(char, options = {}) + escape = options[:escape] || false + char = (!escape ? BASIC_7BIT_CHARACTER_SET.index(char) : BASIC_7BIT_CHARACTER_SET_EXTENSION.key(char)) return nil unless char diff --git a/lib/biju/pdu/encoding/ucs2.rb b/lib/biju/pdu/encoding/ucs2.rb index a5360f6..04cd4a5 100644 --- a/lib/biju/pdu/encoding/ucs2.rb +++ b/lib/biju/pdu/encoding/ucs2.rb @@ -2,7 +2,9 @@ module Biju module PDU module Encoding class UCS2 - def self.decode(string, length: 0) + def self.decode(string, options = {}) + length = options[:length] || 0 + string.scan(/.{4}/).map { |char| char.hex.chr('UCS-2BE') }.join .encode('UTF-8', 'UCS-2BE') end diff --git a/lib/biju/pdu/phone_number.rb b/lib/biju/pdu/phone_number.rb index 4794af6..40e4279 100644 --- a/lib/biju/pdu/phone_number.rb +++ b/lib/biju/pdu/phone_number.rb @@ -3,7 +3,9 @@ module Biju class PhoneNumber attr_accessor :type_of_address, :number - def self.encode(number, type_of_address: :international) + def self.encode(number, options = {}) + type_of_address = options[:type_of_address] || :international + number = number + 'F' if number.length.odd? new( number.scan(/../).map(&:reverse).join, @@ -11,7 +13,9 @@ module Biju ) end - def initialize(number, type_of_address: :international) + def initialize(number, options = {}) + type_of_address = options[:type_of_address] || :international + self.number = number self.type_of_address = TypeOfAddress.new(type_of_address) end diff --git a/lib/biju/pdu/user_data.rb b/lib/biju/pdu/user_data.rb index 0614eb2..c143751 100644 --- a/lib/biju/pdu/user_data.rb +++ b/lib/biju/pdu/user_data.rb @@ -6,10 +6,10 @@ module Biju ucs2: Encoding::UCS2, } - attr_accessor :encoding, :message, :length + attr_accessor :encoding, :message, :length, :user_data_header - def self.encode(message, encoding: nil) - encoding = DataCodingScheme.autodetect(message) if encoding.nil? + def self.encode(message, options = {}) + encoding = options[:encoding] || DataCodingScheme.autodetect(message) raise ArgumentError, 'Unknown encoding' unless ENCODING.has_key?(encoding) res = ENCODING[encoding].encode(message) @@ -17,10 +17,13 @@ module Biju new(message: res[0], length: res[1][:length], encoding: encoding) end - def initialize(message: '', length: 0, encoding: :gsm7bit) - self.encoding = DataCodingScheme.new(encoding) - self.message = message - self.length = length + def initialize(options = {}) + self.encoding = DataCodingScheme.new(options[:encoding] || :gsm7bit) + self.message = options[:message] || '' + + self.length = options[:length] || 0 + + self.user_data_header = options[:user_data_header] || false end def decode diff --git a/spec/biju/pdu/data_coding_scheme_spec.rb b/spec/biju/pdu/data_coding_scheme_spec.rb index 184a23a..b482dfa 100644 --- a/spec/biju/pdu/data_coding_scheme_spec.rb +++ b/spec/biju/pdu/data_coding_scheme_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'spec_helper' require 'biju/pdu' diff --git a/spec/biju/pdu/encoding/gsm7bit_spec.rb b/spec/biju/pdu/encoding/gsm7bit_spec.rb index f2ffed6..dd470ac 100644 --- a/spec/biju/pdu/encoding/gsm7bit_spec.rb +++ b/spec/biju/pdu/encoding/gsm7bit_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'spec_helper' require 'biju/pdu/encoding/gsm7bit' diff --git a/spec/biju/pdu/encoding/ucs2_spec.rb b/spec/biju/pdu/encoding/ucs2_spec.rb index dfe50c2..dd6dec3 100644 --- a/spec/biju/pdu/encoding/ucs2_spec.rb +++ b/spec/biju/pdu/encoding/ucs2_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'spec_helper' require 'biju/pdu/encoding/ucs2'