Module: SmsOnRails::ModelSupport::Draft::InstanceMethods

Defined in:
lib/sms_on_rails/model_support/draft.rb

Instance Method Summary collapse

Instance Method Details

#actual_messageObject

if there is only one outbound message, return the actual (substituted) message. Otherwise, returns the draft message with substituted strings if any



164
165
166
# File 'lib/sms_on_rails/model_support/draft.rb', line 164

def actual_message
  self.outbounds.length == 1 ? outbounds.first.actual_message : message
end

#complete_messageObject

The complete message with header and footer



118
119
120
121
122
123
124
# File 'lib/sms_on_rails/model_support/draft.rb', line 118

def complete_message
  complete_message = ""
  complete_message << "#{header}\n" unless header.blank?
  complete_message << message
  complete_message << "\n#{footer}" unless footer.blank?
  complete_message
end

#create_outbounds_for_phone_numbers(phone_numbers, options = {}) ⇒ Object

Create Outbound Instances based on phone_numbers and options Refer to SmsOnRails::ModelSupport::Outbound.create_outbounds_from_phone



158
159
160
# File 'lib/sms_on_rails/model_support/draft.rb', line 158

def create_outbounds_for_phone_numbers(phone_numbers, options={})
  self.outbounds = self.class.reflections[:outbounds].klass.create_outbounds_for_phone_numbers(phone_numbers, options)
end

#default_headers_and_footersObject

default the headers and footers



112
113
114
115
# File 'lib/sms_on_rails/model_support/draft.rb', line 112

def default_headers_and_footers
  self.header ||= self.class.default_header
  self.footer ||= self.class.default_footer
end

#deliver(options = {}) ⇒ Object

Deliver all the unsent outbound messages error - set this option to add an error message to the draft object

This is locked and safe for individual messages but the draft object itself is not locked down, so it can be processed by multiple threads



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sms_on_rails/model_support/draft.rb', line 133

def deliver(options={})
  options||={}
  deliver_method = options.delete(:bang) ? :deliver! : :deliver

  error_messages = outbounds.inject([]) do |error_messages, o|
    next(error_messages) if o.delivered?
    unless o.send(deliver_method, options)
     error_messages << "Message could not be delivered to: #{o.phone_number.human_display}"
    end
    error_messages
  end
     
  self.update_attribute(:status, error_messages.blank? ?  'PROCESSED' : 'FAILED')
  error_messages.each { |msg| errors.add_to_base(msg) }
  error_messages.blank?
end

#deliver!(options = {}) ⇒ Object

Deliver all outbound messages safely using optimisitic locking Progates any exception thrown



152
153
154
# File 'lib/sms_on_rails/model_support/draft.rb', line 152

def deliver!(options={})
  deliver((options||{}).merge(:bang => true))
end

#initialize(args = {}) ⇒ Object

:nodoc:



92
93
94
95
# File 'lib/sms_on_rails/model_support/draft.rb', line 92

def initialize(args={})#:nodoc:
  super args
  default_headers_and_footers
end

#max_message_lengthObject



109
# File 'lib/sms_on_rails/model_support/draft.rb', line 109

def max_message_length; self.class.max_message_length; end

#message_lengthObject

The length of the message This does not take into consideration substituted params TODO: adjust max length for substituted params



108
# File 'lib/sms_on_rails/model_support/draft.rb', line 108

def message_length; complete_message.length; end

#save_and_deliver(options = {}) ⇒ Object



97
98
99
# File 'lib/sms_on_rails/model_support/draft.rb', line 97

def save_and_deliver(options={})
  save and deliver(options)
end

#save_and_deliver!(options = {}) ⇒ Object



101
102
103
# File 'lib/sms_on_rails/model_support/draft.rb', line 101

def save_and_deliver!(options={})
  save! and deliver!(options)
end