class Smess::Clickatell

Attributes

api_id[RW]
pass[RW]
sender_id[RW]
sender_ids[RW]
sms[R]
user[RW]

Public Class Methods

new(config) click to toggle source
Calls superclass method Smess::Output::new
# File lib/smess/outputs/clickatell.rb, line 29
def initialize(config)
  super
  ::Clickatell::API.debug_mode = true
  ::Clickatell::API.secure_mode = true
end

Public Instance Methods

deliver() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 35
def deliver
  begin
    responses = []
    messages.each do |msg|
      rsp = api.send_message(sms.to, msg.encode('ISO-8859-1'), {from: from, concat: 3, callback: 7})
      responses << rsp
    end
    result = normal_result(responses.first)
  rescue => e
    # connection problem or some error
    result = result_for_error(e)
  end
  result
end
validate_config() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 51
def validate_config
  @api_id     = config.fetch(:api_id)
  @user       = config.fetch(:user)
  @pass       = config.fetch(:pass)
  @sender_id  = config.fetch(:sender_id)
  @sender_ids = config.fetch(:sender_ids)
end

Private Instance Methods

api() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 85
def api
  @api ||= ::Clickatell::API.authenticate(api_id, user, pass)
end
concat_not_supported() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 81
def concat_not_supported
  sms.to[0] == "1" # USA
end
from() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 63
def from
  return nil if sender_not_supported
  sender_ids.split(",").include?(sms.originator) ? sms.originator : sender_id
end
messages() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 68
def messages
  msg = sms.message.strip_nongsm_chars
  concat_not_supported ? Smess.separate_sms(msg) : [msg]
end
normal_result(response) click to toggle source
# File lib/smess/outputs/clickatell.rb, line 89
def normal_result(response)
  # Successful response
  {
    message_id: response['ID'],
    response_code: '0',
    response: response,
    destination_address: sms.to,
    data: result_data
  }
end
result_data() click to toggle source
# File lib/smess/outputs/clickatell.rb, line 112
def result_data
  {
    to: sms.to,
    text: sms.message.strip_nongsm_chars,
    from: from
  }
end
result_for_error(e) click to toggle source
# File lib/smess/outputs/clickatell.rb, line 100
def result_for_error(e)
  {
    response_code: '-1',
    response: {
      temporaryError: 'true',
      responseCode: '-1',
      responseText: e.message
    },
    data: result_data
  }
end
sender_not_supported() click to toggle source

“feature detection” Clickatell's API requires knowledge of country-specific quirks and feature support. Supported features can and does change without notice, breaking some countries.

# File lib/smess/outputs/clickatell.rb, line 76
def sender_not_supported
  sms.to[0] == "1" || # USA
  sms.to[0..2] == "962" || # Jordan
  sms.to[0..2] == "971" # UAE
end