Class: SimCard::ReceivedSmsMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/sim_card/received_sms_message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_id, sender_number, timestamp, text) ⇒ ReceivedSmsMessage

  • message_id : ID of the sms message as provided by the SIM card

  • sender_number : who sent the message

  • timestamp : time of message arrival

  • text : message text



34
35
36
37
38
39
# File 'lib/sim_card/received_sms_message.rb', line 34

def initialize message_id, sender_number, timestamp, text
  @message_id = message_id
  @sender_number = sender_number
  @timestamp = timestamp
  @text = text
end

Instance Attribute Details

#message_idObject (readonly)

Returns the value of attribute message_id.



28
29
30
# File 'lib/sim_card/received_sms_message.rb', line 28

def message_id
  @message_id
end

#sender_numberObject (readonly)

Returns the value of attribute sender_number.



28
29
30
# File 'lib/sim_card/received_sms_message.rb', line 28

def sender_number
  @sender_number
end

#textObject (readonly)

Returns the value of attribute text.



28
29
30
# File 'lib/sim_card/received_sms_message.rb', line 28

def text
  @text
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



28
29
30
# File 'lib/sim_card/received_sms_message.rb', line 28

def timestamp
  @timestamp
end

Class Method Details

.load_messages(at_command_sender) ⇒ Object

parse raw output from SIM card and return list of ReceivedSmsMessage instances. see SimCardTest for examples of raw SIM output.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sim_card/received_sms_message.rb', line 6

def self.load_messages  at_command_sender
  raw_sim_output = at_command_sender.send "AT+CMGL=\"ALL\""
  messages = []
  raw_input2 = raw_sim_output[14..-1] # remove initial AT+CMGL="ALL"\n
  raw_input3 = (raw_input2 || '').split('+CMGL: ')[1..-1]
  
  (raw_input3 || []).each do |raw_message|
    header, *text_lines = raw_message.split("\n")
    
    # +CMGL: 2,"REC READ","+421918987987","","13/08/20,19:00:44+08"
    message_id, _, sender_number, _, date, time = header.gsub("\"", '').split(',')
    
    timestamp = DateTime.strptime (date + ' ' + time), '%y/%m/%d %H:%M:%S'
    message_text = text_lines.join "\n"
    
    sms_message = ReceivedSmsMessage.new message_id, sender_number, timestamp, message_text
    messages << sms_message
  end
  
  return messages.reverse
end

Instance Method Details

#to_sObject



41
42
43
44
45
46
47
48
# File 'lib/sim_card/received_sms_message.rb', line 41

def to_s
  <<STRING
message id: #{@message_id}
sender: #{@sender_number}
timestamp: #{@timestamp}
#{@text}
STRING
end