Class: Textveloper::Sdk

Inherits:
Object
  • Object
show all
Defined in:
lib/textveloper.rb

Instance Method Summary collapse

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(, subaccount_token_number)
  @account_token_number = 
  @subaccount_token_number = subaccount_token_number
end

Instance Method Details

#account_balanceObject



83
84
85
# File 'lib/textveloper.rb', line 83

def 
  hash_contructor(Curl.post(url + api_actions[:puntos_cuenta] + '/', ))
end

#account_dataObject



77
78
79
80
81
# File 'lib/textveloper.rb', line 77

def 
  {
    :cuenta_token => @account_token_number,
  }
end

#account_historyObject



95
96
97
# File 'lib/textveloper.rb', line 95

def 
  hash_contructor(Curl.post(url + api_actions[:envios] + '/',transactional_data))
end

#api_actionsObject



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_historyObject



87
88
89
# File 'lib/textveloper.rb', line 87

def buy_history
  hash_contructor(Curl.post(url + api_actions[:compras] + '/', ))
end

#chunck_message(message) ⇒ Object



135
136
137
138
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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) ⇒ 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 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) ⇒ 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,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) ⇒ 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_smsObject



144
145
146
# File 'lib/textveloper.rb', line 144

def slow_sms
  sleep(1)
end

#subaccount_balanceObject



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_dataObject

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_historyObject



99
100
101
# File 'lib/textveloper.rb', line 99

def transfer_history
  hash_contructor(Curl.post(url + api_actions[:transferencias] + '/',transactional_data))
end

#urlObject



148
149
150
# File 'lib/textveloper.rb', line 148

def url
  'http://api.textveloper.com/'
end

#valid_content_typesObject



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