Class: Kotsms

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

Constant Summary collapse

API_HOST =
"api.kotsms.com.tw"
SMS_ENDPOINT =
"/kotsmsapi-1.php"
BULK_SMS_ENDPOINT =
"/kotsmsapi-2.php"
BALANCE_ENDPOOINT =
"/memberpoint.php"
STATUS_ENDPOOINT =
"/msgstatus.php"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Kotsms

Returns a new instance of Kotsms.



12
13
14
# File 'lib/kotsms.rb', line 12

def initialize(username, password)
	(username, password)
end

Instance Method Details

#balanceObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kotsms.rb', line 65

def balance
	uri = URI.parse(BALANCE_ENDPOOINT)
	uri.query = URI.encode_www_form({
		username: @username,
		password: @password
	})

	response = Net::HTTP.get_response(uri)

	response.body.to_i
end

#deliver(recipient, message, options = {}) ⇒ Object

recipient (string) - sms recipient in general format; e.g. ‘+886912345678’ message (string) - message content options (hash) - optional config options.ignore_cert (boolean) - Ignore SSL certificate or not options.insecure (boolean) - Use plain HTTP or HTTPS options.mode (string) - delivery mode

'bit' - instant delivery (default)
'bulk' - bulk delivery


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
57
58
59
# File 'lib/kotsms.rb', line 30

def deliver(recipient, message, options={})
	protocol = options[:insecure] ? "http" : "https"
	uri = URI.parse "#{protocol}://#{API_HOST}"
	uri.path = case (options[:mode].to_sym rescue nil)
			   when nil, :bit
				   SMS_ENDPOINT
			   when :bulk
				   BULK_SMS_ENDPOINT
			   else
				   raise StandardError.new "Bad delivering mode!"
			   end

	uri.query = URI.encode_www_form({
		username: @username,
		password: @password,
		dstaddr: recipient,
		smbody: Iconv.new("big5", "utf-8").iconv(message),
		dlvtime: (options[:dlvtime] rescue 0),
		vldtime: (options[:vldtime] rescue nil),
		response: (options[:response] rescue nil)
	})

	response = Net::HTTP.start(uri.host, use_ssl: uri.scheme == 'https') do |http|
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:ignore_cert]
		req = Net::HTTP::Get.new uri
		http.request(req)
	end

	parse_response(response.body)["kmsgid"].to_i
end

#deliver_bulk(recipient, message, options = {}) ⇒ Object



61
62
63
# File 'lib/kotsms.rb', line 61

def deliver_bulk(recipient, message, options={})
	deliver(recipient, message, options.merge(mode: :bulk))
end

#login(username, password) ⇒ Object



16
17
18
19
20
# File 'lib/kotsms.rb', line 16

def (username, password)
	@username = username
	@password = password
	true
end

#status(kmsgid) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kotsms.rb', line 77

def status(kmsgid)
	uri = URI.parse(STATUS_ENDPOOINT)
	uri.query = URI.encode_www_form({
		username: @username,
		password: @password,
		kmsgid: kmsgid
	})

	response = Net::HTTP.get_response(uri)

	parse_response(response.body)["statusstr"]
end