Class: Websms::Sender

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

Class Method Summary collapse

Class Method Details

.send_message(to, text) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/websms/sender.rb', line 7

def self.send_message(to, text)
  # Return immediatly and silently if user does not set SMS number
  return false if to.empty?
  raise ArgumentError, "Empty text, wont send it" if text.empty?

  # Get configuration
  c = Websms.configuration

  # SMS Gate settings
  sms_gateway = "http://websms.ru/http_in4.asp"
  sms_user = c.api_user
  sms_password = c.api_password
  # Filter number
  number = to.gsub(/[^\d]/, '')
  # Prepare text
  text = "#{c.client_id}: #{text}" if c.client_id
  etext = text.gsub(' ', '%20')
  etext = Iconv.conv('windows-1251', 'utf-8', etext)

  from_phone = c.alfanum_from ? "&fromPhone=#{c.alfanum_from}" : ''
  # Send request
  request = "#{sms_gateway}?http_username=#{sms_user}&http_password=#{sms_password}#{from_phone}&Phone_list=#{number}&Message=#{etext}"
  c = Curl::Easy.perform(request)
end