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_send_message_info, #format_time_string, #match_string, #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
# 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') }
end

Instance Method Details

#account_is_availableObject



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

def 
  balance_info = get_balance

  ok = 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, '/smsQuery.php', deltime: :N, checkpoint: :Y, mobile: '', msgid: '')

  format_balance_info(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, '/smsSend.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