Class: SendSms

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

Constant Summary collapse

URL =
'http://site6.way2sms.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = "", password = "", autologin = true) ⇒ SendSms

Returns a new instance of SendSms



11
12
13
14
15
16
17
18
# File 'lib/sendsms.rb', line 11

def initialize(username = "", password = "",autologin = true)
  @username = username
  @password = password
  @uri = URI.parse URL
  @cookie = @action = nil
  @referer = URL
  @http = Net::HTTP.new(@uri.host,@uri.port)
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password



7
8
9
# File 'lib/sendsms.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username



7
8
9
# File 'lib/sendsms.rb', line 7

def username
  @username
end

Instance Method Details

#getActionObject



50
51
52
53
54
55
# File 'lib/sendsms.rb', line 50

def getAction
  headers = set_header @cookie, @referer
  response = @http.get("/jsp/InstantSMS.jsp",headers.delete_if {|i,j| j.nil? })
  hdoc = Hpricot(response.body)
  return (hdoc/"#Action").attr('value')  
end

#loginObject



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

def 
  data = 'username='+@username+'&password='+@password
  headers = set_header @cookie, @referer
  response = @http.post("/Login1.action",data,headers.delete_if {|i,j| j.nil? })
  case response.code
    when  /3\d{2}/
      if response['location'].include?("Main.action")
        @cookie ||= response['set-cookie']
        @referer ||= response['referer']
        @action = getAction
        return {:success => true,:message => "Login successfully"}
      end
      return {:success => false,:message => "Login failed"}
    else
      return {:success => false,:message => "Http Error"}
  end
end

#logoutObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/sendsms.rb', line 92

def logout
  response = @http.get("/jsp/logout.jsp");
  @cookie = nil
  case response.code
    when  /2\d{2}/
      return {:success => true,:message => "Logout successfully"}
    else
      return {:success => false,:message => "Logout failed"}
  end
end

#send(msisdn, message) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sendsms.rb', line 63

def send msisdn,message
  if @cookie.nil?
     = 
    return {:success => false,:message => "Login failed"} if ![:success]
  end

  response = send_sms msisdn,message
  case response.code
    when  /2\d{2}/
      return {:success => true,:message => "Send successfully"}
    else
      return {:success => false,:message => "Sending failed"}
  end
end

#send_sms(msisdn, message) ⇒ Object



57
58
59
60
61
# File 'lib/sendsms.rb', line 57

def send_sms msisdn,message
  headers = set_header @cookie, @referer
  data = "MobNo=#{msisdn}&textArea=#{message}&HiddenAction=instantsms&login=&pass=&Action=#{@action}"
  return @http.post("/quicksms.action?custid=\"+custid+\"&sponserid=\"+sponserid+\"",data,headers.delete_if {|i,j| j.nil? })
end

#send_to_many(msisdns, message) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sendsms.rb', line 78

def send_to_many msisdns, message
  if @cookie.nil?
     = 
    return {:success => false,:message => "Login failed"} if ![:success]
  end

  msisdns = msisdns.split(';')
  response = {}
  msisdns.each do | msisdn |
    response[msisdn] = send msisdn,message
  end
  return response
end

#set_header(cookie = nil, referer = nil) ⇒ Object



20
21
22
23
# File 'lib/sendsms.rb', line 20

def set_header(cookie=nil,referer=nil)
  {"Cookie" => cookie , "Referer" => referer ,"Content-Type" => "application/x-www-form-urlencoded",
    "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" }
end

#validate(msisdn = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/sendsms.rb', line 25

def validate(msisdn = nil)
  result = /^(\+|00)?(91)?(9|8|7)[0-9]{9}$/.match(msisdn)
  return {:success => false,:message => "Invalid Msisdn"} if result.nil?
  msisdn_formated = result[0][-10..-1]
  return {:success => true,:message => "Valid Msisdn",:msisdn => msisdn_formated}
end