Class: Kotsms2::Client

Inherits:
Object
  • Object
show all
Includes:
Formatter, Network
Defined in:
lib/kotsms2.rb

Instance Method Summary collapse

Methods included from Formatter

#format_balance_info, #format_message_status, #format_send_message_info, #format_time_string, #match_string, #message_status_sanitize, #to_asia_taipei_timezone, #to_big5, #to_utf8

Methods included from Network

#escape, #get, #parse, #query_string, #request

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/kotsms2.rb', line 11

def initialize(options={})
  @user_agent = options.fetch(:agent) { "kotsms2/#{VERSION}" }
  @api_host   = options.fetch(:host) { 'api.kotsms.com.tw' }
  @username   = options.fetch(:username) { ENV.fetch('KOTSMS_USERNAME') }
  @password   = options.fetch(:password) { ENV.fetch('KOTSMS_PASSWORD') }
  @timeout    = options.fetch(:timeout) { 10 }
end

Instance Method Details

#account_is_availableObject



19
20
21
22
# File 'lib/kotsms2.rb', line 19

def 
  balance_info = get_balance
  balance_info[:message_quota] > 0 && balance_info[:access_success]
end

#get_balanceObject



40
41
42
43
44
# File 'lib/kotsms2.rb', line 40

def get_balance
  response = get(@api_host, '/memberpoint.php')

  format_balance_info(response)
end

#get_message_status(options = {}) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/kotsms2.rb', line 46

def get_message_status(options={})
  options[:message_id] ||= nil

  response = get(@api_host, '/msgstatus.php', kmsgid: options[:message_id])

  format_message_status(response)
end

#send_message(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kotsms2.rb', line 24

def send_message(options={})
  options[:to] ||= nil
  options[:heavy_loading] ||= false

  options[:content] = options[:content].to_s
  options[:at] = format_time_string(options[:at])
  options[:at] = 0 if options[:at].nil? # 據 Kotsms 文件指出,如果要更即時,可以指定為 0,比不給參數還快

  api_path = '/kotsmsapi-1.php' 
  api_path = '/kotsmsapi-2.php' if options[:heavy_loading]

  response = get(@api_host, api_path, dstaddr: options[:to], smbody: to_big5(options[:content]), dlvtime: options[:at])

  format_send_message_info(response)
end