Class: Sgsms::Texter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Texter



10
11
12
13
# File 'lib/sgsms.rb', line 10

def initialize(opts={})
  @options = opts
  @options[:from] = "sgsms" unless @options[:from]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/sgsms.rb', line 15

def options
  @options
end

Instance Method Details

#send_sms(message, to, opts = {}) ⇒ Object



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
# File 'lib/sgsms.rb', line 17

def send_sms(message, to, opts={})
  sms_params = {
    :user => opts[:user] || @options[:user], 
    :password => opts[:password] || @options[:password],
    :from => opts[:from] || @options[:from],
    :action => 'sendsms',
    :text => message,
    :to => to
  }
  
  raise :user_missing unless sms_params[:user]
  raise :password_missing unless sms_params[:password]
  raise :from_missing unless sms_params[:from]

  url = URI.join("http://smsglobal.com.au", 'http-api.php')
  url.query = sms_params.map {|k,v| "#{k.to_s}=#{v.to_s}"}.join('&')
  response = Net::HTTP.get_response(url)

  #puts response
  #return {:status => :ok, :id => 1 }
  case response
  when Net::HTTPSuccess
    if response.body.match(/^OK: 0; .+ ID: ([\dabcdef]+).*SMSGlobalMsgID:(\d+)/m)
      return {:status => :ok, :id => $1.to_s, :msgid => $2.to_i}
    elsif response.body.match(/^ERROR: (.*): (.*)$/)
      return {:status => :fail, :error => $1, :detail => $2}
    else
      raise "Cannot parse body: #{response.body}"
    end
  else
    raise "Net error: #{response.inspect}"
  end
          
end