Class: SmsGlobal::Sender

Inherits:
Object
  • Object
show all
Includes:
Net
Defined in:
lib/smsglobal.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sender

Returns a new instance of Sender.



8
9
10
11
12
13
# File 'lib/smsglobal.rb', line 8

def initialize(options = {})
  @options = options
  raise 'missing :user' unless @options[:user]
  raise 'missing :password' unless @options[:password]
  @base = @options[:base] || 'http://www.smsglobal.com/'
end

Instance Method Details

#send_text(text, to, sender = nil, send_at = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smsglobal.rb', line 15

def send_text(text, to, sender = nil, send_at = nil)
  from = sender || @options[:from] || raise_error('sender is required')
  params = {
    :action => 'sendsms',
    :user => @options[:user],
    :password => @options[:password],
    :from => from,
    :to => to,
    :text => text
  }
  if send_at
    params[:scheduledatetime] = send_at.strftime('%Y-%m-%d %h:%M:%S')
  end
  params[:maxsplit] = @options[:maxsplit] if @options[:maxsplit]

  resp = get(params)

  case resp
  when Net::HTTPSuccess
    if resp.body =~ /^OK: (\d+); ([^\n]+)\s+SMSGlobalMsgID:\s*(\d+)\s*$/
      return {
        :status  => :ok,
        :code    => $1.to_i,
        :message => $2,
        :id      => $3.to_i
      }
    elsif resp.body =~ /^ERROR: (.+)$/
      return {
        :status  => :error,
        :message => $1
      }
    else
      raise "Unable to parse response: '#{resp.body}'"
    end
  else
    return {
      :status  => :failed,
      :code    => resp.code,
      :message => 'Unable to reach SMSGlobal'
    }
  end
end