alidayu-sms 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/alidayu-sms.gemspec +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/alidayu/sms/version.rb +5 -0
- data/lib/alidayu/sms.rb +116 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 036c0a3bcf59057018aab95aeee0eb309b36c690
|
4
|
+
data.tar.gz: 5958f8e2aece43c3c89bdf7743e358b1c43c62b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 115438d024fea75816aaa2273dc905b40260d5cedad826cddfa111c0d3c0c55e75c77d319ff5c936c22c01635be590c22176b5f8802a31ee85336ed1cff35367
|
7
|
+
data.tar.gz: '03914b905d94c5ae519bcf546d5dafc9949e61817bd82bfc0537a2fc33d36f92320684ae5db98ac6becee855b4e6223e0bae73bee4c406acb9c2a5501fed4cac'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Charles
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
## Alidayu-sms ##
|
2
|
+
|
3
|
+
Alidayu sms sdk ruby version of Aliyun(阿里云阿里大于短信SDK).
|
4
|
+
|
5
|
+
|
6
|
+
### Installation ###
|
7
|
+
# Manually from RubyGems.org
|
8
|
+
$ gem install alidayu-sms
|
9
|
+
|
10
|
+
# Or Gemfile if you are using Bundler
|
11
|
+
$ gem alidayu-sms
|
12
|
+
|
13
|
+
### Usage ###
|
14
|
+
```ruby
|
15
|
+
alisms = Alidayu::Sms.new(sms_template: YOUR_SMS_TEMPLATE, sms_sign: YOUR_SMS_SIGN)
|
16
|
+
alisms.send_to(138xxxxxxxx, {code: 1234})
|
17
|
+
# true or false
|
18
|
+
|
19
|
+
# or use send! if you wanna raise error
|
20
|
+
alisms.send_to!(138xxxxxxxx, {code: 1234})
|
21
|
+
# true or raise Alidayu::RequestError
|
22
|
+
```
|
23
|
+
You can also use `send_sms` to send Alidayu sms, it's an alias of `send_to`.
|
24
|
+
|
25
|
+
### Configration ###
|
26
|
+
Before you sending any sms message, a valid configration is necessary.
|
27
|
+
Alidayu-sms supports you use it under Rails or in pure Ruby environment.
|
28
|
+
|
29
|
+
#### Rails ####
|
30
|
+
The Alidayu-sms configuration file is a YAML file, by default located at `config/alidayu.yml`.
|
31
|
+
You need to set your Alidayu access_key which you can get from Aliyun.
|
32
|
+
Here is an example configuration file:
|
33
|
+
|
34
|
+
```yaml
|
35
|
+
# config/alidayu.yml
|
36
|
+
development: &defaults
|
37
|
+
access_key_id: YOUR_ACCESS_KEY_ID
|
38
|
+
access_key_secret: YOUR_ACCESS_KEY_SECRET
|
39
|
+
|
40
|
+
test:
|
41
|
+
<<: *defaults
|
42
|
+
|
43
|
+
production:
|
44
|
+
<<: *defaults
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Non-rails ####
|
48
|
+
If you are using Sinatra or anything pure Ruby, use `Alidayu::Sms.configration` to configrate Alidayu-sms.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Alidayu::Sms.configration do |config|
|
52
|
+
config.access_key_id = YOUR_ACCESS_KEY_ID
|
53
|
+
config.access_key_secret = YOUR_ACCESS_KEY_SECRET
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### License ###
|
58
|
+
Released under the [MIT](http://opensource.org/licenses/MIT) license. See LICENSE file for details.
|
data/Rakefile
ADDED
data/alidayu-sms.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "alidayu/sms/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "alidayu-sms"
|
8
|
+
spec.version = Alidayu::Sms::VERSION
|
9
|
+
spec.authors = ["cenxky"]
|
10
|
+
spec.email = ["cenxky@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Alidayu sms sdk ruby version of Aliyun."
|
13
|
+
spec.description = "Alidayu sms sdk ruby version of Aliyun."
|
14
|
+
spec.homepage = "https://github.com/cenxky/alidayu-sms"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "alidayu/sms"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/alidayu/sms.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require_relative 'sms/version'
|
2
|
+
%w(securerandom net/http ostruct openssl base64 yaml json cgi uri).each{|_| require _}
|
3
|
+
|
4
|
+
module Alidayu
|
5
|
+
class SettingError < StandardError; end
|
6
|
+
class RequestError < StandardError; end
|
7
|
+
|
8
|
+
class Sms
|
9
|
+
SMS_ENDPOINT = 'http://dysmsapi.aliyuncs.com'
|
10
|
+
attr :signature, :template
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def configuration
|
14
|
+
@setting = OpenStruct.new
|
15
|
+
yield @setting
|
16
|
+
end
|
17
|
+
|
18
|
+
def setting
|
19
|
+
return @setting if @setting && !@setting.to_h.nil?
|
20
|
+
if defined?(Rails)
|
21
|
+
config = YAML.load_file("#{Rails.root}/config/alidayu.yml")[Rails.env]
|
22
|
+
if !config.nil? then @setting = OpenStruct.new config else
|
23
|
+
raise SettingError, setting_error_message(:rails)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise SettingError, setting_error_message
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def setting_error_message(reason = :configuration)
|
33
|
+
base_message = 'You have not set access_key_id and access_key_secret '
|
34
|
+
case reason
|
35
|
+
when :configuration
|
36
|
+
base_message << 'by Alidayu::Sms.configuration'
|
37
|
+
when :rails
|
38
|
+
base_message << "in config/alidayu.yml for #{env} environment"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def initialize(options)
|
44
|
+
@template = options[:sms_template]
|
45
|
+
@signature = options[:sms_sign]
|
46
|
+
|
47
|
+
endpoint = URI setting.endpoint || SMS_ENDPOINT
|
48
|
+
@conn = Net::HTTP.new(endpoint.host, endpoint.port).tap do |http|
|
49
|
+
http.open_timeout = 3
|
50
|
+
http.read_timeout = 3
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_to!(reciever, params = {})
|
55
|
+
reciever = Array(reciever).join(',')
|
56
|
+
sms_params = default_params(reciever, params)
|
57
|
+
sig_params = params_with_sign(setting.access_key_secret, sms_params)
|
58
|
+
response = @conn.request_post '/', params_string(sig_params)
|
59
|
+
result = JSON.parse response.body
|
60
|
+
if result['Code'] != 'OK'
|
61
|
+
raise RequestError, "#{result['Code']} #{result['Message']}"
|
62
|
+
end
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
def send_to(*args)
|
67
|
+
send!(*args) rescue false
|
68
|
+
end
|
69
|
+
|
70
|
+
alias send_sms send_to
|
71
|
+
alias send_sms! send_to!
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def default_params(reciever, params)
|
76
|
+
{
|
77
|
+
'AccessKeyId' => setting.access_key_id,
|
78
|
+
'Action' => 'SendSms',
|
79
|
+
'Format' => 'JSON',
|
80
|
+
'RegionId' => 'cn-hangzhou',
|
81
|
+
'SignName' => signature,
|
82
|
+
'PhoneNumbers' => reciever,
|
83
|
+
'TemplateParam' => params.to_json,
|
84
|
+
'SignatureMethod' => 'HMAC-SHA1',
|
85
|
+
'SignatureNonce' => SecureRandom.uuid,
|
86
|
+
'SignatureVersion' => '1.0',
|
87
|
+
'TemplateCode' => template,
|
88
|
+
'Timestamp' => Time.now.utc.strftime('%FT%TZ'),
|
89
|
+
'Version' => '2017-05-25'
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def setting
|
94
|
+
self.class.setting
|
95
|
+
end
|
96
|
+
|
97
|
+
def params_with_sign(key_secret, params)
|
98
|
+
params.merge 'Signature' => sign(key_secret, params)
|
99
|
+
end
|
100
|
+
|
101
|
+
def xencode(str)
|
102
|
+
CGI.escape(str).gsub('+', '%20').gsub('*', '%2A').gsub('%7E', '~')
|
103
|
+
end
|
104
|
+
|
105
|
+
def sign(key_secret, params)
|
106
|
+
key = key_secret + '&'
|
107
|
+
str = 'POST&%2F&' + xencode(params_string params)
|
108
|
+
Base64.strict_encode64 OpenSSL::HMAC.digest('sha1', key, str)
|
109
|
+
end
|
110
|
+
|
111
|
+
def params_string(params)
|
112
|
+
params.sort.map{|arr| arr.map{|e| xencode e}.join('=')}.join('&')
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alidayu-sms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cenxky
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-31 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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
|
+
description: Alidayu sms sdk ruby version of Aliyun.
|
42
|
+
email:
|
43
|
+
- cenxky@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- alidayu-sms.gemspec
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/alidayu/sms.rb
|
59
|
+
- lib/alidayu/sms/version.rb
|
60
|
+
homepage: https://github.com/cenxky/alidayu-sms
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.6.11
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Alidayu sms sdk ruby version of Aliyun.
|
84
|
+
test_files: []
|