Class: Twsms2::Client

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

Instance Method Summary collapse

Methods included from Formatter

#format_balance_info, #format_message_status, #format_send_message_info, #format_time_string, #message_status_sanitize, #to_asia_taipei_timezone

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/twsms2.rb', line 11

def initialize(options={})
  @user_agent = options.fetch(:agent) { "twsms2/#{VERSION}" }
  @api_host   = options.fetch(:host) { 'api.twsms.com' }
  @username   = options.fetch(:username) { ENV.fetch('TWSMS_USERNAME') }
  @password   = options.fetch(:password) { ENV.fetch('TWSMS_PASSWORD') }
  @timeout    = options.fetch(:timeout) { 10 }
end

Instance Method Details

#account_is_availableObject



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

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

#get_balanceObject



36
37
38
39
40
# File 'lib/twsms2.rb', line 36

def get_balance
  response = get(@api_host, '/json/sms_query.php', deltime: :N, checkpoint: :Y, mobile: '', msgid: '')

  format_balance_info(response)
end

#get_message_status(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/twsms2.rb', line 42

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

  response = get(@api_host, '/json/sms_query.php', mobile: options[:phone_number], msgid: options[:message_id])

  format_message_status(response)
end

#send_message(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twsms2.rb', line 24

def send_message(options={})
  options[:to]      ||= nil
  options[:content] ||= nil
  options[:at]      = format_time_string(options[:at])
  options[:long]    = options[:long] || options[:long].nil? ? :Y : :N
  options[:popup]   = options[:popup] ? :Y : :N

  response = get(@api_host, '/json/sms_send.php', popup: options[:popup], mo: :N, longsms: options[:long], mobile: options[:to], message: options[:content], drurl: '', sendtime: options[:at])

  format_send_message_info(response)
end