Class: Rumeme::SmsReply

Inherits:
Object
  • Object
show all
Defined in:
lib/rumeme/sms_reply.rb

Overview

This class represents an SMS reply.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone_number, message, message_id, _when, status) ⇒ SmsReply

Constructor.



7
8
9
# File 'lib/rumeme/sms_reply.rb', line 7

def initialize phone_number, message, message_id, _when, status
  @phone_number, @message, @message_id, @when, @status = phone_number, message, message_id, _when, status
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/rumeme/sms_reply.rb', line 4

def message
  @message
end

#message_idObject (readonly)

Returns the value of attribute message_id.



4
5
6
# File 'lib/rumeme/sms_reply.rb', line 4

def message_id
  @message_id
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



4
5
6
# File 'lib/rumeme/sms_reply.rb', line 4

def phone_number
  @phone_number
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/rumeme/sms_reply.rb', line 4

def status
  @status
end

#whenObject (readonly)

Returns the value of attribute when.



4
5
6
# File 'lib/rumeme/sms_reply.rb', line 4

def when
  @when
end

Class Method Details

.parse(line) ⇒ Object

Parse a reply from a string. Format is: messageID phone when message /(d+)s(d+)s(d+)s(.+)/ Or if no message ID: phone when message /(d+)s(d+)s(.+)/ Or if delivery receipt: messageID messageStatus when /(d+)s(d)s(d+)/ current implementation ignores use_message_id setting (as original code)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rumeme/sms_reply.rb', line 22

def parse line
  # p "parsing line: #{line}"

  message_id, status, message, phone, when_ = case line
    when /^(\d+)\s(\d)\s(\d+)/
      #process delivery report
      [$1.to_i, $2.to_i, nil, nil, $3.to_i]
    when /^(\d+)\s\+?(\d+)\s(\d+)\s(.+)/
      #process message with id
      [$1.to_i, MessageStatus::NONE, unescape($4), $2, $3.to_i]
    when /^\+?(\d+)\s(\d+)\s(.+)/
      #process message without id
      [nil, MessageStatus::NONE, unescape($3), $1, $2.to_i]
    else
      raise ArgumentError.new("can't parse line: #{line}")
  end

  return SmsReply.new(phone, message, message_id, when_, status)
end

.unescape(line) ⇒ Object

Unescape any escaped characters in the string.



13
14
15
# File 'lib/rumeme/sms_reply.rb', line 13

def unescape line
  line.gsub('\n', "\n").gsub('\r', "\r").gsub('\\\\', "\\") if line
end

Instance Method Details

#delivery_report?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rumeme/sms_reply.rb', line 43

def delivery_report?
  @status != MessageStatus::NONE
end