Class: Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/smsruby/send.rb

Overview

The Sender class represent the send layer. Reference a set of strategys wich will perform the same function but in a diferent way, that is, gather all the information required and comunicate with the Admconnection layer. This class it’s called the context class, an acts as the user of the strategys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sendtype, location = 'config_sms.yml') ⇒ Sender

Obtains an instance of the Connection Administrator to control all existing connections. Due to the use of a singleton in the administrator, the same created instance will be obtain, or will be created if an instance doesn’t exist. It also establish the strategy to be use acording to the specified by the user. An Exception is thrown if the instance of Admconnection fail



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smsruby/send.rb', line 67

def initialize(sendtype,location= 'config_sms.yml')
  begin
    @adm = AdmConnection.instance
    @location=location
    @sendtype=sendtype
    @report = 0
    @smsc = nil
    @validity = '0'
    @dst =[]
  end
end

Instance Attribute Details

#admObject (readonly)

Reference an instance for the Connection Administrator



15
16
17
# File 'lib/smsruby/send.rb', line 15

def adm
  @adm
end

#dstObject

Specify the destination number(s) to deliver the sms



17
18
19
# File 'lib/smsruby/send.rb', line 17

def dst
  @dst
end

#locationObject (readonly)

Specify the location of the file used to read send configuration



27
28
29
# File 'lib/smsruby/send.rb', line 27

def location
  @location
end

#msjObject (readonly)

Specify the messaje that will be deliver to destiny



19
20
21
# File 'lib/smsruby/send.rb', line 19

def msj
  @msj
end

#reportObject

Specify whether a delivered report will be generated or not (1: yes, 0: no)



21
22
23
# File 'lib/smsruby/send.rb', line 21

def report
  @report
end

#sendtypeObject

Reference the type of send that will be used to set the parameters and deliver the message



29
30
31
# File 'lib/smsruby/send.rb', line 29

def sendtype
  @sendtype
end

#smscObject

Specify a particular number for the smsc. If not specifyed default number will be used



23
24
25
# File 'lib/smsruby/send.rb', line 23

def smsc
  @smsc
end

#validityObject

Specify the time of validity in minutos of the delivered sms



25
26
27
# File 'lib/smsruby/send.rb', line 25

def validity
  @validity
end

Instance Method Details

#send(msj) ⇒ Object

Represent the send method for the Send class. The message to be send is passed as the only parameter and depending on the choosen strategy the other option values will be obtain diferently. The instance of the context class will be passed to the strategy, and the send function will be executed only if an instance of the Connection Administrator exist.



108
109
110
111
112
113
114
115
116
117
# File 'lib/smsruby/send.rb', line 108

def send(msj)
  begin
    @msj = msj
    !@adm.avlconn ? (raise "There are no active connections") : t=Thread.new{@sendtype.send(self){|e| yield e if block_given?}}
    t[:type]='sp'
  rescue Exception => e
    @adm.log.error "Error sending message :: #{e.message}" unless @adm.log.nil?
    raise e.message
  end
end

#setconfig(dst, smsc, report, validity) ⇒ Object

Establish the option values to be use for sending the SMS message



82
83
84
85
86
87
# File 'lib/smsruby/send.rb', line 82

def setconfig(dst,smsc,report,validity)
  @dst=dst
  @smsc=smsc
  @report=report
  @validity=validity
end

#to_hashObject

Combine all option values into a hash to relate them



92
93
94
95
96
97
98
99
# File 'lib/smsruby/send.rb', line 92

def to_hash
  { :type => 'send',
    :dst => self.dst,
    :msj => self.msj,
    :smsc => self.smsc,
    :report => self.report,
    :validity => self.validity}
end