class Textveloper::Sdk

Public Class Methods

new(account_token_number, subaccount_token_number) click to toggle source
# File lib/textveloper.rb, line 9
def initialize(account_token_number, subaccount_token_number)
  @account_token_number = account_token_number
  @subaccount_token_number = subaccount_token_number
end

Public Instance Methods

account_balance() click to toggle source
# File lib/textveloper.rb, line 83
def account_balance
  hash_contructor(Curl.post(url + api_actions[:puntos_cuenta] + '/', account_data))
end
account_data() click to toggle source
# File lib/textveloper.rb, line 77
def account_data
  {
    :cuenta_token => @account_token_number,
  }
end
account_history() click to toggle source
# File lib/textveloper.rb, line 95
def account_history
  hash_contructor(Curl.post(url + api_actions[:envios] + '/',transactional_data))
end
api_actions() click to toggle source
# File lib/textveloper.rb, line 14
def api_actions
  {
  :enviar => 'enviar',
  :puntos_cuenta => 'saldo-cuenta',
  :puntos_subcuenta => 'saldo-subcuenta',
  :compras => 'historial-compras',
  :envios => 'historial-envios',
  :transferencias => 'historial-transferencias'
  }
end
buy_history() click to toggle source
# File lib/textveloper.rb, line 87
def buy_history
  hash_contructor(Curl.post(url + api_actions[:compras] + '/', account_data))
end
chunck_message(message) click to toggle source
# File lib/textveloper.rb, line 135
def chunck_message(message)
  #Leave space for pagination i.e: "BLAh blah blah (2/3)"
  paginate(message.scan(/.{1,155}\b/).map(&:strip))
end
core_operation(number, message) click to toggle source
# File lib/textveloper.rb, line 25
def core_operation(number, message)
  data = {
    :cuenta_token => @account_token_number,
    :subcuenta_token => @subaccount_token_number,
    :telefono => format_phone(number),
    :mensaje => message
  }
  return Curl.post(url + api_actions[:enviar] + '/', data )
end
format_phone(phone_number) click to toggle source

private

# File lib/textveloper.rb, line 119
def format_phone(phone_number)
  phone_number.nil? ? "" : phone_number.gsub(/\W/,"").sub(/^58/,"").sub(/(^4)/, '0\1')
end
hash_constructor_with_numbers(numbers,response) click to toggle source
# File lib/textveloper.rb, line 109
def hash_constructor_with_numbers(numbers,response)
  data = Hash.new
  numbers.each_with_index do |number, index|
    data[number.to_sym] = hash_contructor(response[index])
  end
  data
end
hash_contructor(response) click to toggle source
# File lib/textveloper.rb, line 127
def hash_contructor(response)
  if valid_content_types.any? { |type| response.content_type.match(type) }
    JSON.parse(response.body_str)
  else
    {"transaccion"=>"error", "mensaje_transaccion"=>"ERROR_EN_SERVICIO"}
  end
end
mass_messages(numbers, message) click to toggle source
# File lib/textveloper.rb, line 49
def mass_messages(numbers, message)
  response = []
  if message.size <= 160
    numbers.each do |number|
      response << core_operation(number, message)
      slow_sms
    end
  else
    numbers.each do |number|
      chunck_message(message).each do |m|
        response << core_operation(number,m)
        slow_sms
      end
    end
  end
  numbers.map!{ |n| format_phone(n)}
  show_format_response(numbers,response)
end
paginate(arr) click to toggle source
# File lib/textveloper.rb, line 140
def paginate(arr)
  arr.map!.with_index {|elem,i| elem << " #{i+1}/#{arr.size}" }
end
send_sms(number,message) click to toggle source

Servicio SMS

# File lib/textveloper.rb, line 37
def send_sms(number,message)
  response = []
  if message.size <= 160
    response << core_operation(number,message)
  else
    chunck_message(message).each do |m|
      response << core_operation(number, m)
    end
  end
  show_format_response([format_phone(number)],response)
end
show_format_response(numbers,response) click to toggle source

metodos de formato de data

# File lib/textveloper.rb, line 105
def show_format_response(numbers,response)
  hash_constructor_with_numbers(numbers,response)
end
slow_sms() click to toggle source
# File lib/textveloper.rb, line 144
def slow_sms
  sleep(1)
end
subaccount_balance() click to toggle source
# File lib/textveloper.rb, line 91
def subaccount_balance
  hash_contructor(Curl.post(url + api_actions[:puntos_subcuenta] + '/', transactional_data))
end
transactional_data() click to toggle source

Historial de Transacciones

# File lib/textveloper.rb, line 70
def transactional_data
  {
    :cuenta_token => @account_token_number,
    :subcuenta_token => @subaccount_token_number
  }
end
transfer_history() click to toggle source
# File lib/textveloper.rb, line 99
def transfer_history
  hash_contructor(Curl.post(url + api_actions[:transferencias] + '/',transactional_data))
end
url() click to toggle source
# File lib/textveloper.rb, line 148
def url
  'http://api.textveloper.com/'
end
valid_content_types() click to toggle source
# File lib/textveloper.rb, line 123
def valid_content_types
  ["application/json", "application/x-javascript", "text/javascript", "text/x-javascript", "text/x-json"]
end