Class: TextAnywhere_ruby

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, originator, options = {}) ⇒ TextAnywhere_ruby

Returns a new instance of TextAnywhere_ruby.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/textanywhere_ruby.rb', line 137

def initialize(username,password,originator,options = {})
  live!
  use_http!

  @options = default_options
  @options[:externallogin] = username 
  @options[:password] = password
  @options[:originator] = originator if originator
  
  @options.merge(options)
  @remember_originator = @options[:originator] 
end

Class Method Details

.hiObject



5
6
7
# File 'lib/textanywhere_ruby.rb', line 5

def self.hi()
  "hello there"
end

Instance Method Details

#base_paramsObject



81
82
83
84
85
86
87
88
# File 'lib/textanywhere_ruby.rb', line 81

def base_params
  [
    :method,
    :returncsvstring,
    :externallogin,
    :password
  ]
end

#client_billing_reference=(val) ⇒ Object



221
222
223
# File 'lib/textanywhere_ruby.rb', line 221

def client_billing_reference=(val)
  @options[:clientbillingreference] = val
end

#credits_leftObject

the credits left - ok



293
294
295
296
297
# File 'lib/textanywhere_ruby.rb', line 293

def credits_left
  @options[:method] = 'GetCreditsLeft'
  response = ta_response(base_params)
  return response
end

#default_optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/textanywhere_ruby.rb', line 31

def default_options
  {:method=>"",
  :returncsvstring=>"false",
  :externallogin=>"",
  :password=>"",
  :clientbillingreference=>"all",
  :clientmessagereference=>"",
  :originator=>"",
  :destinations=>"",
  :body=>"",
  :validity=>72,
  :charactersetid=>2,
  :replymethodid=>1,
  :replydata=>"",
  :statusnotificationurl=>"",
  :number=>"",
  :keyword=>"",
  :shortcode=>"",
  :rbid=>""}
end

#format_number(phone) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/textanywhere_ruby.rb', line 158

def format_number(phone)
  phone = phone.gsub(/[^0-9]/, "")
  
  if phone.start_with?("00")
    phone = phone[2..-1]
  end
  if phone.start_with?("0")
    phone = "44" + phone[1..-1]
  end
  phone = "+" + phone 

  return phone 
end

#format_response(response) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/textanywhere_ruby.rb', line 185

def format_response(response)
  hash = {}

  response.children.each do |p|
    if p.children.length > 1 || p.children.children.length > 1
       new_item = format_response(p)
    else 
      new_item = p.inner_html 
    end 

    if hash.has_key?(p.name) 
      unless hash[p.name].is_a?(Array)
        hash[p.name] = [] << hash[p.name]
      end
      hash[p.name] << new_item 
    else 
      hash[p.name] = new_item
    end
  end
  return hash 
end

#get_html_content(requested_url) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/textanywhere_ruby.rb', line 211

def get_html_content(requested_url)
  response = Nokogiri::XML(open(requested_url))
  trans_code = response.xpath('//Transaction/Code').inner_text().to_i
  trans_text = response.xpath('//Transaction/Description').inner_text()

  a = format_response(response.xpath("/*"))
  puts a
  return a
end

#inbound(number = "", keyword = "") ⇒ Object

should be ok



300
301
302
303
304
305
306
307
308
# File 'lib/textanywhere_ruby.rb', line 300

def inbound(number="", keyword="")
  number = @options['originator'] if number == ""
  @options[:method] = 'GetSMSInbound'
  @options[:number] = number 
  @options[:keyword] = keyword
  
  response = ta_response(inbound_params)
  return response
end

#inbound_paramsObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/textanywhere_ruby.rb', line 90

def inbound_params
  [
    :method,
    :returncsvstring,
    :externallogin,
    :password,
    :number,
    :keyword
  ]
end

#is_ok(response) ⇒ Object



207
208
209
# File 'lib/textanywhere_ruby.rb', line 207

def is_ok(response)
  response["Transaction"]["Code"].to_i == 1
end

#live!Object



13
14
15
# File 'lib/textanywhere_ruby.rb', line 13

def live!
  @is_live = true 
end

#live?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/textanywhere_ruby.rb', line 9

def live?
  @is_live
end

#live_send(destinations, body, clientmessagereference, options = {}) ⇒ Object

live send a text message - finished.



241
242
243
244
245
246
247
248
249
# File 'lib/textanywhere_ruby.rb', line 241

def live_send(destinations,body,clientmessagereference,options = {})
  @options[:method] = 'sendsms'
  @options[:destinations] = to_csv(destinations) 
  @options[:clientmessagereference] = clientmessagereference
  @options[:body] = body
  @options.merge(options)
  response = ta_response(send_params)
  return response
end

#method_for(method) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/textanywhere_ruby.rb', line 350

def method_for(method)
  reply = {
    :no_reply=>1,
    :reply_to_email=>2,
    :reply_to_web_service=>3,
    :send_phone_no=>4,
    :reply_to_url=>5,
    :no_reply_use_shortcode=>7
  }[method]

  reply = 1 unless reply
  return reply
end

#no_reply_originator(orig = "") ⇒ Object



364
365
366
367
368
# File 'lib/textanywhere_ruby.rb', line 364

def no_reply_originator(orig="")
  @options[:replymethodid] = 1
  @options[:replydata] = "" 
  @options[:originator] = orig unless orig==""
end

#no_reply_phone_number(orig = "") ⇒ Object



370
371
372
373
374
# File 'lib/textanywhere_ruby.rb', line 370

def no_reply_phone_number(orig="")
  @options[:replymethodid] = 4
  @options[:replydata] = "" 
  @options[:originator] = orig unless orig==""
end

#no_reply_to_shortcode(shortcode) ⇒ Object



394
395
396
397
398
# File 'lib/textanywhere_ruby.rb', line 394

def no_reply_to_shortcode(shortcode)
  @options[:replymethodid] = 7
  @options[:replydata] = ""
  @options[:originator] = shortcode
end

#premium_inbound(shortcode, keyword) ⇒ Object

should be ok



311
312
313
314
315
316
317
318
# File 'lib/textanywhere_ruby.rb', line 311

def premium_inbound(shortcode,keyword)
  @options[:method] = 'GetPremiumSMSInbound'
  @options[:shortcode] = number 
  @options[:keyword] = keyword

  response = ta_response(premium_inbound_params)
  return response
end

#premium_inbound_paramsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/textanywhere_ruby.rb', line 101

def premium_inbound_params
  [
    :method,
    :returncsvstring,
    :externallogin,
    :password,
    :shortcode,
    :keyword
  ]
end

#premium_status(clientmessagereference) ⇒ Object



332
333
334
335
336
337
# File 'lib/textanywhere_ruby.rb', line 332

def premium_status(clientmessagereference)
  @options[:method] = 'GetPremiumSMSStatus'    
  @options[:clientmessagereference] = clientmessagereference
  response = ta_response(status_params)
  return response
end

#replyObject

get replies from the text message - OK



260
261
262
263
264
265
# File 'lib/textanywhere_ruby.rb', line 260

def reply
  @options[:method] = 'getsmsreply'
  @options[:clientmessagereference] = clientmessagereference
  response = ta_response(status_params)
  return response
end

#reply_to_email(email) ⇒ Object



376
377
378
379
380
# File 'lib/textanywhere_ruby.rb', line 376

def reply_to_email(email)
  @options[:replymethodid] = 2
  @options[:replydata] = email
  @options[:originator] = ""
end

#reply_to_url(url) ⇒ Object



388
389
390
391
392
# File 'lib/textanywhere_ruby.rb', line 388

def reply_to_url(url)
  @options[:replymethodid] = 5
  @options[:replydata] = url
  @options[:originator] = ""
end

#reply_to_web_serviceObject



382
383
384
385
386
# File 'lib/textanywhere_ruby.rb', line 382

def reply_to_web_service
  @options[:replymethodid] = 3
  @options[:replydata] = ""
  @options[:originator] = ""
end

#restore_originatorObject



267
268
269
270
271
# File 'lib/textanywhere_ruby.rb', line 267

def restore_originator
  @options[:originator] = @remember_originator
  @options[:replydata] = ""
  @options[:replymethodid] = 1
end

#send(destinations, body, clientmessagereference = "ref", options = {}) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/textanywhere_ruby.rb', line 150

def send(destinations,body,clientmessagereference="ref",options = {})
  if live? 
    return live_send(destinations,body,clientmessagereference,options)
  else
    return test_send(destinations,body,clientmessagereference,options)
  end
end

#send_paramsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/textanywhere_ruby.rb', line 52

def send_params
  [
    :method,
    :returncsvstring,
    :externallogin,
    :password,
    :clientbillingreference,
    :clientmessagereference,
    :originator,
    :destinations,
    :body,
    :validity,
    :charactersetid,
    :replymethodid,
    :replydata,
    :statusnotificationurl
  ]
end

#send_premium(rbid, body, clientmessagereference, options = {}) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/textanywhere_ruby.rb', line 320

def send_premium(rbid,body,clientmessagereference,options = {})
  @options[:method] = 'SendPremiumSMS'
  @options.merge(options)

  @options[:clientmessagereference] = clientmessagereference
  @options[:rbid] = rbid
  @options[:body] = body

  response = ta_response(send_premium_params)
  return response
end

#send_premium_paramsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/textanywhere_ruby.rb', line 112

def send_premium_params
  [
    :returncsvstring,
    :externallogin,
    :password,
    :clientbillingreference,
    :clientmessagereference,
    :rbid,
    :body,
    :validity,
    :charactersetid,
    :statusnotificationurl
  ]
end

#status(clientmessagereference) ⇒ Object

get status of text message - finished



252
253
254
255
256
257
# File 'lib/textanywhere_ruby.rb', line 252

def status(clientmessagereference)
  @options[:method] = 'getsmsstatus'
  @options[:clientmessagereference] = clientmessagereference
  response = ta_response(status_params)
  return response
end

#status_paramsObject



71
72
73
74
75
76
77
78
79
# File 'lib/textanywhere_ruby.rb', line 71

def status_params
  [
    :method,
    :returncsvstring,
    :externallogin,
    :password,
    :clientmessagereference
  ]
end

#subscriber_paramsObject



127
128
129
130
131
132
133
134
135
# File 'lib/textanywhere_ruby.rb', line 127

def subscriber_params
  [
    :returncsvstring,
    :externallogin,
    :password,
    :number,
    :keyword
  ]
end

#subscribers(number = "", keyword = "") ⇒ Object



339
340
341
342
343
344
345
346
347
348
# File 'lib/textanywhere_ruby.rb', line 339

def subscribers(number="", keyword="")
  @options[:method] = 'GetSubscribers'

  number = @options['originator'] if number == ""

  @options[:number] = number 
  @options[:keyword] = keyword
  response = ta_response(subscriber_params)
  return response
end

#ta_response(params) ⇒ Object

params is an array of symbols. Use this array to create a url containing only those symbols as parameters. Array is created from the @options variable need to send this off to the internet and get answer back.



229
230
231
232
233
234
235
236
237
238
# File 'lib/textanywhere_ruby.rb', line 229

def ta_response(params)
  url = ""
  params.each do |param|
    url = url + "&" if url != ""
    url = url + param.to_s + "=" + URI::encode(@options[param].to_s)
  end 
  response = get_html_content(@start_url + "?" + url)
  puts url 
  return response
end

#test!Object



17
18
19
# File 'lib/textanywhere_ruby.rb', line 17

def test!
  @is_live = false 
end

#test_send(destinations, body, clientmessagereference, options = {}) ⇒ Object

test send a message - we’re OK



281
282
283
284
285
286
287
288
289
290
# File 'lib/textanywhere_ruby.rb', line 281

def test_send(destinations,body,clientmessagereference,options = {})
  @options[:method] = 'testsendsms'
  @options[:clientmessagereference] = clientmessagereference
  @options[:destinations] = to_csv(destinations)
  @options[:body] = body
  @options.merge(options)
  response = ta_response(send_params)

  return response
end

#test_serviceObject

Test service is working. All OK



274
275
276
277
278
# File 'lib/textanywhere_ruby.rb', line 274

def test_service
  @options[:method] = 'testservice'
  response = ta_response(base_params)
  return response
end

#to_csv(destinations) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/textanywhere_ruby.rb', line 172

def to_csv(destinations)
  if destinations.kind_of?(Array)
    ret = ""
    destinations.each do |dest|
      ret=ret+"," if ret != ""
      ret = ret + (format_number(dest))
    end
  else
    ret = (format_number(destinations)) 
  end 
  return ret
end

#use_http!Object



26
27
28
29
# File 'lib/textanywhere_ruby.rb', line 26

def use_http!
  @start_url = "http://www.textapp.net/webservice/httpservice.aspx"
  @is_https = false 
end

#use_https!Object



21
22
23
24
# File 'lib/textanywhere_ruby.rb', line 21

def use_https!
  @start_url = "https://www.textapp.net/webservice/httpservice.aspx"
  @is_https = true 
end