Class: Wire2Air

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

Overview

Class for interacting with the Wire2Air sms sending and receiving service. Example usage:

connection = Wire2Air.new(:username => 'your_username',
  :password => 'your password',
  :profile_id => 42,
  :vasid => 12345) # replace the options with the ones found for your account
short_code = 234 # replace with the shortcode from your account you wish to use
test_number = "123456789"
connection.submit_sm(short_code, test_number, "A message to send")

Defined Under Namespace

Classes: AccountUpdateError, CreditCardDeclinedError, FailedAuthenticationError, JobId, KeywordIsTakenError, NotEnoughCreditsError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Wire2Air

Initializes a Wire2Air object with the common profile info needed for making connections.

Options Hash (opts):

  • :username (String)

    The user name to connect as

  • :password (String)

    The password (in plain text) for connecting

  • :profile_id (Integer)

    The id of the profile

  • :vasid (Integer)

    The vasid of the account



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wire2air.rb', line 50

def initialize(opts)
  valid_keys =[:username, :password, :profile_id, :vasid]
  if opts.keys.sort != valid_keys.sort
    raise ArgumentError.new "The options must only have the keys #{valid_keys}"
  end

  opts.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



62
63
64
# File 'lib/wire2air.rb', line 62

def password
  @password
end

#profile_idObject (readonly)

Returns the value of attribute profile_id.



62
63
64
# File 'lib/wire2air.rb', line 62

def profile_id
  @profile_id
end

#usernameObject (readonly) Also known as: userid

Returns the value of attribute username.



62
63
64
# File 'lib/wire2air.rb', line 62

def username
  @username
end

#vasidObject (readonly)

Returns the value of attribute vasid.



62
63
64
# File 'lib/wire2air.rb', line 62

def vasid
  @vasid
end

Instance Method Details

#check_keyword(short_code, keyword) ⇒ Object

Checks whether the keyword can be registered.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/wire2air.rb', line 156

def check_keyword(short_code, keyword)
  url = URI.parse('http://mzone.wire2air.com/mserver/servicemanager/api/checkkeywordapi.aspx')
  response = Net::HTTP.post_form(url, {
      'USERID' => userid,
      'PASSWORD' => password,
      'VASID' => vasid,
      'SHORTCODE' => short_code,
      'KEYWORD' => keyword
  })

  case response.body
    when /Err:0/
      return true
    when /Err:705/
      return false
    when /Err:301/
      raise FailedAuthenticationError
    else
      raise StandardError.new response.body
  end

end

#check_sms_creditsObject

returns the number of credits available



141
142
143
144
145
146
147
148
149
150
# File 'lib/wire2air.rb', line 141

def check_sms_credits
  url = URI.parse('http://smsapi.wire2air.com/smsadmin/checksmscredits.aspx')
  res = Net::HTTP.post_form(url, {
      'USERID' => userid,
      'PASSWORD' => password,
      'VASID' => vasid
  }).body
  raise FailedAuthenticationError if res =~ /ERR:301/
  res.to_i
end

#register_keyword(opts) ⇒ Integer

Wire2air provides simple HTTP interface for clients to register available keyword for a given short code. :delete -> Delete keyword from service Default is :add

Options Hash (opts):

  • :service_name (String)

    Service name for the keyword

  • :short_code (String)
  • :keyword (String)
  • :processor_url (String)

    The url of the webservice

  • :help_msg (String)

    Response for help message

  • :stop_msg (String)

    Responce for opt-out message

  • :action (String)

    :add -> Register new service with keyword

Raises:

  • ArgumentError If arguments are missing/invalid

  • KeywordIsTakenError



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/wire2air.rb', line 195

def register_keyword(opts)
  url = URI.parse('http://mzone.wire2air.com/mserver/servicemanager/api/checkkeywordapi.aspx')
  params = {}
  params['USERID'] = username
  params['PASSWORD'] = password
  params['VASID'] = vasid
  params['SHORTCODE'] = opts[:short_code]
  params['SERVICENAME'] = opts[:service_name]
  params['KEYWORD'] = opts[:keyword]
  params['PROCESSORURL'] = opts[:processor_url]
  params['HELPMSG'] = opts[:help_msg]
  params['STOPMSG'] = opts[:stop_msg]
  params['ACTION'] = (opts[:action] == :delete) ? "DELETE" : "ADD"

  res = Net::HTTP.post_form(url, params).body
  puts res

  case res
    when /Err:70[012346789]/, /Err:71[0134]/
      raise ArgumentError.new res
    when /Err:300/
      raise FailedAuthenticationError
    when /Err:705/
      raise KeywordIsTakenError
    when /Err:712/
      raise "Sticky session is not allowed"
  end

  res.match(/SERVICEID:(\d+)/)[1].to_i

end

#submit_sm(from, mobile_number, text, opts = {}) ⇒ JobId, Integer

sends an sms what is passed from incoming sms message for the HTTP API. an Integer for the BatchID is returned.

Options Hash (opts):

  • :batch_reference (String)

    A reference string used when sending many sms

  • :network_id (String)

    Th id of the destination network. This is the same as

Raises:

  • NotEnoughError Not enough credits to send the sms

  • FailedAuthenticationError some authentication details are wrong



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wire2air.rb', line 81

def submit_sm(from, mobile_number, text, opts = {})
  params = common_options
  params['VERSION'] = '2.0'
  params['FROM'] = from
  params['TEXT'] = text
  params['NETWORKID'] = opts[:network_id] if opts.has_key? :network_id
  batch_send = !(mobile_number.is_a? String)

  if !batch_send
    params['TO'] = mobile_number
  else
    params['TO'] = mobile_number.join(',')
    params['BATCHNAME'] = opts[:batch_reference]

  end

  url = URI.parse('http://smsapi.wire2air.com/smsadmin/submitsm.aspx')
  res = Net::HTTP.post_form(url, params).body
  case res
    when /^ERR: 301/
      raise FailedAuthenticationError
    when /^ERR: 305/
      raise NotEnoughCreditsError
  end
  if (batch_send)
    res.match(/BATCHID: \d+/)[1]
  else
    puts res
    JobId.from_s(res)
  end
end

#subscribe_keywords(keyword_credits = 1) ⇒ void

This method returns an undefined value.

Adds some credits to the account



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wire2air.rb', line 116

def subscribe_keywords(keyword_credits = 1)
  url = URI.parse('http://mzone.wire2air.com/mserver/api/subscribekeywords.aspx')
  res = Net::HTTP.post_form(url, {
      'USERID' => userid,
      'PASSWORD' => password,
      'KEYWORDREDITS' => keyword_credits

  })
  case res.body
    when /^Err:70[34]/
      raise ArgumentError.new "Missing username or password"
    when /^Err:705/
      raise KeywordIsTakenError
    when /^Err:300/
      raise FailedAuthenticationError
    when /^Err:715/
      raise AccountUpdateError
    when /^Err:716/
      raise CreditCardDeclinedError
  end

end