# -*- encoding : utf-8 -*- require 'savon' module Esendex class Client INBOX_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/InboxService.asmx?wsdl' SEND_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/SendService.asmx?wsdl' attr_accessor :username, :password, :account_reference attr_reader :sent_message_ids def initialize(*args) @username = args.shift @password = args.shift @account_reference = args.shift @sent_message_ids = [] Savon.configure do |config| config.raise_errors = true config.log = false config.log_level = :error HTTPI.log = false end @message_kind = String.new end def send_message(recipient, text) if text.scan(/./mu).size == text.size sms_parts = text.scan(/.{610}/mu) sms_parts << text[sms_parts.to_s.size..text.size] @message_kind = 'Text' sms(recipient, sms_parts) else sms_parts = text.scan(/.{70}/mu) sms_parts << text[sms_parts.to_s.size..text.size] @message_kind = 'Unicode' sms(recipient, sms_parts) end end private def connect @client = Savon::Client.new(SEND_SERVICE_WSDL) #http://jira.codehaus.org/browse/JRUBY-5529 - jruby-openssl in --1.9 jruby mode @client.http.auth.ssl.verify_mode=(:none) end def sms(recipient, messages) connect messages.each do |message| resp = @client.request :com, :send_message_full do |soap| soap.header["com:MessengerHeader"] = {"com:Username" => @username, "com:Password" => @password, "com:Account" => @account_reference} soap.body = {"com:recipient" => recipient, "com:body" => message, "com:type" => @message_kind} end @sent_message_ids << resp.to_hash[:send_message_full_response][:send_message_full_result] end end end #module Client end #module Esendex #sms = Esendex::Client.new 'username', 'password', 'account_reference' #sms.send_message 'PHONENUMBER', 'message in any language'