Class: Dovado::Router::Sms::Message

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/dovado/router/sms/message.rb

Overview

A text message (SMS).

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Message

Create a new Dovado::Router::Sms::Message object.

Parameters:

  • args (Hash) (defaults to: nil)

    arguments.

Options Hash (args):

  • :id (Integer, String, Symbol)

    Message Id.

  • :body (String)

    Message body.

  • :pdus (Array)

    Message PDU's.

  • :from (String)

    Message sender.

  • :sent (DateTime)

    Message send timestamp.

  • :encoding (Encoding)

    Message text encoding.

Since:

  • 1.0.0


40
41
42
43
44
45
46
47
48
49
# File 'lib/dovado/router/sms/message.rb', line 40

def initialize(args=nil)
  unless args.nil?
    @id = args[:id] unless args[:id].nil?
    @body = args[:body] unless args[:body].nil?
    @pdus = args[:pdus] unless args[:pdus].nil?
    @from = args[:from] unless args[:from].nil?
    @sent = args[:sent] unless args[:sent].nil?
    @encoding = args[:encoding] unless args[:encoding].nil?
  end
end

Instance Attribute Details

#bodyString (readonly)

Message body.

Returns:

  • (String)

Since:

  • 1.0.0


17
18
19
# File 'lib/dovado/router/sms/message.rb', line 17

def body
  @body
end

#encodingEncoding (readonly)

Message text encoding.

Returns:

  • (Encoding)

Since:

  • 1.0.0


29
30
31
# File 'lib/dovado/router/sms/message.rb', line 29

def encoding
  @encoding
end

#fromString (readonly)

Message sender.

Returns:

  • (String)

Since:

  • 1.0.0


23
24
25
# File 'lib/dovado/router/sms/message.rb', line 23

def from
  @from
end

#idString, ... (readonly)

Message Id.

Returns:

  • (String, Integer, Symbol)

Since:

  • 1.0.0


14
15
16
# File 'lib/dovado/router/sms/message.rb', line 14

def id
  @id
end

#pdusArray (readonly)

Message PDU's.

Returns:

  • (Array)

Since:

  • 1.0.0


20
21
22
# File 'lib/dovado/router/sms/message.rb', line 20

def pdus
  @pdus
end

#sentDateTime (readonly)

Message send timestamp.

Returns:

  • (DateTime)

Since:

  • 1.0.0


26
27
28
# File 'lib/dovado/router/sms/message.rb', line 26

def sent
  @sent
end

Class Method Details

.from_string(string = nil) ⇒ Message

Create a new Dovado::Router::Sms::Message object from a String.

Parameters:

  • string (String) (defaults to: nil)

    message data to create object from.

Returns:

Since:

  • 1.0.0


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dovado/router/sms/message.rb', line 55

def self.from_string(string=nil)
  hash = ThreadSafe::Cache.new
  message_body = ""
  array = string.split("\n")
  array.each do |row|
    row_array = row.split(':')
    if row_array.length == 2
      key = row_array[0].downcase
      val = row_array[1]
      case key.strip
      when 'from'
        hash[:from] = val.strip
      when 'alphabet'
        begin
          hash[:encoding] = Encoding.find(val.strip)
        rescue ArgumentError
          hash[:encoding] = Encoding::UTF_8
        end
      when 'id'
        hash[:id] = val.strip.to_i
      end
    elsif row_array.length > 2
      sent = row.match(/[Ss]ent\:\W(.*)/)
      hash[:sent] = DateTime.parse(sent[1].strip)
    else
      unless row.downcase =~ /end of sms/
        message_body += "#{row}\n"
      end
    end
  end
  hash[:body] = message_body.tr(">>", "").tr("\x17", "").strip.force_encoding(hash[:encoding])
    
  Message.new(hash)
end