sendsms 0.0.2 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sendsms.rb +206 -34
- metadata +5 -7
data/lib/sendsms.rb
CHANGED
@@ -1,36 +1,69 @@
|
|
1
|
+
#############################################################################
|
2
|
+
# #
|
3
|
+
# Copyright (C) 2012 Revath S Kumar #
|
4
|
+
# #
|
5
|
+
# This program is free software: you can redistribute it and/or modify #
|
6
|
+
# it under the terms of the GNU General Public License as published by #
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or #
|
8
|
+
# (at your option) any later version. #
|
9
|
+
# #
|
10
|
+
# This program is distributed in the hope that it will be useful, #
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
13
|
+
# GNU General Public License for more details. #
|
14
|
+
# #
|
15
|
+
# You should have received a copy of the GNU General Public License #
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/> #
|
17
|
+
# #
|
18
|
+
#############################################################################
|
19
|
+
|
1
20
|
require "net/http"
|
2
21
|
require "net/https"
|
3
22
|
require "uri"
|
4
23
|
require "hpricot"
|
5
24
|
|
25
|
+
#
|
26
|
+
# The class which handles sending sms with way2sms
|
27
|
+
#
|
28
|
+
# @author Revath S Kumar
|
29
|
+
#
|
6
30
|
class SendSms
|
7
31
|
attr_accessor :username, :password
|
8
32
|
|
33
|
+
# The way2sms root url
|
9
34
|
URL = 'http://site6.way2sms.com'
|
10
35
|
|
11
|
-
|
36
|
+
#
|
37
|
+
# Initiate Sendsms class with way2sms username and password
|
38
|
+
#
|
39
|
+
# @param [String] username way2sms username
|
40
|
+
# @param [String] password way2sms password
|
41
|
+
# @param [true,false] auto_logout
|
42
|
+
# Set to logout after sending SMS. we don't need to call the logout explicitly
|
43
|
+
# Recommended to turn it off when you are sending SMS repeatedly
|
44
|
+
#
|
45
|
+
#
|
46
|
+
def initialize(username , password ,auto_logout = true)
|
12
47
|
@username = username
|
13
48
|
@password = password
|
14
49
|
@uri = URI.parse URL
|
15
50
|
@cookie = @action = nil
|
16
51
|
@referer = URL
|
52
|
+
@auto_logout = auto_logout
|
17
53
|
@http = Net::HTTP.new(@uri.host,@uri.port)
|
18
54
|
end
|
19
55
|
|
20
|
-
def set_header(cookie=nil,referer=nil)
|
21
|
-
{"Cookie" => cookie , "Referer" => referer ,"Content-Type" => "application/x-www-form-urlencoded",
|
22
|
-
"User-Agent" => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0" }
|
23
|
-
end
|
24
|
-
|
25
|
-
def validate(msisdn = nil)
|
26
|
-
result = /^(\+|00)?(91)?(9|8|7)[0-9]{9}$/.match(msisdn)
|
27
|
-
return {:success => false,:message => "Invalid Msisdn"} if result.nil?
|
28
|
-
msisdn_formated = result[0][-10..-1]
|
29
|
-
return {:success => true,:message => "Valid Msisdn",:msisdn => msisdn_formated}
|
30
|
-
end
|
31
56
|
|
57
|
+
# Login to way2sms.com
|
58
|
+
#
|
59
|
+
# @visibility public
|
60
|
+
# @return [json]
|
61
|
+
# A json response with status and message
|
62
|
+
# { :success => true, :message => "Login Successful" }
|
63
|
+
# { :success => false, :message => "Login Failed" }
|
64
|
+
#
|
32
65
|
def login
|
33
|
-
data =
|
66
|
+
data = "username=#{@username}&password=#{@password}"
|
34
67
|
headers = set_header @cookie, @referer
|
35
68
|
response = @http.post("/Login1.action",data,headers.delete_if {|i,j| j.nil? })
|
36
69
|
case response.code
|
@@ -47,34 +80,81 @@ class SendSms
|
|
47
80
|
end
|
48
81
|
end
|
49
82
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
83
|
+
#
|
84
|
+
# To send Individual and Group SMS
|
85
|
+
# This method support Group SMS from version 0.0.5
|
86
|
+
#
|
87
|
+
# @param [Mixed] msisdns
|
88
|
+
# The msisdn/msisdns to send the SMS
|
89
|
+
# Individual
|
90
|
+
# A single msisdn as String Eg: "9995436867"
|
91
|
+
# Group
|
92
|
+
# An array of msisdns
|
93
|
+
# Eg: ["9995436867","9037107542","9037864203"]
|
94
|
+
# An hash of msisdns
|
95
|
+
# Eg: {0 => "9995436867",1 => "9037107542",2 => "9037864203"}
|
96
|
+
# A semicolon(;) seperated string of msisdns
|
97
|
+
# Eg: "9995436867;9037107542;9037864203"
|
98
|
+
# @param [String] message The message to send
|
99
|
+
#
|
100
|
+
# @visibility public
|
101
|
+
# @return [json]
|
102
|
+
# A json response with status and message
|
103
|
+
# Individual
|
104
|
+
# {:success => true,:message => "Send successfull"}
|
105
|
+
# {:success => false,:message => "Send failed"}
|
106
|
+
# Group
|
107
|
+
# {
|
108
|
+
# "9995436867" => {:success => true,:message => "Send successfully"},
|
109
|
+
# "9037864203" => {:success => true,:message => "Send successfully"},
|
110
|
+
# "9037107542" => {:success => true,:message => "Send successfully"}
|
111
|
+
# }
|
112
|
+
#
|
113
|
+
def send msisdns,message
|
64
114
|
if @cookie.nil?
|
65
115
|
login_res = login
|
66
116
|
return {:success => false,:message => "Login failed"} if !login_res[:success]
|
67
117
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
118
|
+
if msisdns.kind_of?(String) && !msisdns.include?(";")
|
119
|
+
response = send_sms msisdns,message
|
120
|
+
logout if @auto_logout
|
121
|
+
return response
|
122
|
+
else
|
123
|
+
if msisdns.kind_of?(String) && msisdns.include?(";")
|
124
|
+
msisdns = msisdns.split(';')
|
125
|
+
end
|
126
|
+
response = {}
|
127
|
+
msisdns.each do | key, msisdn |
|
128
|
+
msisdn = key if msisdn.nil?
|
129
|
+
response[msisdn] = send_sms msisdn,message
|
130
|
+
end
|
131
|
+
logout if @auto_logout
|
132
|
+
return response
|
75
133
|
end
|
76
134
|
end
|
77
135
|
|
136
|
+
#
|
137
|
+
# To send Group SMS
|
138
|
+
#
|
139
|
+
# @deprecated Use {#send} instead of this method
|
140
|
+
#
|
141
|
+
#
|
142
|
+
# @param [String] msisdns
|
143
|
+
# A semicolon seperated string of msisdns
|
144
|
+
# Eg: "9995436867;9037107542;9037864203"
|
145
|
+
# @param [String] message The message to send
|
146
|
+
#
|
147
|
+
# @visibility public
|
148
|
+
#
|
149
|
+
# @return [json]
|
150
|
+
# A json response with status and message
|
151
|
+
# {
|
152
|
+
# "9995436867" => {:success => true,:message => "Send successfully"},
|
153
|
+
# "9037864203" => {:success => true,:message => "Send successfully"},
|
154
|
+
# "9037107542" => {:success => true,:message => "Send successfully"}
|
155
|
+
# }
|
156
|
+
#
|
157
|
+
#
|
78
158
|
def send_to_many msisdns, message
|
79
159
|
if @cookie.nil?
|
80
160
|
login_res = login
|
@@ -89,6 +169,17 @@ class SendSms
|
|
89
169
|
return response
|
90
170
|
end
|
91
171
|
|
172
|
+
#
|
173
|
+
# To logout the way2sms session
|
174
|
+
#
|
175
|
+
# @visibility public
|
176
|
+
#
|
177
|
+
# @return [json]
|
178
|
+
# A json with status and message
|
179
|
+
# { :success => true,:message => "Logout successfully" }
|
180
|
+
#
|
181
|
+
#
|
182
|
+
#
|
92
183
|
def logout
|
93
184
|
response = @http.get("/jsp/logout.jsp");
|
94
185
|
@cookie = nil
|
@@ -99,4 +190,85 @@ class SendSms
|
|
99
190
|
return {:success => false,:message => "Logout failed"}
|
100
191
|
end
|
101
192
|
end
|
193
|
+
|
194
|
+
private
|
195
|
+
|
196
|
+
#
|
197
|
+
# Method to fetch the unique identifier in the send sms form
|
198
|
+
# @visibility private
|
199
|
+
# @return [String]
|
200
|
+
# The unique identifier
|
201
|
+
#
|
202
|
+
def getAction
|
203
|
+
headers = set_header @cookie, @referer
|
204
|
+
response = @http.get("/jsp/InstantSMS.jsp",headers.delete_if {|i,j| j.nil? })
|
205
|
+
hdoc = Hpricot(response.body)
|
206
|
+
return (hdoc/"#Action").attr('value')
|
207
|
+
end
|
208
|
+
|
209
|
+
#
|
210
|
+
# To set the Headers for each request
|
211
|
+
#
|
212
|
+
# @param [Sting] cookie
|
213
|
+
# The cookie which need to set
|
214
|
+
# @param [String] referer
|
215
|
+
# The referer which need to set
|
216
|
+
#
|
217
|
+
# @visibility private
|
218
|
+
# @return [Json] A json header
|
219
|
+
#
|
220
|
+
def set_header(cookie=nil,referer=nil)
|
221
|
+
{"Cookie" => cookie , "Referer" => referer ,"Content-Type" => "application/x-www-form-urlencoded",
|
222
|
+
"User-Agent" => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0" }
|
223
|
+
end
|
224
|
+
|
225
|
+
#
|
226
|
+
# To validate and format the msisdn passed by the user
|
227
|
+
# Format the msisdn into 10 digit.
|
228
|
+
# Accepted Formats
|
229
|
+
#
|
230
|
+
# 00919995436867
|
231
|
+
# +919995436867
|
232
|
+
# 919995436867
|
233
|
+
# 9995436867
|
234
|
+
#
|
235
|
+
# @param [String] msisdn
|
236
|
+
# The msisdn which need to validated
|
237
|
+
#
|
238
|
+
# @visibility private
|
239
|
+
# @return [Json]
|
240
|
+
# A Json with status and message
|
241
|
+
#
|
242
|
+
def validate(msisdn = nil)
|
243
|
+
result = /^(\+|00)?(91)?(9|8|7)[0-9]{9}$/.match(msisdn)
|
244
|
+
return {:success => false,:message => "Invalid Msisdn"} if result.nil?
|
245
|
+
msisdn_formated = result[0][-10..-1]
|
246
|
+
return {:success => true,:message => "Valid Msisdn",:msisdn => msisdn_formated}
|
247
|
+
end
|
248
|
+
|
249
|
+
#
|
250
|
+
# The method which post the msisdn, message and unique identifier to way2sms
|
251
|
+
#
|
252
|
+
# @param [String] msisdn
|
253
|
+
# A string of msisdn
|
254
|
+
# @param [String] message
|
255
|
+
# A string of message
|
256
|
+
#
|
257
|
+
# @visibility private
|
258
|
+
#
|
259
|
+
# @return [HttpPost]
|
260
|
+
# The Http post object
|
261
|
+
#
|
262
|
+
#
|
263
|
+
def send_sms msisdn,message
|
264
|
+
headers = set_header @cookie, @referer
|
265
|
+
data = "MobNo=#{msisdn}&textArea=#{message}&HiddenAction=instantsms&login=&pass=&Action=abfghst5654g"
|
266
|
+
response = @http.post("/quicksms.action?custid=\"+custid+\"&sponserid=\"+sponserid+\"",data,headers.delete_if {|i,j| j.nil? })
|
267
|
+
case response.code
|
268
|
+
when /2\d{2}/
|
269
|
+
{:success => true,:message => "Send successfully"}
|
270
|
+
else
|
271
|
+
{:success => false,:message => "Sending failed"}
|
272
|
+
end
|
273
|
+
end
|
102
274
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sendsms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
-
default_executable:
|
12
|
+
date: 2012-03-04 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: hpricot
|
17
|
-
requirement: &
|
16
|
+
requirement: &83129430 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,7 +21,7 @@ dependencies:
|
|
22
21
|
version: '0'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *83129430
|
26
25
|
description: The library Helps you to send SMS via way2sms in India
|
27
26
|
email:
|
28
27
|
- rsk@revathskumar.com
|
@@ -31,7 +30,6 @@ extensions: []
|
|
31
30
|
extra_rdoc_files: []
|
32
31
|
files:
|
33
32
|
- lib/sendsms.rb
|
34
|
-
has_rdoc: true
|
35
33
|
homepage: http://blog.revathskumar.com
|
36
34
|
licenses: []
|
37
35
|
post_install_message:
|
@@ -52,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
50
|
version: '0'
|
53
51
|
requirements: []
|
54
52
|
rubyforge_project:
|
55
|
-
rubygems_version: 1.
|
53
|
+
rubygems_version: 1.8.10
|
56
54
|
signing_key:
|
57
55
|
specification_version: 3
|
58
56
|
summary: Send SMS via way2sms
|