chuanglan 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8cd2bcd5507a7d5f90450e3d55725a978f01957
4
- data.tar.gz: 5dbf0ef2f5e9341001d60fc9e2a3e1ddd0ea8a66
3
+ metadata.gz: 6b8b4f4a43937f6cc143da99126052b225cfa1bb
4
+ data.tar.gz: ce6c6b8cc58f4e2b87f01e6cc5f2da24dc3ada0c
5
5
  SHA512:
6
- metadata.gz: 856eecf4e3bf5ded9fba1445f4ab031a268bf752e3b16519d8c4fc1c5e1e4a180b0778e62a5434717dda3f76e7b84dd6112807a9829058432f1a236963b9e3f1
7
- data.tar.gz: b76da84f6c680dbb470c509e21989b681845ac65564fcd61e8891c86349abca447dc0b942d1f7e2b09510feb91890c517284872240b0f20ca4d921ca8d610d58
6
+ metadata.gz: c8437d052a19f82b2903543af9925dd5eba6011f3b9526bd618d0cf1dad2856710d22494e4ef115c3c33fe1d2fa3b29c17a8b5b2df046cf2928c662a5c350c4c
7
+ data.tar.gz: b04da6407495a827620c45961654b6e393540080240792f91d449db6d80d0f067726a24c4ee02895b7762aa0b458088a6f39394fb7e3b6151c321a65b6f7dbd0
data/README.md CHANGED
@@ -23,15 +23,31 @@ Or install it yourself as:
23
23
  ```ruby
24
24
  Chuanglan.username = 'username'
25
25
  Chuanglan.password = 'password'
26
+
27
+ Chuanglan::International.username = 'international_username'
28
+ Chuanglan::International.password = 'international_password'
26
29
  ```
27
30
 
28
- ### Send sms
31
+ ### Send SMS
29
32
 
30
33
  ```ruby
31
34
  Chuanglan.send_to!('10086', '流量唔够用啊')
32
- # Chuanglan.send_to!(['10086', '10010'], '信号好差啊')
35
+ Chuanglan.send_to!(['10086', '10010'], '信号好差啊')
36
+ Chuanglan.send_to!('10086', '流量唔够用啊', un: 'un', pw: 'pw', rd: 0, ex: 'ex')
37
+ ```
38
+
39
+ ### Check balance
40
+
41
+ ```ruby
42
+ Chuanglan.balance # => 100
33
43
  ```
34
44
 
45
+ ### Send International SMS
46
+
47
+ ```ruby
48
+ Chuanglan::International.send_to!('8613612345678', '流量唔够用啊')
49
+ Chuanglan::International.send_to!('8613612345678', '流量唔够用啊', un: 'un', pw: 'pw', dc: 15, rf: 1, tf: 3)
50
+ ```
35
51
 
36
52
  ## Development
37
53
 
@@ -41,7 +57,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
41
57
 
42
58
  ## Contributing
43
59
 
44
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chuanglan. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GaiaMagic/chuanglan. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
45
61
 
46
62
 
47
63
  ## License
data/lib/chuanglan.rb CHANGED
@@ -1,16 +1,42 @@
1
1
  require 'chuanglan/version'
2
+ require 'chuanglan/international'
2
3
  require 'chuanglan/request'
3
4
  require 'chuanglan/request_exception'
4
5
 
5
6
  module Chuanglan
6
- GATEWAY = 'http://222.73.117.158:80/msg/HttpBatchSendSM'
7
+ GATEWAY = 'https://sms.253.com'
7
8
  @timeout = 5
8
9
 
9
10
  class << self
10
11
  attr_accessor :username, :password, :timeout
11
12
 
12
- def send_to!(recipients, message)
13
- Request.new(recipients, message).perform!
13
+ def send_to!(recipients, message, params = {})
14
+ params = base_params.merge(params)
15
+ params[:phone] = Array(recipients).join(',')
16
+ params[:msg] = message
17
+ rsp = Request.new("#{GATEWAY}/msg/send", params).perform
18
+ success_or_raise_exception(rsp)
19
+ end
20
+
21
+ def balance
22
+ rsp = Request.new("#{GATEWAY}/msg/balance", un: username, pw: password).perform
23
+ success_or_raise_exception(rsp).to_i
24
+ end
25
+
26
+ private
27
+
28
+ def base_params
29
+ {
30
+ un: username,
31
+ pw: password,
32
+ rd: 1,
33
+ }
34
+ end
35
+
36
+ def success_or_raise_exception(response)
37
+ headers, data = response.body.split("\n")
38
+ timestamp, code = headers.split(',')
39
+ code == '0' ? data : raise(RequestException.new(code))
14
40
  end
15
41
  end
16
42
  end
@@ -0,0 +1,34 @@
1
+ module Chuanglan
2
+ class International
3
+ GATEWAY = 'https://intapi.253.com'
4
+
5
+ class << self
6
+ attr_accessor :username, :password, :timeout
7
+
8
+ def send_to!(recipients, message, params = {})
9
+ params = base_params.merge(params)
10
+ params[:da] = Array(recipients).join(',')
11
+ params[:sm] = message
12
+ rsp = Request.new("#{GATEWAY}/mt", params).perform
13
+ success_or_raise_exception(rsp)
14
+ end
15
+
16
+ private
17
+
18
+ def base_params
19
+ {
20
+ un: username,
21
+ pw: password,
22
+ dc: 15,
23
+ rf: 1,
24
+ tf: 3,
25
+ }
26
+ end
27
+
28
+ def success_or_raise_exception(response)
29
+ data = URI.decode_www_form(response.body).to_h
30
+ data['id'] || raise(RequestException.new(data['r']))
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,42 +2,19 @@ require 'net/http'
2
2
 
3
3
  module Chuanglan
4
4
  class Request
5
- def initialize(recipients, message)
6
- @recipients = Array(recipients)
7
- @message = message
5
+
6
+ def initialize(url, params)
7
+ @uri = URI(url)
8
+ @params = params
8
9
  end
9
10
 
10
- def perform!
11
- uri = URI(GATEWAY)
11
+ def perform
12
12
  timeout = Chuanglan.timeout
13
- response = Net::HTTP.start(uri.host,
14
- uri.port,
15
- open_timeout: timeout,
16
- read_timeout: timeout) do |http|
17
- post_data = URI.encode_www_form(payload)
18
- http.request_post(uri.path, post_data)
13
+ is_https = @uri.scheme == 'https'
14
+ Net::HTTP.start(@uri.host, @uri.port, open_timeout: timeout, read_timeout: timeout, use_ssl: is_https) do |http|
15
+ post_data = URI.encode_www_form(@params)
16
+ http.request_post(@uri.path, post_data)
19
17
  end
20
-
21
- success_or_raise_exception(response)
22
- end
23
-
24
- private
25
-
26
- def payload
27
- {
28
- account: Chuanglan.username,
29
- pswd: Chuanglan.password,
30
- mobile: @recipients.join(','),
31
- msg: @message,
32
- needstatus: 'true',
33
- }
34
- end
35
-
36
- def success_or_raise_exception(response)
37
- headers, msgid= response.body.split("\n")
38
- timestamp, code = headers.split(',')
39
-
40
- code == '0' || raise(RequestException.new(code))
41
18
  end
42
19
  end
43
20
  end
@@ -1,6 +1,7 @@
1
1
  module Chuanglan
2
2
  class RequestException < StandardError
3
3
  ERROR_MESSAGES = {
4
+ '100' => 'Bad API Address',
4
5
  '101' => 'No such user', # Not officially documented
5
6
  '102' => 'Wrong password',
6
7
  '103' => 'Push too hurry',
@@ -1,3 +1,3 @@
1
1
  module Chuanglan
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chuanglan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HungYuHei
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-08 00:00:00.000000000 Z
11
+ date: 2017-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,7 @@ files:
84
84
  - bin/setup
85
85
  - chuanglan.gemspec
86
86
  - lib/chuanglan.rb
87
+ - lib/chuanglan/international.rb
87
88
  - lib/chuanglan/request.rb
88
89
  - lib/chuanglan/request_exception.rb
89
90
  - lib/chuanglan/version.rb
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project:
111
- rubygems_version: 2.4.5
112
+ rubygems_version: 2.6.6
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Ruby SDK for 创蓝短信网关