kotsms2 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: caba16f48eabc4f0d3067cd20b8f8ba4c67d00f1
4
+ data.tar.gz: adb7317fe123b5a1ed90f5062e7940002bcaf703
5
+ SHA512:
6
+ metadata.gz: f743ae865b95c28a08e9ad53f753701d5ce9b92c8e243d3a02facffcbdaba43fcd2e7ad2f690aef41699d7d417e11ddcbdeca7bb160cde71f8039f692ba13573
7
+ data.tar.gz: d3737900c4ba72f004ac765e4bf0f85b083d6f3507c1239e54f3acbcd4585276e844e837eb92241b5961dbabda61678b7daa15580337c874bee5b74760bc7f46
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kotsms2.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Guanting Chen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,174 @@
1
+ Kotsms2 ( 2016 新版 簡訊王 KotSMS API Ruby 版套件 )
2
+ =================================================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/kotsms2.svg)](https://badge.fury.io/rb/kotsms2) [![Build Status](https://travis-ci.org/guanting112/kotsms2.svg?branch=master)](https://travis-ci.org/guanting112/kotsms2)
5
+
6
+ ![kotsms](http://i.imgur.com/Q4QbtOP.png)
7
+
8
+ 此為針對 [簡訊王][kotsms_homepage] KotSMS API 開發的專屬套件
9
+
10
+ 您可以透過該套件來實作簡訊王的一般、預約簡訊發送 與 簡訊餘額查詢 的程式
11
+
12
+ 適用於
13
+ --------
14
+
15
+ Ruby 2 以上的版本,也可在 Ruby On Rails 專案引入
16
+
17
+ 安裝方式
18
+ --------
19
+
20
+ 請在您的 Ruby 或 Rails 專案裡的 Gemfile 加入以下指令
21
+
22
+ ```ruby
23
+ gem 'kotsms2', '~> 1.0.0'
24
+ ```
25
+
26
+ 然後執行 bundle install 更新套件組
27
+
28
+ $ bundle
29
+
30
+ 或也可以直接將 kotsms2 裝在系統上
31
+
32
+ $ gem install kotsms2
33
+
34
+ 使用方式
35
+ --------
36
+
37
+ 本 API 套件,提供幾組以下方法來方便您開發簡訊相關服務
38
+
39
+ 但要使用前,需要先[註冊簡訊王的會員][kotsms_signup],否則您的程式無法存取簡訊王的 API 管道
40
+
41
+ ```ruby
42
+ require 'kotsms2'
43
+
44
+ # Kotsms2 是走 https 的方式進行系統操作
45
+ sms_client = Kotsms2::Client.new(username: '會員帳號', password: '會員密碼', agent: "Mozilla/5.0 (可自訂 user-agent)")
46
+ ```
47
+
48
+ 使用範例
49
+ --------
50
+
51
+ ### 系統檢查
52
+
53
+ 檢查帳號是否有效、是否可以發送簡訊,有效為 true、無效則為 false
54
+
55
+ ```ruby
56
+ sms_client.account_is_available
57
+ ```
58
+
59
+ ----
60
+
61
+ ### 發送簡訊
62
+
63
+ #### 一般使用
64
+
65
+ 手機號碼格式 0911222333 ( 台灣手機 )、886911222333 ( 國碼 + 手機號碼 )
66
+
67
+ ```ruby
68
+ # 備註:簡訊王的內容需要為 Big 5 編碼,本套件將會自動進行轉換,若有遇到無法轉換的,會將會替換成「?」號
69
+ sms_client.send_message to: '手機號碼', content: "簡訊內容.."
70
+ ```
71
+
72
+ #### 預約發送
73
+
74
+ 若要使用預約發送,可以指定 at 參數給 send_message 方法
75
+
76
+ 同時程式會自動轉換時區 至 簡訊王 適用的時區 ( +08:00 )
77
+
78
+ ```ruby
79
+ # 純 Ruby 請用加秒數的方式
80
+ sms_client.send_message to: '手機號碼', content: "預約簡訊測試: #{Time.now}", at: Time.now + 120
81
+
82
+ # 在 Ruby On Rails 專案則可以用 Rails 專用的方法
83
+ sms_client.send_message to: '手機號碼', content: "預約簡訊測試: #{Time.now}", at: Time.now + 2.days
84
+ ```
85
+
86
+ #### 切換 大量發送 專用線路
87
+
88
+ 根據簡訊王文件說明,若要進行上千、萬數以上的大量發送,建議使用專用線路
89
+
90
+ 因此,本程式有提供一個 heavy_loading 參數,讓您切換至大量發送專用線路
91
+
92
+ ```ruby
93
+ sms_client.send_message to: '手機號碼', content: "簡訊內容..", heavy_loading: true
94
+ ```
95
+
96
+ 當您指定了該參數後,將會使用另外的線路進行發送
97
+
98
+ 但請根據實際發送需求進行使用,小量發送可以不用加入 heavy_loading 參數
99
+
100
+ 因為簡訊王的大量發送專用線路,屬於另一種處理方式,小量發送並不會比較即時
101
+
102
+ ### 發送簡訊 的 回傳結果
103
+
104
+ #### 發送成功
105
+
106
+ 當你執行完成後,send_message 方法會回傳一組 hash 型態的結果
107
+
108
+ 只要 access_success 的值為 true 就一定代表發送成功
109
+
110
+ 系統會另外回傳一組 message_id 用來讓你追蹤簡訊
111
+
112
+ ```ruby
113
+ {:access_success=>true, :message_id=>"123456789", :error=>nil}
114
+ ```
115
+
116
+ #### 發生錯誤時
117
+
118
+ 若 access_success 為 false 則表示過程有出現錯誤
119
+
120
+ 以下範例為帳號密碼的錯誤,error 參數則是簡訊王的 error code
121
+
122
+ error code 的部分,請以 簡訊王 API 文件的定義為主,本套件不處理相關結果
123
+
124
+ ```ruby
125
+ {:access_success=>false, :message_id=>nil, :error=>"KOTSMS:-2"}
126
+ ```
127
+
128
+ ----
129
+
130
+ ### 查詢簡訊餘額
131
+
132
+ 若你需要查詢您會員帳號的簡訊餘額,可以用以下指令處理
133
+
134
+ ```ruby
135
+ sms_client.get_balance
136
+ ```
137
+
138
+ ### 查詢簡訊餘額 的 回傳結果
139
+
140
+ #### 得到簡訊餘額
141
+
142
+ 當你執行完成後,get_balance 方法會回傳一組 hash 型態的結果
143
+
144
+ 只要 access_success 的值為 true 就一定代表系統有成功取得資料
145
+
146
+ message_quota 則是簡訊餘額,代表你還剩幾封可以用,若為 0 就代表都沒有
147
+
148
+ ```ruby
149
+ # 備註:簡訊商若發送過程中出現意外狀況,會晚點將簡訊額度補回您的會員帳號
150
+ {:access_success=>true, :message_quota=>10, :error=>nil}
151
+ ```
152
+
153
+ #### 發生錯誤
154
+
155
+ 若 access_success 為 false 則表示過程有出現錯誤,同時 message_quota 會為 0
156
+
157
+ ```ruby
158
+ {:access_success=>false, :message_quota=>0, :error=>"KOTSMS:-2"}
159
+ ```
160
+
161
+
162
+ LICENSE
163
+ --------
164
+
165
+ 本專案原始碼採 MIT LICENSE 授權 ( 詳見 LICENSE 檔案 )
166
+
167
+ 連結分享
168
+ --------
169
+
170
+ [註冊簡訊王的會員][kotsms_signup]、[簡訊王官網][kotsms_homepage]、[API 技術支援][kotsms_tech]
171
+
172
+ [kotsms_signup]: https://kotsms.com.tw/index.php?selectpage=MembersReg&step=viewrules
173
+ [kotsms_homepage]: https://www.kotsms.com.tw
174
+ [kotsms_tech]: https://kotsms.com.tw/index.php?selectpage=pagenews&kind=4
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :spec
5
+
6
+ Rake::TestTask.new(:spec) do |t|
7
+ t.test_files = FileList['spec/*_spec.rb']
8
+ t.warning = true
9
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kotsms2"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/kotsms2.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kotsms2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kotsms2"
8
+ spec.version = Kotsms2::VERSION
9
+ spec.authors = ["Guanting Chen"]
10
+ spec.email = ["cgt886@gmail.com"]
11
+ spec.license = "MIT"
12
+ spec.platform = Gem::Platform::RUBY
13
+ spec.summary = %q{2016 新版 簡訊王 KotSMS API ( 純 Ruby / Rails 專案適用 )}
14
+ spec.description = %q{2016 新版 簡訊王 KotSMS API ( 純 Ruby / Rails 專案適用 )}
15
+ spec.homepage = "https://github.com/guanting112/kotsms2"
16
+ spec.required_ruby_version = '~> 2'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.7"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ # spec.add_development_dependency 'webmock', '~> 1.18'
34
+ spec.add_development_dependency 'minitest', '~> 5.0'
35
+ end
@@ -0,0 +1,5 @@
1
+ module Kotsms2
2
+ class Error < StandardError; end
3
+ class ClientError < Error; end
4
+ class ServerError < Error; end
5
+ end
@@ -0,0 +1,75 @@
1
+ module Kotsms2
2
+ module Formatter
3
+ def to_big5(old_string)
4
+ new_string = old_string.encode("BIG5", :invalid => :replace, :undef => :replace, :replace => "?")
5
+ end
6
+
7
+ def to_utf8(old_string)
8
+ new_string = old_string.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
9
+ end
10
+
11
+ def match_string(rule, string)
12
+ match_data = rule.match(string)
13
+ match_data.nil? ? nil : match_data[1]
14
+ end
15
+
16
+ def format_time_string(time)
17
+ return nil if time.nil?
18
+ new_time = to_asia_taipei_timezone(time)
19
+ new_time.strftime('%Y/%m/%d %H:%M:%S')
20
+ end
21
+
22
+ def to_asia_taipei_timezone(time)
23
+ utc_time = time.utc? ? time.dup : time.dup.utc
24
+ asia_taipei_time = utc_time.getlocal('+08:00')
25
+ asia_taipei_time
26
+ end
27
+
28
+ def format_send_message_info(original_info)
29
+ new_info = {
30
+ access_success: false,
31
+ message_id: nil,
32
+ error: nil
33
+ }
34
+
35
+ code_text = match_string(/^kmsgid=(?<code>-?\d+)$/, original_info)
36
+ code_number = code_text.to_i
37
+
38
+ new_info[:access_success] = !code_text.nil? && code_number > -1
39
+
40
+ if new_info[:access_success]
41
+ new_info[:message_id] = code_number.to_s
42
+ else
43
+ new_info[:error] = "KOTSMS:CODE_NOT_FOUND"
44
+ new_info[:error] = "KOTSMS:#{code_text}" unless code_text.nil?
45
+ new_info[:error].upcase!
46
+ end
47
+
48
+ new_info
49
+ end
50
+
51
+ def format_balance_info(original_info)
52
+ new_info = {
53
+ access_success: false,
54
+ message_quota: 0,
55
+ error: nil
56
+ }
57
+
58
+ code_text = match_string(/^(?<code>-?\d+)$/, original_info)
59
+ code_number = code_text.to_i
60
+
61
+ new_info[:access_success] = !code_text.nil? && code_number > -1
62
+
63
+ if new_info[:access_success]
64
+ new_info[:message_quota] = code_number
65
+ else
66
+ new_info[:error] = "KOTSMS:CODE_NOT_FOUND"
67
+ new_info[:error] = "KOTSMS:#{code_text}" unless code_text.nil?
68
+ new_info[:error].upcase!
69
+ end
70
+
71
+ new_info
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,43 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+
4
+ module Kotsms2
5
+ module Network
6
+ def get(host, request_uri, params = {})
7
+ uri = URI('https://' + host + request_uri)
8
+ uri.query = query_string(params.merge(username: @username, password: @password))
9
+
10
+ message = Net::HTTP::Get.new(uri.request_uri)
11
+ message['User-Agent'] = @user_agent
12
+
13
+ parse(request(uri, message), host)
14
+ end
15
+
16
+ def parse(http_response, host)
17
+ case http_response
18
+ when Net::HTTPSuccess
19
+ http_response.body
20
+ when Net::HTTPClientError
21
+ raise ClientError, "#{http_response.code} response from #{host}"
22
+ when Net::HTTPServerError
23
+ raise ServerError, "#{http_response.code} response from #{host}"
24
+ else
25
+ raise Error, "#{http_response.code} response from #{host}"
26
+ end
27
+ end
28
+
29
+ def request(uri, message)
30
+ http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port)
31
+ http.use_ssl = true
32
+ http.request(message)
33
+ end
34
+
35
+ def query_string(params)
36
+ params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
37
+ end
38
+
39
+ def escape(component)
40
+ CGI.escape(component.to_s)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Kotsms2
2
+ VERSION = "0.5.0"
3
+ end
data/lib/kotsms2.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'kotsms2/version'
2
+ require 'kotsms2/network'
3
+ require 'kotsms2/exception'
4
+ require 'kotsms2/formatter'
5
+
6
+ module Kotsms2
7
+ class Client
8
+ include Kotsms2::Network
9
+ include Kotsms2::Formatter
10
+
11
+ def initialize(options={})
12
+ @user_agent = options.fetch(:agent) { "kotsms2/#{VERSION}" }
13
+ @api_host = options.fetch(:host) { 'api.kotsms.com.tw' }
14
+ @username = options.fetch(:username) { ENV.fetch('KOTSMS_USERNAME') }
15
+ @password = options.fetch(:password) { ENV.fetch('KOTSMS_PASSWORD') }
16
+ end
17
+
18
+ def account_is_available
19
+ balance_info = get_balance
20
+
21
+ ok = balance_info[:message_quota] > 0 && balance_info[:access_success]
22
+ end
23
+
24
+ def send_message(options={})
25
+ options[:to] ||= nil
26
+ options[:heavy_loading] ||= false
27
+
28
+ options[:content] = options[:content].to_s
29
+ options[:at] = format_time_string(options[:at])
30
+ options[:at] = 0 if options[:at].nil? # 據 Kotsms 文件指出,如果要更即時,可以指定為 0,比不給參數還快
31
+
32
+ api_path = '/kotsmsapi-1.php'
33
+ api_path = '/kotsmsapi-2.php' if options[:heavy_loading]
34
+
35
+ response = get(@api_host, api_path, dstaddr: options[:to], smbody: to_big5(options[:content]), dlvtime: options[:at])
36
+
37
+ format_send_message_info(response)
38
+ end
39
+
40
+ def get_balance
41
+ response = get(@api_host, '/memberpoint.php')
42
+
43
+ format_balance_info(response)
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kotsms2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Guanting Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: 2016 新版 簡訊王 KotSMS API ( 純 Ruby / Rails 專案適用 )
56
+ email:
57
+ - cgt886@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - kotsms2.gemspec
71
+ - lib/kotsms2.rb
72
+ - lib/kotsms2/exception.rb
73
+ - lib/kotsms2/formatter.rb
74
+ - lib/kotsms2/network.rb
75
+ - lib/kotsms2/version.rb
76
+ homepage: https://github.com/guanting112/kotsms2
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.4
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: 2016 新版 簡訊王 KotSMS API ( 純 Ruby / Rails 專案適用 )
101
+ test_files: []