Class: Coolsms::Client

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

Constant Summary collapse

COOLSMS_URL =
"https://api.coolsms.co.kr/1/send"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/coolsms/client.rb', line 10

def initialize(options)
  raise ArgumentError.new "sender must not be nil" unless options[:sender]
  raise ArgumentError.new "api_key must not be nil" unless options[:api_key]
  raise ArgumentError.new "api_secret must not be nil" unless options[:api_secret]

  @api_key = options[:api_key]
  @api_secret = options[:api_secret]
  @sender = options[:sender]
end

Instance Method Details

#send_message(options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/coolsms/client.rb', line 20

def send_message(options)
  raise ArgumentError.new "to must not be nil" unless options[:to]
  raise ArgumentError.new "text must not be nil" unless options[:text]

  ts = timestamp
  slt = salt
  signature = signature(ts,slt)
  type = options[:text].bytesize > 80 ? 'LMS' : 'SMS'
  body = { :api_key => @api_key, :type => type, :salt => slt, :signature => signature, :to => options[:to], :timestamp => ts, :from => @sender, :text => options[:text] }

  if type == 'LMS'
    body.merge! :subject => options[:subject] ? options[:subject] : ""
  end

  res = HTTParty.post(COOLSMS_URL, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, :body => body)
  parse_response res
end