Class: Mensario

Inherits:
Object
  • Object
show all
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

Class Method Details

.api_call(task, data = {}) ⇒ Hash

Do de api call with all data and process the response

Parameters:

  • task (String)

    Name of api task

  • data (Hash) (defaults to: {})

    Hash containing all data

Returns:

  • (Hash)

    response hash

Raises:



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.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

.balanceInteger

Allows to consult the balance remaining of the license

Returns:

  • (Integer)

    the balance remaining



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

Parameters:

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :config (Object) — default: 'config/mensario.yml'

    Path to the configuration file

  • :profile (Object) — default: :default

    Profile in configuration to load



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

Parameters:

  • Message (Integer)

    id to cancell

Returns:

  • (Boolean)

    Return ‘true’ if message is cancelled and ‘false’ if the message can’t be cancelled



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

Parameters:

  • license (String)

    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

Parameters:

  • password (String)

    Password



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

Parameters:

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :sender (Object)

    Name of who send the sms

  • :text (Object)

    Content of sms

  • :date (Object) — default: 'now'

    Ruby Time object with the send date. Message is sent inmediately if :date is undefined

  • :code (Object)

    Country code of mobile

  • :phone (Object)

    Telephone number to send the sms

  • :timezone (Object) — default: ''

    Timezone of the send.

Returns:

  • (Integer)

    Id of sms



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.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_config {|_self| ... } ⇒ Object

Set configuration parameters license, username and password through a block

Examples:

Mensario::set_config do |m|
  m.license = "ASDER1234512"
  m.username = "caee4444"
  m.password = "xcderfa23"
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Mensario)

    the object that the method was called on



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

Parameters:

  • id (Integer)

    Id of the sms

Returns:

  • (String)

    status code



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

Parameters:

  • username (String)

    Username



51
52
53
# File 'lib/mensario.rb', line 51

def self.username=(username)
  @@config[:username] = username
end