summaryrefslogtreecommitdiff
path: root/contrib/smsweb/smswsc/sms_message.rb
blob: 787b58bb684dbc3e87d23cd6c5ac508bd36dc1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'smswsc/builder'
require 'rexml/document'
require 'base64'

module SMSWSC

  class SMSMessage
    attr_accessor :message_status
    attr_accessor :addr_type
    attr_accessor :addr
    attr_accessor :user_data
    attr_accessor :uri

    def initialize(options = {})
      @message_status = options["message_status"]
      @addr_type = options["addr-type"]
      @addr = options["addr"]
      @user_data = options["user-data"]
    end

    def uri=(value)
      @uri = value
    end

    def self.load_xml(parent)
      message = SMSMessage.new

      parent.each_element do |element|
        case element.name
        when "message-status"
          message.message_status = element.text.to_s
        when "addr-type"
          message.addr_type = element.text.to_s
        when "addr"
          message.addr = element.text.to_s
        when "user-data"
          message.user_data = Base64.decode64(element.text.to_s)
        when "uri"
          message.uri = URI.parse(element.text.to_s)
       end
      end

      return message
    end

    def to_xml(builder = nil, element = "sms-message")
      xml = ""
      unless builder
        builder = SMSWSC.new_builder(xml)
      end

      builder.tag!(element.to_s) {
        builder.tag! "message-status", @message_status
        builder.tag! "addr-type", @addr_type
        builder.tag! "addr", @addr
        builder.tag! "user-data", Base64.encode64(@user_data)
      }

      return xml
    end
  end
end