class Coolsms::Client
Constants
- COOLSMS_URL
Public Class Methods
new(options)
click to toggle source
# 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
Public Instance Methods
send_message(options)
click to toggle source
# 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
Private Instance Methods
parse_response(res)
click to toggle source
# File lib/coolsms/client.rb, line 40 def parse_response(res) parsed_response = JSON.parse(res.parsed_response) { :success => res.code > 200 ? false : true, :error_count => parsed_response['error_count'], :success_count => parsed_response['success_count'], :result_message => parsed_response['result_message'], :status_code => res.code } end
salt()
click to toggle source
# File lib/coolsms/client.rb, line 45 def salt SecureRandom.hex(30) end
signature(ts,slt)
click to toggle source
# File lib/coolsms/client.rb, line 53 def signature(ts,slt) digest = OpenSSL::Digest.new 'md5' OpenSSL::HMAC.hexdigest digest, @api_secret, "#{ts}#{slt}" end
timestamp()
click to toggle source
# File lib/coolsms/client.rb, line 49 def timestamp Time.now.to_i end