Class: SmscManager::Sms

Inherits:
Object
  • Object
show all
Includes:
StompMessage::XmlHelper
Defined in:
lib/smsc_manager/sms.rb

Overview

and strips out the unnecessary junk

Constant Summary collapse

@@MAX_SIZE =

3*160 # max 1500 characters, change if your smsc suports less

1500
@@msisdn_valid_prefix =
%w{ 63999 999 0999 1000 888 +63999 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, dest, src = '888') ⇒ Sms

Returns a new instance of Sms.

Raises:



33
34
35
36
37
38
39
# File 'lib/smsc_manager/sms.rb', line 33

def initialize(txt,dest,src='888')
#  self.orig_txt=txt
  self.destination=dest
  self.source=src
  self.text=strip_text(txt)
  raise InvalidPrefix.new("invalid prefix: valid prefix area:" + Sms.valid ) if !prefix_test
end

Instance Attribute Details

#destinationObject

, :orig_txt



14
15
16
# File 'lib/smsc_manager/sms.rb', line 14

def destination
  @destination
end

#sourceObject

, :orig_txt



14
15
16
# File 'lib/smsc_manager/sms.rb', line 14

def source
  @source
end

#textObject

, :orig_txt



14
15
16
# File 'lib/smsc_manager/sms.rb', line 14

def text
  @text
end

Class Method Details

.check_destination(dst) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/smsc_manager/sms.rb', line 43

def self.check_destination(dst)
  flag = false
  
  @@msisdn_valid_prefix.each { |w| k= Sms.starts_with? w , dst
                                 flag=true if k }
  flag
end

.load_xml(xml_string) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/smsc_manager/sms.rb', line 58

def self.load_xml(xml_string)
  doc=REXML::Document.new(xml_string)
  dest=REXML::XPath.first(doc, "//destination").text
  src=REXML::XPath.first(doc, "//source").text
  text=REXML::XPath.first(doc, "//text").text
  sms=SmscManager::Sms.new(text,dest,src)
end

.max_lengthObject



15
16
17
# File 'lib/smsc_manager/sms.rb', line 15

def self.max_length
  @@MAX_SIZE
end

.starts_with?(prefix, dest) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/smsc_manager/sms.rb', line 28

def self.starts_with?(prefix,dest)
  prefix = prefix.to_s
  return false if dest == nil
  dest[0, prefix.length] == prefix
end

.validObject

create new sms, limit the text, src is 1000



20
21
22
23
24
# File 'lib/smsc_manager/sms.rb', line 20

def self.valid
  sentence=" "
  @@msisdn_valid_prefix.each {|t| sentence << t <<" " }
  sentence
end

Instance Method Details

#get_message_contentObject



25
26
27
# File 'lib/smsc_manager/sms.rb', line 25

def get_message_content
  self.text
end

#limit_text(val) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/smsc_manager/sms.rb', line 65

def limit_text(val)
  #limit to @@MAX_SIZE characters
    ret=""
    size=[val.size,@@MAX_SIZE].min-1
 #   0.upto(size) do |i|
 #   ret << val[i]
 #   end
    ret=val[0..size]
  ret
end

#prefix_testObject



40
41
42
# File 'lib/smsc_manager/sms.rb', line 40

def prefix_test
  Sms.check_destination(self.destination)
end

#strip_text(txt) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/smsc_manager/sms.rb', line 75

def strip_text(txt) 
   # puts "text is #{txt} nil #{txt==nil}"
        txt= " " if txt==nil
        result=txt.gsub(/@/,"(at)")   # replace all at signs  return result
    #    result=result.gsub(/\n/," ")   # replace all new lines  return result
     #   result=result.gsub(/\r/," ")   # replace all new lines  return result
       result=result.gsub('_',"-")   # replace all new lines  return result
     #  result=result.gsub(/-/," ")   # replace all - with space...angelo bug return result
    #    result=result4.gsub(/\//,"")   # replace all new lines  return result
    
        limit_text(result)
end

#to_xmlObject



50
51
52
53
54
55
56
# File 'lib/smsc_manager/sms.rb', line 50

def to_xml
  msg_xml = REXML::Element.new "sms"
  msg_xml =self.add_elements(msg_xml)
  msg_xml.to_s
 # doc= REXML::Document.new sms_xml.to_s
 # doc.to_s
end