lib/esendex4soap.rb in esendex4soap-0.6.1 vs lib/esendex4soap.rb in esendex4soap-0.7.0

- old
+ new

@@ -1,11 +1,47 @@ # -*- encoding : utf-8 -*- require 'savon' require 'log4r' -require 'wtf_lang' +#require 'wtf_lang' +require 'api_smith' +module LanguageTools + class DetectLanguageDotCom + include APISmith::Client + base_uri "http://ws.detectlanguage.com" + endpoint "0.2" + + def initialize(api_key) + @api_key = api_key + add_query_options! :key => @api_key + end + + def detect(text) + (post "detect", :extra_query => {:q => text})["data"]["detections"].first["language"] + end + + end + + class AlchemyLanguage + include APISmith::Client + base_uri "http://access.alchemyapi.com/" + endpoint "calls/text" + + def initialize(api_key) + @api_key = api_key + add_query_options! :outputMode => 'json', :apikey => @api_key + end + + def detect(text) + (post "TextGetLanguage", :extra_query => {:text => text})["language"] + end + end + +end + module Esendex + class Client include Log4r 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' ACCOUNT_SERVICE_WSDL = 'https://www.esendex.com/secure/messenger/soap/AccountService.asmx?wsdl' @@ -15,39 +51,42 @@ def initialize(*args) @username = args.shift @password = args.shift @account_reference = args.shift + # @detectlanguage_api_key, @alchemyapi_api_key = args.shift, args.shift @sent_message_ids, @sent_message_statuses = [], {} + @language = LanguageTools::DetectLanguageDotCom.new(args.shift) + Savon.configure do |config| config.raise_errors = true config.log = false config.log_level = :error HTTPI.log = false end @log = Logger.new "#{self.class.name}" @log.outputters = Outputter.stdout - @log.level = ERROR + @log.level = DEBUG #ERROR @message_kind = String.new end def send_message(recipient, text, originator, validityperiod) - @log.debug "TO:#{recipient} text size:#{text.size} in #{text.full_lang} language" + @log.debug "TO:#{recipient} text size:#{text.size} in #{@language.detect(text)} language" #TODO: handle other languages as well - unless text.full_lang == 'russian' - sms_parts = text.scan(/.{610}/mu) - sms_parts << text[sms_parts.to_s.size..text.size] + unless @language.detect(text) == 'ru' + sms_parts = text.scan(/.{1,160}/mu) + @log.debug "#{text[sms_parts.to_s.size..text.size]}" @message_kind = 'Text' @log.debug "#{@message_kind}:#{text.scan(/./mu)} = #{text.size}. #{sms_parts.size} part/s." sms(recipient, sms_parts, originator, validityperiod) else - sms_parts = text.scan(/.{70}/mu) - sms_parts << text[sms_parts.to_s.size..text.size] + sms_parts = text.scan(/.{1,70}/mu) + @log.debug "#{text[sms_parts.to_s.size..text.size]}" @message_kind = 'Unicode' @log.debug "#{@message_kind}:#{text.scan(/./mu)} != #{text.size}. #{sms_parts.size} part/s." sms(recipient, sms_parts, originator, validityperiod) end end @@ -118,7 +157,7 @@ end #module Client end #module Esendex -#sms = Esendex::Client.new 'username', 'password', 'account_reference', 'from', 'expire int hours' -#sms.send_message 'PHONENUMBER', 'message in any language' +#sms = Esendex::Client.new 'username', 'password', 'account_reference', 'api_key for detectlanguage' +#sms.send_message 'PHONENUMBER', 'message in any language', 'FROM', 'expire int hours'