Class: Kotsms

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

Constant Summary collapse

SMS_ENDPOINT =
"https://api.kotsms.com.tw/kotsmsapi-1.php"
BULK_SMS_ENDPOINT =
"https://api.kotsms.com.tw/kotsmsapi-2.php"
BALANCE_ENDPOOINT =
"http://mail2sms.com.tw/memberpoint.php"
STATUS_ENDPOOINT =
"http://mail2sms.com.tw/msgstatus.php"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Kotsms

Returns a new instance of Kotsms.



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

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

Instance Method Details

#balanceObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kotsms.rb', line 51

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(dst, content, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kotsms.rb', line 21

def deliver(dst, content, options={})
  endpoint = 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 = URI.parse(endpoint)

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

  response = Net::HTTP.get_response(uri)

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

#deliver_bulk(dst, content, options = {}) ⇒ Object



47
48
49
# File 'lib/kotsms.rb', line 47

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

#login(username, password) ⇒ Object



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

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

#status(kmsgid) ⇒ Object



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

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