Class: SmsOnRails::ServiceProviders::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sms_on_rails/service_providers/base.rb

Direct Known Subclasses

Clickatell, Dummy, EmailGateway

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_service_provider(provider) ⇒ Object

Locate the service provider object by provider_id, string, symbol, or ServiceProvider object Defaults to the default service provider

SmsOnRails::ServiceProviders::Base.get_service_provider :dummy
SmsOnRails::ServiceProviders::Base.get_service_provider 1
SmsOnRails::ServiceProviders::Base.get_service_provider SmsOnRails::ServiceProviders::Dummy


209
210
211
212
213
214
215
216
217
218
# File 'lib/sms_on_rails/service_providers/base.rb', line 209

def get_service_provider(provider)
  case provider.class.to_s
   when 'Fixnum'           then provider_by_id(provider)
   when 'String'           then (provider.to_i > 0 ? provider_by_id(provider) : provider_by_name(provider))
   when 'Symbol'           then provider_by_name(provider)
   when 'NilClass'         then nil
   else
     provider
   end
end

.human_nameObject

Human name of provider



166
167
168
# File 'lib/sms_on_rails/service_providers/base.rb', line 166

def human_name
  @human_name ||= self.to_s.demodulize.titleize
end

.max_charactersObject



156
157
158
# File 'lib/sms_on_rails/service_providers/base.rb', line 156

def max_characters
  self.config[:max_characters]||140
end

.nameObject

Name of service provider (downcase no spaces)



161
162
163
# File 'lib/sms_on_rails/service_providers/base.rb', line 161

def name
  @name ||= self.to_s.demodulize.underscore
end

.provider_by_id(provider_id) ⇒ Object

Locate the service provider by provider id

Example: SmsOnRails::ServiceProviders::Base.provider_by_id 1


193
194
195
# File 'lib/sms_on_rails/service_providers/base.rb', line 193

def provider_by_id(provider_id)
  provider_map[provider_id.to_i] if provider_id.to_i > 0
end

.provider_by_name(provider_name) ⇒ Object

Locate the service provider by name or symbol

Example: SmsOnRails::ServiceProviders::Base.provider_by_name :dummy


199
200
201
202
# File 'lib/sms_on_rails/service_providers/base.rb', line 199

def provider_by_name(provider_name)
  key = provider_name.to_s.downcase
  provider_list.detect{|p| key == p.name || provider_name == p.human_name}
end

.provider_listObject

List of all providers available



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/sms_on_rails/service_providers/base.rb', line 179

def provider_list
  @provider_list ||= subclasses.inject([]) do |list, klass_name|
    klass = klass_name.kind_of?(String) ? klass_name.constantize : klass_name
    begin
      list << klass.instance
    rescue LoadError => exc
      logger.error "#{klass} load error: #{exc}"
    end
    list
  end
end

.provider_mapObject

Hash map of provider_id to the provider



171
172
173
174
175
176
# File 'lib/sms_on_rails/service_providers/base.rb', line 171

def provider_map
  @provider_map ||= provider_list.inject({}) do |map, klass|
    map[klass.provider_id] = klass unless klass.nil?
    map
  end
end

.set_default_service_provider(provider) ⇒ Object



149
150
151
152
153
154
# File 'lib/sms_on_rails/service_providers/base.rb', line 149

def set_default_service_provider(provider)
  return if $gems_rake_task
  provider_instance = get_service_provider(provider)
  raise SmsOnRails::FatalSmsError.new("Cannot set default provider to #{provider}. Check that support for #{provider} exists.") unless provider_instance
  self.default_service_provider = provider_instance
end

Instance Method Details

#deliverObject

Deliver all the sms messages marked with this as its sms service provider



42
43
44
# File 'lib/sms_on_rails/service_providers/base.rb', line 42

def deliver
  Outbound.deliver(delivery_options.merge(:conditions => ['sms_service_provider_id = ?', self.provider_id]))
end

#requires_carrier?Boolean

Returns:

  • (Boolean)


39
# File 'lib/sms_on_rails/service_providers/base.rb', line 39

def requires_carrier?; false; end

#send_message(phone_number, message, options = {}) ⇒ Object

Sends a message to a phone number directly to the provider. No validations are performed

  • phone_number the exact string text to send

  • message - the string message to send

  • options - additional options to be used by the provider



55
56
57
# File 'lib/sms_on_rails/service_providers/base.rb', line 55

def send_message(phone_number, message, options={})
  raise SmsOnRails::SmsError.new("Override send_message in subclass #{self.class}")
end

#send_sms(sms, options = {}) ⇒ Object

Send an Sms with validation sms is an sms active record object that responds to phone_number and full_message

Refer to send_to_phone for more infomation on validation



88
89
90
# File 'lib/sms_on_rails/service_providers/base.rb', line 88

def send_sms(sms, options={})
  send_to_phone(sms.phone_number, sms.actual_message, options)
end

#send_to_phone(phone_obj, message, options = {}) ⇒ Object

Sends a message to a phone number active record object or string and performs validation on the phone_number

  • phone_number can be either a phone number active record obj or a string text. Strings are converted to phone_number objects

  • message - the message to send

  • options - additional options to be used by the provider

Options

:skip_validation - skips validation and sends the message directly to the provider



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sms_on_rails/service_providers/base.rb', line 70

def send_to_phone(phone_obj, message, options={})

  if options[:skip_validation]
    phone_text = phone_obj.is_a?(ActiveRecord::Base) ? format_phone_number(phone_number) : phone_obj
    return send_message(phone_text, message, options)
  end

  phone_number = phone_obj.is_a?(ActiveRecord::Base) ? phone_obj :
                 find_or_create_phone_number(phone_obj, options)

  assert_message_options!(phone_number, message, options)
  send_message(format_phone_number(phone_number), message, options)
end