class Mensario
A class that allow us to send a sms message through Mensario
SMS Service
Constants
- API_HOST
Api host
- API_PATH
Api path
- API_PORT
Api port
Public Class Methods
Do de api call with all data and process the response
@param [String] task Name of api task @param [Hash] data Hash containing all data @return [Hash] response hash
# File lib/mensario.rb, line 81 def self.api_call(task, data = {}) #Get config self::config unless @@config[:license] && @@config[:username] && @@config[:password] basic = { 'task' => ["#{task}"], 'license' => { 'number' =>@@config[:license], 'user' =>@@config[:username], 'pass' =>@@config[:password] } } xml = XmlSimple.xml_out(basic.merge(data), :rootname => 'api', :XmlDeclaration => '<?xml version="1.0" encoding="UTF-8"?>') begin http = Net::HTTP.new(API_HOST, API_PORT) http.use_ssl = true request = Net::HTTP::Post.new(API_PATH) request.body = xml response = http.request(request) rescue Exception => e raise MensarioHttpException e.message end result = XmlSimple.xml_in(response.body) raise MensarioApiException.new(result['result'].first), result['result'].first unless result['result'].first == 'OK' return result end
Allows to consult the balance remaining of the license @return [Integer] the balance remaining
# File lib/mensario.rb, line 191 def self.balance self::api_call('QUANT-QRY')['quantity'].first.to_i end
Load configuration and save it in @@config
@param [Hash] opts Options @option opts :config (‘config/mensario.yml’) Path to the configuration file @option opts :profile (:default) Profile in configuration to load
# File lib/mensario.rb, line 116 def self.config(opts = {}) file = opts[:config] || Dir.getwd + '/config/mensario.yml' config = YAML.load_file(file) profile = opts[:profile] || :default unless config[profile] raise MensarioException, "Profile doesn't exist in configuration file #{file}" end @@config = config[profile] end
Cancel the message which receives as a parameter @param [Integer] Message id to cancell @return [Boolean] Return ‘true’ if message is cancelled
and 'false' if the message can't be cancelled
# File lib/mensario.rb, line 179 def self.destroy(id) xml = { 'msg' => { 'id' => [id] } } self::api_call('MSG-CANC',xml)['cancel'].first.to_i == 1 end
Set the value of the license number
@param [String] license License number
# File lib/mensario.rb, line 44 def self.license=(license) @@config[:license] = license end
Set the value of the password of license
@param [String] password Password
# File lib/mensario.rb, line 58 def self.password=(password) @@config[:password] = password end
Send a sms
@param [Hash] opts Options @option opts :sender Name of who send the sms @option opts :text Content of sms @option opts :date (‘now’) Ruby Time object with the send date.
Message is sent inmediately if :date is undefined
@option opts :code Country code of mobile @option opts :phone Telephone number to send the sms @option opts :timezone (”) Timezone of the send.
All options are required except :date and :timezone
@return [Integer] Id of sms
# File lib/mensario.rb, line 142 def self.send_message(opts = {}) date = opts[:date] || '00000000000000' date = date.strftime('%Y%m%d%H%M%S') if date.class == Time xml = { 'timezone' => [ opts[:timezone] || '' ], 'msg' => { 'sender' => [ opts[:sender] ], 'text' => [ URI.encode(opts[:text]) ], 'date' => [ date ], 'rcp' => { 'cod' => opts[:code], 'phn' => opts[:phone] } } } self::api_call('SEND', xml)['msg'].first['id'].first.to_i end
Set configuration parameters license, username and password through a block
@example
Mensario::set_config do |m| m.license = "ASDER1234512" m.username = "caee4444" m.password = "xcderfa23" end
# File lib/mensario.rb, line 71 def self.set_config yield self end
Get the status of a sms
@param [Integer] id Id of the sms @return [String] status code
# File lib/mensario.rb, line 166 def self.status(id) xml = { 'msg' => { 'id' => ["#{id}"] } } self::api_call('MSG-QRY', xml)['status'].first['cod'] end
Set the value of the username of license
@param [String] username Username
# File lib/mensario.rb, line 51 def self.username=(username) @@config[:username] = username end