kotsms2 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5296875c0b1b8a755e8552f54a58660f26349f70
4
- data.tar.gz: 22e473ec7a42c8b02a2f2c84ea096a0e24e3dc7e
3
+ metadata.gz: 683a33d67cc7f8b8c5f25c39a2b24a9d9f20b000
4
+ data.tar.gz: ca2fad420aaff2d8851673ba906d3748561c72a7
5
5
  SHA512:
6
- metadata.gz: 8e6301990768aa5a7ea71ea42a7bf039bbaf8a75cb7ceefbc827ecdfbe3f413e5fa6eb8eb880f1e96097f131f5d405bfec562579670975452e90f03f964a386a
7
- data.tar.gz: f7e1889421d00d4bea51523c8fee0969d18434c29feca67faa556d55128c15b587e9737a872d514b722ff1743a11a9a5832acbedc0669d92fa3b104ba075722d
6
+ metadata.gz: bab1abc6f97a79aa52631491e826086fa5d654985bb7405554d4b4e61562812645b88b05d856699f08d340136e9b864ba9631eb7b9a4998f4d4642daa2fdf76b
7
+ data.tar.gz: 67ce2000bf30f8aa860aee138d061e779593dd26ec5e92368f1f3f4ee23a8e1ad7854fe0c638870b3f3651906dc836dc20d1cb9e4a7e04da2a37b12cb85afbaf
data/README.md CHANGED
@@ -22,7 +22,7 @@ Ruby 2 以上的版本,也可在 Ruby On Rails 專案引入
22
22
  請在您的 Ruby 或 Rails 專案裡的 Gemfile 加入以下指令
23
23
 
24
24
  ```ruby
25
- gem 'kotsms2', '~> 1.0.0'
25
+ gem 'kotsms2', '~> 1.1.0'
26
26
  ```
27
27
 
28
28
  然後執行 bundle install 更新套件組
@@ -43,9 +43,18 @@ gem 'kotsms2', '~> 1.0.0'
43
43
  ```ruby
44
44
  require 'kotsms2'
45
45
 
46
- # Kotsms2 是走 https 的方式進行系統操作
46
+ # 程式將會以 SSL 的方式,走 https 連線到簡訊商的系統
47
+ sms_client = Kotsms2::Client.new(username: '會員帳號', password: '會員密碼')
48
+ ```
49
+
50
+ 也可以加入 agent ( user-agent ) 跟 timeout ( 逾時時間/秒 ) 參數 至 client 物件
51
+
52
+ ```ruby
47
53
  sms_client = Kotsms2::Client.new(
48
- username: '會員帳號', password: '會員密碼', agent: "Mozilla/5.0 (可自訂 user-agent)"
54
+ username: '會員帳號',
55
+ password: '會員密碼',
56
+ agent: "Mozilla/5.0 ( Hello World )",
57
+ timeout: 10
49
58
  )
50
59
  ```
51
60
 
@@ -131,7 +140,7 @@ error code 的部分,請以 簡訊王 API 文件的定義為主,本套件不
131
140
 
132
141
  ----
133
142
 
134
- ### 查詢簡訊餘額
143
+ ### 查詢目前帳號所持有的簡訊餘額
135
144
 
136
145
  若你需要查詢您會員帳號的簡訊餘額,可以用以下指令處理
137
146
 
@@ -139,7 +148,7 @@ error code 的部分,請以 簡訊王 API 文件的定義為主,本套件不
139
148
  sms_client.get_balance
140
149
  ```
141
150
 
142
- ### 查詢簡訊餘額 的 回傳結果
151
+ ### 查詢目前帳號所持有的簡訊餘額 的 回傳結果
143
152
 
144
153
  #### 得到簡訊餘額
145
154
 
@@ -162,6 +171,75 @@ message_quota 則是簡訊餘額,代表你還剩幾封可以用,若為 0 就
162
171
  {:access_success=>false, :message_quota=>0, :error=>"KOTSMS:-2"}
163
172
  ```
164
173
 
174
+ ### 查詢特定的簡訊發送狀態
175
+
176
+ 若你需要查詢特定的簡訊發送狀態,您可以指定 message id 向簡訊商查詢該封簡訊最後是否已發送成功
177
+
178
+ ```ruby
179
+ sms_client.get_message_status message_id: '在 send_message 得到的 message_id'
180
+ ```
181
+
182
+ ### 查詢特定的簡訊發送狀態 的 回傳結果
183
+
184
+ #### 得到發送狀況
185
+
186
+ 當你執行完成後,get_message_status 方法會回傳一組 hash 型態的結果
187
+
188
+ 只要 access_success 的值為 true 就一定代表系統有成功取得資料
189
+
190
+ is_delivered 代表是否已寄到用戶手機,true 為是、false 為有發生 delivered 以外的狀況
191
+
192
+ message_status 代表訊息狀態,可以知道是否已抵達 或是 發生通信上的錯誤 等等的相關資訊
193
+
194
+ ```ruby
195
+ {:access_success=>true, :is_delivered=>true, :message_status=>"delivered", :error=>nil}
196
+ ```
197
+
198
+ #### get_message_status 裡的 message_status 涵義
199
+
200
+ ```ruby
201
+ 'delivered' # 簡訊已抵達
202
+ 'expired' # 簡訊寄送超過有效時間
203
+ 'deleted' # 已被刪除
204
+ 'undelivered' # 無法送達
205
+ 'transmitting' # 傳輸中,正在接收
206
+ 'unknown' # 未知錯誤,可能無效
207
+ 'rejected' # 被拒絕
208
+ 'incorrect_sms_system_syntax' # 簡訊商編碼錯誤
209
+ 'status_undefined' # 核心 API 無法得知狀況
210
+ ```
211
+
212
+ #### 發生錯誤時
213
+
214
+ 若 access_success 為 false 則表示過程有出現錯誤,同時 is_delivered 會為 false,message_status 會是 'status_undefined'
215
+
216
+ ```ruby
217
+ {:access_success=>false, :is_delivered=>false, :message_status=>nil, :error=>"KOTSMS:MEMBERERROR"}
218
+ {:access_success=>false, :is_delivered=>false, :message_status=>nil, :error=>"KOTSMS:NOSMS"}
219
+ ```
220
+
221
+ ----
222
+
223
+ 例外狀況的處理
224
+ --------
225
+
226
+ 在某些情況下,程式會擲出一些例外給 ruby 去處理,你可以先用 Kotsms2 自帶的 Error 來先做 rescue
227
+
228
+ ```ruby
229
+
230
+ begin
231
+ sms_client.account_is_available
232
+ rescue Kotsms2::ClientError
233
+ 'Client 物件有內部錯誤'
234
+ rescue Kotsms2::ServerError => error
235
+ "伺服器端有一些無法處理的狀況 #{error.message}"
236
+ rescue Kotsms2::ClientTimeoutError
237
+ "發生 Timeout 囉"
238
+ rescue Kotsms2::Error => error
239
+ "發生非預期的錯誤 #{error.message}"
240
+ end
241
+
242
+ ```
165
243
 
166
244
  LICENSE
167
245
  --------
@@ -2,4 +2,5 @@ module Kotsms2
2
2
  class Error < StandardError; end
3
3
  class ClientError < Error; end
4
4
  class ServerError < Error; end
5
+ class ClientTimeoutError < Error; end
5
6
  end
@@ -25,6 +25,45 @@ module Kotsms2
25
25
  asia_taipei_time
26
26
  end
27
27
 
28
+ def message_status_sanitize(original_text)
29
+ status_table = {
30
+ 'SUCCESSED' => 'delivered',
31
+ 'DELIVRD' => 'delivered',
32
+ 'EXPIRED' => 'expired',
33
+ 'DELETED' => 'deleted',
34
+ 'UNDELIV' => 'undelivered',
35
+ 'ACCEPTD' => 'transmitting',
36
+ 'UNKNOWN' => 'unknown',
37
+ 'REJECTD' => 'rejected',
38
+ 'SYNTAXE' => 'incorrect_sms_system_syntax'
39
+ }
40
+
41
+ new_text = status_table[original_text]
42
+ new_text.nil? ? 'status_undefined' : new_text
43
+ end
44
+
45
+ def format_message_status(original_info)
46
+ new_info = {
47
+ access_success: false,
48
+ is_delivered: false,
49
+ message_status: nil,
50
+ error: nil
51
+ }
52
+
53
+ status_text = match_string(/^statusstr=(?<status>\w+)$/, original_info)
54
+
55
+ new_info[:access_success] = !status_text.nil? && status_text != 'MEMBERERROR' && status_text != 'NOSMS'
56
+
57
+ if new_info[:access_success]
58
+ new_info[:message_status] = message_status_sanitize(status_text)
59
+ new_info[:is_delivered] = new_info[:message_status] == 'delivered'
60
+ else
61
+ new_info[:error] = status_text.nil? ? "KOTSMS:CODE_NOT_FOUND" : "KOTSMS:#{status_text}".upcase
62
+ end
63
+
64
+ new_info
65
+ end
66
+
28
67
  def format_send_message_info(original_info)
29
68
  new_info = {
30
69
  access_success: false,
@@ -21,6 +21,8 @@ module Kotsms2
21
21
  raise ClientError, "#{http_response.code} response from #{host}"
22
22
  when Net::HTTPServerError
23
23
  raise ServerError, "#{http_response.code} response from #{host}"
24
+ when 'READ_TIMEOUT'
25
+ raise ClientTimeoutError, "Read Timeout from #{host}"
24
26
  else
25
27
  raise Error, "#{http_response.code} response from #{host}"
26
28
  end
@@ -28,8 +30,11 @@ module Kotsms2
28
30
 
29
31
  def request(uri, message)
30
32
  http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
33
+ http.read_timeout = @timeout
31
34
  http.use_ssl = true
32
35
  http.request(message)
36
+ rescue Net::ReadTimeout
37
+ 'READ_TIMEOUT'
33
38
  end
34
39
 
35
40
  def query_string(params)
@@ -1,3 +1,3 @@
1
1
  module Kotsms2
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/kotsms2.rb CHANGED
@@ -13,6 +13,7 @@ module Kotsms2
13
13
  @api_host = options.fetch(:host) { 'api.kotsms.com.tw' }
14
14
  @username = options.fetch(:username) { ENV.fetch('KOTSMS_USERNAME') }
15
15
  @password = options.fetch(:password) { ENV.fetch('KOTSMS_PASSWORD') }
16
+ @timeout = options.fetch(:timeout) { 10 }
16
17
  end
17
18
 
18
19
  def account_is_available
@@ -41,5 +42,14 @@ module Kotsms2
41
42
 
42
43
  format_balance_info(response)
43
44
  end
45
+
46
+ def get_message_status(options={})
47
+ options[:message_id] ||= nil
48
+
49
+ response = get(@api_host, '/msgstatus.php', kmsgid: options[:message_id])
50
+
51
+ format_message_status(response)
52
+ end
53
+
44
54
  end
45
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kotsms2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guanting Chen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-04 00:00:00.000000000 Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.5.1
97
+ rubygems_version: 2.6.4
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: 2016 新版 簡訊王 KotSMS API ( 純 Ruby / Rails 專案適用 )