textanywhere_ruby 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TextAnywhere_ruby'
4
+ puts TextAnywhere_ruby.hi(ARGV[0])
@@ -0,0 +1,401 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ class TextAnywhere_ruby
5
+ def self.hi()
6
+ "hello there"
7
+ end
8
+
9
+ def live?
10
+ @is_live
11
+ end
12
+
13
+ def live!
14
+ @is_live = true
15
+ end
16
+
17
+ def test!
18
+ @is_live = false
19
+ end
20
+
21
+ def use_https!
22
+ @start_url = "https://www.textapp.net/webservice/httpservice.aspx"
23
+ @is_https = true
24
+ end
25
+
26
+ def use_http!
27
+ @start_url = "http://www.textapp.net/webservice/httpservice.aspx"
28
+ @is_https = false
29
+ end
30
+
31
+ def default_options
32
+ {:method=>"",
33
+ :returncsvstring=>"false",
34
+ :externallogin=>"",
35
+ :password=>"",
36
+ :clientbillingreference=>"all",
37
+ :clientmessagereference=>"",
38
+ :originator=>"",
39
+ :destinations=>"",
40
+ :body=>"",
41
+ :validity=>72,
42
+ :charactersetid=>2,
43
+ :replymethodid=>1,
44
+ :replydata=>"",
45
+ :statusnotificationurl=>"",
46
+ :number=>"",
47
+ :keyword=>"",
48
+ :shortcode=>"",
49
+ :rbid=>""}
50
+ end
51
+
52
+ def send_params
53
+ [
54
+ :method,
55
+ :returncsvstring,
56
+ :externallogin,
57
+ :password,
58
+ :clientbillingreference,
59
+ :clientmessagereference,
60
+ :originator,
61
+ :destinations,
62
+ :body,
63
+ :validity,
64
+ :charactersetid,
65
+ :replymethodid,
66
+ :replydata,
67
+ :statusnotificationurl
68
+ ]
69
+ end
70
+
71
+ def status_params
72
+ [
73
+ :method,
74
+ :returncsvstring,
75
+ :externallogin,
76
+ :password,
77
+ :clientmessagereference
78
+ ]
79
+ end
80
+
81
+ def base_params
82
+ [
83
+ :method,
84
+ :returncsvstring,
85
+ :externallogin,
86
+ :password
87
+ ]
88
+ end
89
+
90
+ def inbound_params
91
+ [
92
+ :method,
93
+ :returncsvstring,
94
+ :externallogin,
95
+ :password,
96
+ :number,
97
+ :keyword
98
+ ]
99
+ end
100
+
101
+ def premium_inbound_params
102
+ [
103
+ :method,
104
+ :returncsvstring,
105
+ :externallogin,
106
+ :password,
107
+ :shortcode,
108
+ :keyword
109
+ ]
110
+ end
111
+
112
+ def send_premium_params
113
+ [
114
+ :returncsvstring,
115
+ :externallogin,
116
+ :password,
117
+ :clientbillingreference,
118
+ :clientmessagereference,
119
+ :rbid,
120
+ :body,
121
+ :validity,
122
+ :charactersetid,
123
+ :statusnotificationurl
124
+ ]
125
+ end
126
+
127
+ def subscriber_params
128
+ [
129
+ :returncsvstring,
130
+ :externallogin,
131
+ :password,
132
+ :number,
133
+ :keyword
134
+ ]
135
+ end
136
+
137
+ def initialize(username,password,originator,options = {})
138
+ live!
139
+ use_http!
140
+
141
+ @options = default_options
142
+ @options[:externallogin] = username
143
+ @options[:password] = password
144
+ @options[:originator] = originator if originator
145
+
146
+ @options.merge(options)
147
+ @remember_originator = @options[:originator]
148
+ end
149
+
150
+ def send(destinations,body,clientmessagereference="ref",options = {})
151
+ if live?
152
+ return live_send(destinations,body,clientmessagereference,options)
153
+ else
154
+ return test_send(destinations,body,clientmessagereference,options)
155
+ end
156
+ end
157
+
158
+ def format_number(phone)
159
+ phone = phone.gsub(/[^0-9]/, "")
160
+
161
+ if phone.start_with?("00")
162
+ phone = phone[2..-1]
163
+ end
164
+ if phone.start_with?("0")
165
+ phone = "44" + phone[1..-1]
166
+ end
167
+ phone = "+" + phone
168
+
169
+ return phone
170
+ end
171
+
172
+ def to_csv(destinations)
173
+ if destinations.kind_of?(Array)
174
+ ret = ""
175
+ destinations.each do |dest|
176
+ ret=ret+"," if ret != ""
177
+ ret = ret + (format_number(dest))
178
+ end
179
+ else
180
+ ret = (format_number(destinations))
181
+ end
182
+ return ret
183
+ end
184
+
185
+ def format_response(response)
186
+ hash = {}
187
+
188
+ response.children.each do |p|
189
+ if p.children.length > 1 || p.children.children.length > 1
190
+ new_item = format_response(p)
191
+ else
192
+ new_item = p.inner_html
193
+ end
194
+
195
+ if hash.has_key?(p.name)
196
+ unless hash[p.name].is_a?(Array)
197
+ hash[p.name] = [] << hash[p.name]
198
+ end
199
+ hash[p.name] << new_item
200
+ else
201
+ hash[p.name] = new_item
202
+ end
203
+ end
204
+ return hash
205
+ end
206
+
207
+ def is_ok(response)
208
+ response["Transaction"]["Code"].to_i == 1
209
+ end
210
+
211
+ def get_html_content(requested_url)
212
+ response = Nokogiri::XML(open(requested_url))
213
+ trans_code = response.xpath('//Transaction/Code').inner_text().to_i
214
+ trans_text = response.xpath('//Transaction/Description').inner_text()
215
+
216
+ a = format_response(response.xpath("/*"))
217
+ puts a
218
+ return a
219
+ end
220
+
221
+ def client_billing_reference=(val)
222
+ @options[:clientbillingreference] = val
223
+ end
224
+
225
+ #params is an array of symbols. Use this array to create
226
+ #a url containing only those symbols as parameters.
227
+ #Array is created from the @options variable
228
+ #need to send this off to the internet and get answer back.
229
+ def ta_response(params)
230
+ url = ""
231
+ params.each do |param|
232
+ url = url + "&" if url != ""
233
+ url = url + param.to_s + "=" + URI::encode(@options[param].to_s)
234
+ end
235
+ response = get_html_content(@start_url + "?" + url)
236
+ puts url
237
+ return response
238
+ end
239
+
240
+ #live send a text message - finished.
241
+ def live_send(destinations,body,clientmessagereference,options = {})
242
+ @options[:method] = 'sendsms'
243
+ @options[:destinations] = to_csv(destinations)
244
+ @options[:clientmessagereference] = clientmessagereference
245
+ @options[:body] = body
246
+ @options.merge(options)
247
+ response = ta_response(send_params)
248
+ return response
249
+ end
250
+
251
+ #get status of text message - finished
252
+ def status(clientmessagereference)
253
+ @options[:method] = 'getsmsstatus'
254
+ @options[:clientmessagereference] = clientmessagereference
255
+ response = ta_response(status_params)
256
+ return response
257
+ end
258
+
259
+ #get replies from the text message - OK
260
+ def reply
261
+ @options[:method] = 'getsmsreply'
262
+ @options[:clientmessagereference] = clientmessagereference
263
+ response = ta_response(status_params)
264
+ return response
265
+ end
266
+
267
+ def restore_originator
268
+ @options[:originator] = @remember_originator
269
+ @options[:replydata] = ""
270
+ @options[:replymethodid] = 1
271
+ end
272
+
273
+ #Test service is working. All OK
274
+ def test_service
275
+ @options[:method] = 'testservice'
276
+ response = ta_response(base_params)
277
+ return response
278
+ end
279
+
280
+ #test send a message - we're OK
281
+ def test_send(destinations,body,clientmessagereference,options = {})
282
+ @options[:method] = 'testsendsms'
283
+ @options[:clientmessagereference] = clientmessagereference
284
+ @options[:destinations] = to_csv(destinations)
285
+ @options[:body] = body
286
+ @options.merge(options)
287
+ response = ta_response(send_params)
288
+
289
+ return response
290
+ end
291
+
292
+ #the credits left - ok
293
+ def credits_left
294
+ @options[:method] = 'GetCreditsLeft'
295
+ response = ta_response(base_params)
296
+ return response
297
+ end
298
+
299
+ #should be ok
300
+ def inbound(number="", keyword="")
301
+ number = @options['originator'] if number == ""
302
+ @options[:method] = 'GetSMSInbound'
303
+ @options[:number] = number
304
+ @options[:keyword] = keyword
305
+
306
+ response = ta_response(inbound_params)
307
+ return response
308
+ end
309
+
310
+ #should be ok
311
+ def premium_inbound(shortcode,keyword)
312
+ @options[:method] = 'GetPremiumSMSInbound'
313
+ @options[:shortcode] = number
314
+ @options[:keyword] = keyword
315
+
316
+ response = ta_response(premium_inbound_params)
317
+ return response
318
+ end
319
+
320
+ def send_premium(rbid,body,clientmessagereference,options = {})
321
+ @options[:method] = 'SendPremiumSMS'
322
+ @options.merge(options)
323
+
324
+ @options[:clientmessagereference] = clientmessagereference
325
+ @options[:rbid] = rbid
326
+ @options[:body] = body
327
+
328
+ response = ta_response(send_premium_params)
329
+ return response
330
+ end
331
+
332
+ def premium_status(clientmessagereference)
333
+ @options[:method] = 'GetPremiumSMSStatus'
334
+ @options[:clientmessagereference] = clientmessagereference
335
+ response = ta_response(status_params)
336
+ return response
337
+ end
338
+
339
+ def subscribers(number="", keyword="")
340
+ @options[:method] = 'GetSubscribers'
341
+
342
+ number = @options['originator'] if number == ""
343
+
344
+ @options[:number] = number
345
+ @options[:keyword] = keyword
346
+ response = ta_response(subscriber_params)
347
+ return response
348
+ end
349
+
350
+ def method_for(method)
351
+ reply = {
352
+ :no_reply=>1,
353
+ :reply_to_email=>2,
354
+ :reply_to_web_service=>3,
355
+ :send_phone_no=>4,
356
+ :reply_to_url=>5,
357
+ :no_reply_use_shortcode=>7
358
+ }[method]
359
+
360
+ reply = 1 unless reply
361
+ return reply
362
+ end
363
+
364
+ def no_reply_originator(orig="")
365
+ @options[:replymethodid] = 1
366
+ @options[:replydata] = ""
367
+ @options[:originator] = orig unless orig==""
368
+ end
369
+
370
+ def no_reply_phone_number(orig="")
371
+ @options[:replymethodid] = 4
372
+ @options[:replydata] = ""
373
+ @options[:originator] = orig unless orig==""
374
+ end
375
+
376
+ def reply_to_email(email)
377
+ @options[:replymethodid] = 2
378
+ @options[:replydata] = email
379
+ @options[:originator] = ""
380
+ end
381
+
382
+ def reply_to_web_service
383
+ @options[:replymethodid] = 3
384
+ @options[:replydata] = ""
385
+ @options[:originator] = ""
386
+ end
387
+
388
+ def reply_to_url(url)
389
+ @options[:replymethodid] = 5
390
+ @options[:replydata] = url
391
+ @options[:originator] = ""
392
+ end
393
+
394
+ def no_reply_to_shortcode(shortcode)
395
+ @options[:replymethodid] = 7
396
+ @options[:replydata] = ""
397
+ @options[:originator] = shortcode
398
+ end
399
+
400
+ end
401
+
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'TextAnywhere_ruby'
3
+
4
+ class TextAnywhere_rubyTest < Test::Unit::TestCase
5
+ def test_english_hello
6
+ assert_equal "hello there", TextAnywhere_ruby.hi()
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textanywhere_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sean Bamforth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &27004152 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *27004152
25
+ description: Library to communicate with TextAnywhere.net
26
+ email: sean@theguru.co.uk
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - Rakefile
32
+ - lib/textanywhere_ruby.rb
33
+ - bin/textanywhere_ruby
34
+ - test/test_textanywhere_ruby.rb
35
+ homepage: http://rubygems.org/gems/textanywhere_ruby
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.11
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: textanywhere_ruby!
59
+ test_files:
60
+ - test/test_textanywhere_ruby.rb