Class: Textveloper::Sdk
- Inherits:
-
Object
- Object
- Textveloper::Sdk
- Defined in:
- lib/textveloper.rb
Instance Method Summary collapse
- #account_balance ⇒ Object
- #account_data ⇒ Object
- #account_history ⇒ Object
- #api_actions ⇒ Object
- #buy_history ⇒ Object
- #chunck_message(message) ⇒ Object
- #core_operation(number, message) ⇒ Object
-
#format_phone(phone_number) ⇒ Object
private.
- #hash_constructor_with_numbers(numbers, response) ⇒ Object
- #hash_contructor(response) ⇒ Object
-
#initialize(account_token_number, subaccount_token_number) ⇒ Sdk
constructor
A new instance of Sdk.
- #mass_messages(numbers, message) ⇒ Object
- #paginate(arr) ⇒ Object
-
#send_sms(number, message) ⇒ Object
Servicio SMS.
-
#show_format_response(numbers, response) ⇒ Object
metodos de formato de data.
- #slow_sms ⇒ Object
- #subaccount_balance ⇒ Object
-
#transactional_data ⇒ Object
Historial de Transacciones.
- #transfer_history ⇒ Object
- #url ⇒ Object
- #valid_content_types ⇒ Object
Constructor Details
#initialize(account_token_number, subaccount_token_number) ⇒ Sdk
Returns a new instance of Sdk.
9 10 11 12 |
# 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 |
Instance Method Details
#account_balance ⇒ Object
83 84 85 |
# File 'lib/textveloper.rb', line 83 def account_balance hash_contructor(Curl.post(url + api_actions[:puntos_cuenta] + '/', account_data)) end |
#account_data ⇒ Object
77 78 79 80 81 |
# File 'lib/textveloper.rb', line 77 def account_data { :cuenta_token => @account_token_number, } end |
#account_history ⇒ Object
95 96 97 |
# File 'lib/textveloper.rb', line 95 def account_history hash_contructor(Curl.post(url + api_actions[:envios] + '/',transactional_data)) end |
#api_actions ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# 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 ⇒ Object
87 88 89 |
# File 'lib/textveloper.rb', line 87 def buy_history hash_contructor(Curl.post(url + api_actions[:compras] + '/', account_data)) end |
#chunck_message(message) ⇒ Object
135 136 137 138 |
# File 'lib/textveloper.rb', line 135 def () #Leave space for pagination i.e: "BLAh blah blah (2/3)" paginate(.scan(/.{1,155}\b/).map(&:strip)) end |
#core_operation(number, message) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/textveloper.rb', line 25 def core_operation(number, ) data = { :cuenta_token => @account_token_number, :subcuenta_token => @subaccount_token_number, :telefono => format_phone(number), :mensaje => } return Curl.post(url + api_actions[:enviar] + '/', data ) end |
#format_phone(phone_number) ⇒ Object
private
119 120 121 |
# 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) ⇒ Object
109 110 111 112 113 114 115 |
# 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) ⇒ Object
127 128 129 130 131 132 133 |
# 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) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/textveloper.rb', line 49 def (numbers, ) response = [] if .size <= 160 numbers.each do |number| response << core_operation(number, ) slow_sms end else numbers.each do |number| ().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) ⇒ Object
140 141 142 |
# 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) ⇒ Object
Servicio SMS
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/textveloper.rb', line 37 def send_sms(number,) response = [] if .size <= 160 response << core_operation(number,) else ().each do |m| response << core_operation(number, m) end end show_format_response([format_phone(number)],response) end |
#show_format_response(numbers, response) ⇒ Object
metodos de formato de data
105 106 107 |
# File 'lib/textveloper.rb', line 105 def show_format_response(numbers,response) hash_constructor_with_numbers(numbers,response) end |
#slow_sms ⇒ Object
144 145 146 |
# File 'lib/textveloper.rb', line 144 def slow_sms sleep(1) end |
#subaccount_balance ⇒ Object
91 92 93 |
# File 'lib/textveloper.rb', line 91 def subaccount_balance hash_contructor(Curl.post(url + api_actions[:puntos_subcuenta] + '/', transactional_data)) end |
#transactional_data ⇒ Object
Historial de Transacciones
70 71 72 73 74 75 |
# File 'lib/textveloper.rb', line 70 def transactional_data { :cuenta_token => @account_token_number, :subcuenta_token => @subaccount_token_number } end |
#transfer_history ⇒ Object
99 100 101 |
# File 'lib/textveloper.rb', line 99 def transfer_history hash_contructor(Curl.post(url + api_actions[:transferencias] + '/',transactional_data)) end |
#url ⇒ Object
148 149 150 |
# File 'lib/textveloper.rb', line 148 def url 'http://api.textveloper.com/' end |
#valid_content_types ⇒ Object
123 124 125 |
# File 'lib/textveloper.rb', line 123 def valid_content_types ["application/json", "application/x-javascript", "text/javascript", "text/x-javascript", "text/x-json"] end |