Class: Mensario
- Inherits:
-
Object
- Object
- Mensario
- Defined in:
- lib/mensario.rb
Overview
A class that allow us to send a sms message through Mensario SMS Service
Constant Summary collapse
- API_HOST =
Api host
'es.servicios.mensario.com'
- API_PORT =
Api port
443
- API_PATH =
Api path
'/sem/api/api.do'
- @@config =
Store config
{}
Class Method Summary collapse
-
.api_call(task, data = {}) ⇒ Hash
Do de api call with all data and process the response.
-
.balance ⇒ Integer
Allows to consult the balance remaining of the license.
-
.config(opts = {}) ⇒ Object
Load configuration and save it in @@config.
-
.destroy(id) ⇒ Boolean
Cancel the message which receives as a parameter.
-
.license=(license) ⇒ Object
Set the value of the license number.
-
.password=(password) ⇒ Object
Set the value of the password of license.
-
.send_message(opts = {}) ⇒ Integer
Send a sms.
-
.set_config {|_self| ... } ⇒ Object
Set configuration parameters license, username and password through a block.
-
.status(id) ⇒ String
Get the status of a sms.
-
.username=(username) ⇒ Object
Set the value of the username of license.
Class Method Details
.api_call(task, data = {}) ⇒ Hash
Do de api call with all data and process the response
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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. end result = XmlSimple.xml_in(response.body) raise MensarioApiException.new(result['result'].first), result['result'].first unless result['result'].first == 'OK' return result end |
.balance ⇒ Integer
Allows to consult the balance remaining of the license
191 192 193 |
# File 'lib/mensario.rb', line 191 def self.balance self::api_call('QUANT-QRY')['quantity'].first.to_i end |
.config(opts = {}) ⇒ Object
Load configuration and save it in @@config
116 117 118 119 120 121 122 123 124 125 126 |
# 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 |
.destroy(id) ⇒ Boolean
Cancel the message which receives as a parameter
179 180 181 182 183 184 185 186 187 |
# 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 |
.license=(license) ⇒ Object
Set the value of the license number
44 45 46 |
# File 'lib/mensario.rb', line 44 def self.license=(license) @@config[:license] = license end |
.password=(password) ⇒ Object
Set the value of the password of license
58 59 60 |
# File 'lib/mensario.rb', line 58 def self.password=(password) @@config[:password] = password end |
.send_message(opts = {}) ⇒ Integer
Send a sms
All options are required except :date and :timezone
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mensario.rb', line 142 def self.(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_config {|_self| ... } ⇒ Object
Set configuration parameters license, username and password through a block
71 72 73 |
# File 'lib/mensario.rb', line 71 def self.set_config yield self end |
.status(id) ⇒ String
Get the status of a sms
166 167 168 169 170 171 172 173 |
# File 'lib/mensario.rb', line 166 def self.status(id) xml = { 'msg' => { 'id' => ["#{id}"] } } self::api_call('MSG-QRY', xml)['status'].first['cod'] end |
.username=(username) ⇒ Object
Set the value of the username of license
51 52 53 |
# File 'lib/mensario.rb', line 51 def self.username=(username) @@config[:username] = username end |