twsms2 1.1.1 → 1.2.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: 604e4232d10dd76ea6064e215fae633f8b020746
4
- data.tar.gz: 5585e90bff7241bdd4bafdf2562e4e3848b6fed6
3
+ metadata.gz: 7c9b64329c26980a4442660b5d63122cf0e0f716
4
+ data.tar.gz: d9c410f2c1024f1c2debc649d1ece684e1856427
5
5
  SHA512:
6
- metadata.gz: 3268a918b026b4f3ff830ffede2457a0a9cd05815121e26a4a7af4164424bc39866026d9b7dd925ddfb43bffb0f486295334aa25fa9a0deaac6670d4726cacc7
7
- data.tar.gz: bf60f687de9713a89c52bba76200254aad1da2558ae405581f58a3ded581eead607441e49154c4b619af32189cedfc3fcf4e321f273fc6ef5d13281435be237c
6
+ metadata.gz: 4a604ca2daab6d18ce54609f2b0c85aa877d7c03c11a2bc7a55b540a4fb08711b79d3d5845fe43b48161df8cc68b2650a34fca050ab907ffcc0480d873a55e59
7
+ data.tar.gz: d85225365844f09670f052985419df9d43615bf77c728d170c3e22c45c723fe61340e374fbc3b139182d4682b9cb95e23c55fedb47226e49bc84a9038a7444f0
data/README.md CHANGED
@@ -38,14 +38,23 @@ gem 'twsms2', '~> 1.1.0'
38
38
 
39
39
  本 API 套件,提供幾組以下方法來方便您開發簡訊相關服務
40
40
 
41
- 但要使用前,需要先[註冊台灣簡訊的會員][twsms_signup],否則您的程式無法存取台灣簡訊的 API 管道
41
+ 基本執行範例如下,但要使用前,需要先[註冊台灣簡訊的會員][twsms_signup],否則無法存取相關服務
42
42
 
43
43
  ```ruby
44
44
  require 'twsms2'
45
45
 
46
- # Twsms2 是走 https 的方式進行系統操作
46
+ # 程式將會以 SSL 的方式,走 https 連線到簡訊商的系統
47
+ sms_client = Twsms2::Client.new(username: '會員帳號', password: '會員密碼')
48
+ ```
49
+
50
+ 也可以加入 agent ( user-agent ) 跟 timeout ( 逾時時間/秒 ) 參數 至 client 物件
51
+
52
+ ```ruby
47
53
  sms_client = Twsms2::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
 
@@ -60,8 +69,6 @@ sms_client = Twsms2::Client.new(
60
69
  sms_client.account_is_available
61
70
  ```
62
71
 
63
- ----
64
-
65
72
  ### 發送簡訊
66
73
 
67
74
  #### 一般使用
@@ -169,6 +176,28 @@ message_quota 則是簡訊餘額,代表你還剩幾封可以用,若為 0 就
169
176
  {:access_success=>false, :message_quota=>0, :error=>"TWSMS:00010"}
170
177
  ```
171
178
 
179
+ ----
180
+
181
+ 例外狀況的處理
182
+ --------
183
+
184
+ 在某些情況下,程式會擲出一些例外給 ruby 去處理,你可以先用 Twsms2 自帶的 Error 來先做 rescue
185
+
186
+ ```ruby
187
+
188
+ begin
189
+ sms_client.account_is_available
190
+ rescue Twsms2::ClientError
191
+ 'Client 物件有內部錯誤'
192
+ rescue Twsms2::ServerError => error
193
+ "伺服器端有一些無法處理的狀況 #{error.message}"
194
+ rescue Twsms2::ClientTimeoutError
195
+ "發生 Timeout 囉"
196
+ rescue Twsms2::Error => error
197
+ "發生非預期的錯誤 #{error.message}"
198
+ end
199
+
200
+ ```
172
201
 
173
202
  LICENSE
174
203
  --------
@@ -2,4 +2,5 @@ module Twsms2
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
@@ -17,6 +17,49 @@ module Twsms2
17
17
  asia_taipei_time
18
18
  end
19
19
 
20
+ def message_status_sanitize(original_text)
21
+ new_text = case original_text
22
+ when 'DELIVRD' then 'delivered'
23
+ when 'EXPIRED' then 'expired'
24
+ when 'DELETED' then 'deleted'
25
+ when 'UNDELIV' then 'undelivered'
26
+ when 'ACCEPTD' then 'transmitting'
27
+ when 'UNKNOWN' then 'unknown'
28
+ when 'REJECTD' then 'rejected'
29
+ when 'SYNTAXE' then 'incorrect_sms_system_syntax'
30
+ when 'MOBERROR' then 'incorrect_phone_number'
31
+ when 'MSGERROR' then 'incorrect_content'
32
+ when 'OTHERROR' then 'sms_system_other_error'
33
+ when 'REJERROR' then 'illegal_content'
34
+ else 'status_undefined'
35
+ end
36
+
37
+ new_text
38
+ end
39
+
40
+ def format_message_status(original_info)
41
+ new_info = {
42
+ access_success: false,
43
+ is_delivered: false,
44
+ message_status: nil,
45
+ error: nil
46
+ }
47
+
48
+ code_text = match_string(/<code>(?<code>\w+)<\/code>/, original_info)
49
+ status_text = match_string(/<statustext>(?<status>\w+)<\/statustext>/, original_info)
50
+
51
+ new_info[:access_success] = !code_text.nil? && !status_text.nil? && code_text == '00000'
52
+
53
+ if new_info[:access_success]
54
+ new_info[:message_status] = message_status_sanitize(status_text)
55
+ new_info[:is_delivered] = new_info[:message_status] == 'delivered'
56
+ else
57
+ new_info[:error] = code_text.nil? ? "TWSMS:CODE_NOT_FOUND" : "TWSMS:#{code_text}".upcase
58
+ end
59
+
60
+ new_info
61
+ end
62
+
20
63
  def format_send_message_info(original_info)
21
64
  new_info = {
22
65
  access_success: false,
@@ -21,6 +21,8 @@ module Twsms2
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 Twsms2
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 Twsms2
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/twsms2.rb CHANGED
@@ -13,6 +13,7 @@ module Twsms2
13
13
  @api_host = options.fetch(:host) { 'api.twsms.com' }
14
14
  @username = options.fetch(:username) { ENV.fetch('TWSMS_USERNAME') }
15
15
  @password = options.fetch(:password) { ENV.fetch('TWSMS_PASSWORD') }
16
+ @timeout = options.fetch(:timeout) { 10 }
16
17
  end
17
18
 
18
19
  def account_is_available
@@ -37,5 +38,15 @@ module Twsms2
37
38
 
38
39
  format_balance_info(response)
39
40
  end
41
+
42
+ def get_message_status(options={})
43
+ options[:message_id] ||= nil
44
+ options[:phone_number] ||= nil
45
+
46
+ response = get(@api_host, '/smsQuery.php', mobile: options[:phone_number], msgid: options[:message_id])
47
+
48
+ format_message_status(response)
49
+ end
50
+
40
51
  end
41
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twsms2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.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-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler