Class: SendSms

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

Overview

The class which handles sending sms with way2sms

Author:

  • Revath S Kumar

Constant Summary collapse

URL =

The way2sms root url

'http://site6.way2sms.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, auto_logout = true) ⇒ SendSms

Initiate Sendsms class with way2sms username and password

Parameters:

  • username (String)

    way2sms username

  • password (String)

    way2sms password

  • auto_logout (true, false) (defaults to: true)

    Set to logout after sending SMS. we don’t need to call the logout explicitly Recommended to turn it off when you are sending SMS repeatedly



46
47
48
49
50
51
52
53
54
# File 'lib/sendsms.rb', line 46

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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



31
32
33
# File 'lib/sendsms.rb', line 31

def password
  @password
end

#usernameObject

Returns the value of attribute username.



31
32
33
# File 'lib/sendsms.rb', line 31

def username
  @username
end

Instance Method Details

#loginjson

Login to way2sms.com

Returns:

  • (json)

    A json response with status and message

    { :success => true, :message => "Login Successful" }
    { :success => false, :message => "Login Failed" }
    


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sendsms.rb', line 65

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

#logoutjson

To logout the way2sms session

Returns:

  • (json)

    A json with status and message

    { :success => true,:message => "Logout successfully" }
    


184
185
186
187
188
189
190
191
192
193
# File 'lib/sendsms.rb', line 184

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(msisdns, message) ⇒ json

To send Individual and Group SMS This method support Group SMS from version 0.0.5

Parameters:

  • msisdns (Mixed)

    The msisdn/msisdns to send the SMS

    Individual
      A single msisdn as String Eg: "9995436867"
    Group
      An array of msisdns
        Eg: ["9995436867","9037107542","9037864203"]
      An hash of msisdns
        Eg: {0 => "9995436867",1 => "9037107542",2 => "9037864203"}
      A semicolon(;) seperated string of msisdns
        Eg: "9995436867;9037107542;9037864203"
    
  • message (String)

    The message to send

Returns:

  • (json)

    A json response with status and message

    Individual
      {:success => true,:message => "Send successfull"}
      {:success => false,:message => "Send failed"}
    Group
      {
        "9995436867" => {:success => true,:message => "Send successfully"},
        "9037864203" => {:success => true,:message => "Send successfully"},
        "9037107542" => {:success => true,:message => "Send successfully"}
      }
    


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sendsms.rb', line 113

def send msisdns,message
  if @cookie.nil?
     = 
    return {:success => false,:message => "Login failed"} if ![:success]
  end
  if msisdns.kind_of?(String) && !msisdns.include?(";")
    response = send_sms msisdns,message
    logout if @auto_logout
    return response
  else
    if msisdns.kind_of?(String) && msisdns.include?(";")
      msisdns = msisdns.split(';')
    end
    response = {}
    msisdns.each do | key, msisdn |
      msisdn = key if msisdn.nil?
      response[msisdn] = send_sms msisdn,message
    end
    logout if @auto_logout
    return response
  end
end

#send_to_many(msisdns, message) ⇒ json

Deprecated.

Use #send instead of this method

To send Group SMS

Parameters:

  • msisdns (String)

    A semicolon seperated string of msisdns Eg: “9995436867;9037107542;9037864203”

  • message (String)

    The message to send

Returns:

  • (json)

    A json response with status and message

    {
      "9995436867" => {:success => true,:message => "Send successfully"},
      "9037864203" => {:success => true,:message => "Send successfully"},
      "9037107542" => {:success => true,:message => "Send successfully"}
    }
    


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

def send_to_many msisdns, message
  @auto_logout = false
  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