aliyun-rails 0.1.9 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +93 -84
- data/Rakefile +8 -8
- data/lib/aliyun/rails/connector/roa_client.rb +168 -168
- data/lib/aliyun/rails/connector/rpc_client.rb +117 -122
- data/lib/aliyun/rails/dysms.rb +27 -30
- data/lib/aliyun/rails/dyvms.rb +27 -30
- data/lib/aliyun/rails/version.rb +9 -9
- data/lib/aliyun/rails.rb +22 -0
- metadata +7 -7
- data/lib/aliyun-rails.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dd2919d59678f49ad2cf11ee04b09699998223435406bca266b028450ba498e
|
4
|
+
data.tar.gz: 339a221b039e588e6e0c7070b1264de67ca43260e1ee1f7f9423e54672b308f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9eecf24324919ed9725024aa9a83091f7544c3fca085773aee5d4f6576f7a2fa7e7a3294d11785784a9e6f318833da01f1a451e6d4db7d6070ab55d33cf22d45
|
7
|
+
data.tar.gz: 4423ce1c0668764732833a6a0a95f4230243000e91bb5576a09d2780f2a88224a511253bd8242dcd911eed6fadf5e583ba137d4b1cf97246d726f5dcca0ef8af
|
data/README.md
CHANGED
@@ -1,85 +1,94 @@
|
|
1
|
-
[English](./README.md) | 简体中文
|
2
|
-
|
3
|
-
|
4
|
-
<p align="center">
|
5
|
-
<a href=" https://www.alibabacloud.com"><img src="https://aliyunsdk-pages.alicdn.com/icons/Aliyun.svg"></a>
|
6
|
-
</p>
|
7
|
-
|
8
|
-
<h1 align="center">非官方SDK套件-用于RAILS
|
9
|
-
|
10
|
-
<p align="center">
|
11
|
-
<a href="https://badge.fury.io/rb/aliyunsdkcore"><img src="https://badge.fury.io/rb/aliyunsdkcore.svg" alt="Gem Version"></a>
|
12
|
-
<a href="https://travis-ci.org/aliyun/openapi-core-ruby-sdk"><img src="https://travis-ci.org/aliyun/openapi-core-ruby-sdk.svg?branch=master" alt="Build Status"></a>
|
13
|
-
<a href="https://ci.appveyor.com/project/aliyun/openapi-core-ruby-sdk/branch/master"><img src="https://ci.appveyor.com/api/projects/status/uyepkk5bjbynofvu/branch/master?svg=true" alt="Build status"></a>
|
14
|
-
<a href="https://codecov.io/gh/aliyun/openapi-core-ruby-sdk"><img src="https://codecov.io/gh/aliyun/openapi-core-ruby-sdk/branch/master/graph/badge.svg" alt="codecov"></a>
|
15
|
-
</p>
|
16
|
-
|
17
|
-
|
18
|
-
支持 Rails 轻松访问阿里云服务,例如:弹性云主机(ECS)、负载均衡(SLB)、云监控(CloudMonitor)等。
|
19
|
-
|
20
|
-
本文档介绍如何安装和使用 aliyun-rails
|
21
|
-
|
22
|
-
## 使用诊断
|
23
|
-
[Troubleshoot](https://troubleshoot.api.aliyun.com/?source=github_sdk) 提供 OpenAPI 使用诊断服务,通过 `RequestID` 或 `报错信息` ,帮助开发者快速定位,为开发者提供解决方案。
|
24
|
-
|
25
|
-
## 安装
|
26
|
-
|
27
|
-
```sh
|
28
|
-
$ gem install aliyun-rails
|
29
|
-
```
|
30
|
-
|
31
|
-
## 使用
|
32
|
-
|
33
|
-
RPC 示例;
|
34
|
-
|
35
|
-
```ruby
|
36
|
-
require "aliyun-rails"
|
37
|
-
|
38
|
-
client = Dysms.new(
|
39
|
-
access_key_id: ENV['ACCESS_KEY_ID'],
|
40
|
-
access_key_secret: ENV['ACCESS_KEY_SECRET'],
|
41
|
-
)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
1
|
+
[English](./README.md) | 简体中文
|
2
|
+
|
3
|
+
|
4
|
+
<p align="center">
|
5
|
+
<a href=" https://www.alibabacloud.com"><img src="https://aliyunsdk-pages.alicdn.com/icons/Aliyun.svg"></a>
|
6
|
+
</p>
|
7
|
+
|
8
|
+
<h1 align="center">非官方SDK套件-用于RAILS项目管理</h1>
|
9
|
+
|
10
|
+
<p align="center">
|
11
|
+
<a href="https://badge.fury.io/rb/aliyunsdkcore"><img src="https://badge.fury.io/rb/aliyunsdkcore.svg" alt="Gem Version"></a>
|
12
|
+
<a href="https://travis-ci.org/aliyun/openapi-core-ruby-sdk"><img src="https://travis-ci.org/aliyun/openapi-core-ruby-sdk.svg?branch=master" alt="Build Status"></a>
|
13
|
+
<a href="https://ci.appveyor.com/project/aliyun/openapi-core-ruby-sdk/branch/master"><img src="https://ci.appveyor.com/api/projects/status/uyepkk5bjbynofvu/branch/master?svg=true" alt="Build status"></a>
|
14
|
+
<a href="https://codecov.io/gh/aliyun/openapi-core-ruby-sdk"><img src="https://codecov.io/gh/aliyun/openapi-core-ruby-sdk/branch/master/graph/badge.svg" alt="codecov"></a>
|
15
|
+
</p>
|
16
|
+
|
17
|
+
|
18
|
+
支持 Rails 轻松访问阿里云服务,例如:弹性云主机(ECS)、负载均衡(SLB)、云监控(CloudMonitor)等。
|
19
|
+
|
20
|
+
本文档介绍如何安装和使用 aliyun-rails
|
21
|
+
|
22
|
+
## 使用诊断
|
23
|
+
[Troubleshoot](https://troubleshoot.api.aliyun.com/?source=github_sdk) 提供 OpenAPI 使用诊断服务,通过 `RequestID` 或 `报错信息` ,帮助开发者快速定位,为开发者提供解决方案。
|
24
|
+
|
25
|
+
## 安装
|
26
|
+
|
27
|
+
```sh
|
28
|
+
$ gem install aliyun-rails
|
29
|
+
```
|
30
|
+
|
31
|
+
## 使用
|
32
|
+
|
33
|
+
RPC 示例;
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require "aliyun-rails"
|
37
|
+
|
38
|
+
client = Dysms.new(
|
39
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
40
|
+
access_key_secret: ENV['ACCESS_KEY_SECRET'],
|
41
|
+
)
|
42
|
+
|
43
|
+
# 可以直接将API参数放到 initializers下
|
44
|
+
# Aliyun::Rails.config do |i|
|
45
|
+
# i.access_key_id = "XXX"
|
46
|
+
# i.access_key_secret = "YYYY"
|
47
|
+
# end
|
48
|
+
# 随后直接初始化
|
49
|
+
# client = Dysms.new
|
50
|
+
|
51
|
+
# then use the send_sms method
|
52
|
+
response = client.send_sms("1380000000", "SMS_10010", {param1: "11"}, "SIGN_NAME")
|
53
|
+
|
54
|
+
puts response
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
ROA 示例:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'aliyun-rails'
|
62
|
+
|
63
|
+
client = ROAClient.new(
|
64
|
+
endpoint: 'http://ros.aliyuncs.com',
|
65
|
+
api_version: '2015-09-01',
|
66
|
+
access_key_id: ENV['ACCESS_KEY_ID'],
|
67
|
+
access_key_secret: ENV['ACCESS_KEY_SECRET'],
|
68
|
+
)
|
69
|
+
|
70
|
+
response = client.request(
|
71
|
+
method: 'GET',
|
72
|
+
uri: '/regions',
|
73
|
+
options: {
|
74
|
+
timeout: 15000
|
75
|
+
}
|
76
|
+
)
|
77
|
+
|
78
|
+
print response.body
|
79
|
+
```
|
80
|
+
|
81
|
+
## 问题
|
82
|
+
[提交 Issue](https://github.com/ciscolive/aliyun-rails/issues/new/choose),不符合指南的问题可能会立即关闭。
|
83
|
+
|
84
|
+
|
85
|
+
## 发行说明
|
86
|
+
每个版本的详细更改记录在[发行说明](CHANGELOG.md)中。
|
87
|
+
|
88
|
+
|
89
|
+
## 贡献
|
90
|
+
提交 Pull Request 之前请阅读[贡献指南](CONTRIBUTING.md)。
|
91
|
+
|
92
|
+
|
93
|
+
## 许可证
|
85
94
|
[MIT](LICENSE.md)
|
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rubocop/rake_task"
|
5
|
-
|
6
|
-
RuboCop::RakeTask.new
|
7
|
-
|
8
|
-
task default: :rubocop
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rubocop/rake_task"
|
5
|
+
|
6
|
+
RuboCop::RakeTask.new
|
7
|
+
|
8
|
+
task default: :rubocop
|
@@ -1,168 +1,168 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "faraday"
|
4
|
-
require "securerandom"
|
5
|
-
require "active_support/all"
|
6
|
-
require "net/http"
|
7
|
-
|
8
|
-
module Net::HTTPHeader
|
9
|
-
def capitalize(name)
|
10
|
-
name
|
11
|
-
end
|
12
|
-
private :capitalize
|
13
|
-
end
|
14
|
-
|
15
|
-
module Aliyun
|
16
|
-
module Rails
|
17
|
-
module Connector
|
18
|
-
class ROAClient
|
19
|
-
attr_accessor :endpoint, :api_version, :access_key_id,
|
20
|
-
:access_key_secret, :security_token, :hostname, :opts
|
21
|
-
|
22
|
-
def initialize(config)
|
23
|
-
validate config
|
24
|
-
|
25
|
-
self.endpoint = config[:endpoint]
|
26
|
-
self.api_version = config[:api_version]
|
27
|
-
self.access_key_id = config[:access_key_id]
|
28
|
-
self.access_key_secret = config[:access_key_secret]
|
29
|
-
self.security_token = config[:security_token]
|
30
|
-
end
|
31
|
-
|
32
|
-
def request(method:, uri:, params: {}, body: {}, headers: {}, options: {})
|
33
|
-
# :"Content-Type" => "application/json" to "content-type" => "application/json"
|
34
|
-
headers.deep_transform_keys! { |key| key.to_s.downcase }
|
35
|
-
mix_headers = default_headers.merge(headers)
|
36
|
-
|
37
|
-
response = connection.send(method.downcase) do |request|
|
38
|
-
request.url uri, params
|
39
|
-
if body
|
40
|
-
if mix_headers["content-type"].start_with? "application/json"
|
41
|
-
request_body = body.to_json
|
42
|
-
elsif mix_headers["content-type"].start_with? "application/x-www-form-urlencoded"
|
43
|
-
request_body = URI.encode_www_form(body)
|
44
|
-
else
|
45
|
-
request_body = body
|
46
|
-
end
|
47
|
-
mix_headers["content-md5"] = Digest::MD5.base64digest request_body
|
48
|
-
mix_headers["content-length"] = request_body.length.to_s
|
49
|
-
request.body = request_body
|
50
|
-
end
|
51
|
-
string2sign = string_to_sign(method, uri, mix_headers, params)
|
52
|
-
mix_headers[:authorization] = authorization(string2sign)
|
53
|
-
mix_headers.each { |key, value| request.headers[key] = value }
|
54
|
-
end
|
55
|
-
|
56
|
-
return response if options.has_key? :raw_body
|
57
|
-
|
58
|
-
response_content_type = response.headers["Content-Type"] || ""
|
59
|
-
if response_content_type.start_with?("application/json")
|
60
|
-
if response.status >= 400
|
61
|
-
result = JSON.parse(response.body)
|
62
|
-
raise StandardError, "code: #{response.status}, #{result['Message']} requestid: #{result['RequestId']}"
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
if response_content_type.start_with?("text/xml")
|
67
|
-
result = Hash.from_xml(response.body)
|
68
|
-
raise ACSError, result["Error"] if result["Error"]
|
69
|
-
end
|
70
|
-
|
71
|
-
response
|
72
|
-
end
|
73
|
-
|
74
|
-
def connection(adapter = Faraday.default_adapter)
|
75
|
-
Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
|
76
|
-
end
|
77
|
-
|
78
|
-
def get(uri: "", headers: {}, params: {}, options: {})
|
79
|
-
request(method: :get, uri: uri, params: params, body: {}, headers: headers, options: options)
|
80
|
-
end
|
81
|
-
|
82
|
-
def post(uri: "", headers: {}, params: {}, body: {}, options: {})
|
83
|
-
request(method: :post, uri: uri, params: params, body: body, headers: headers, options: options)
|
84
|
-
end
|
85
|
-
|
86
|
-
def put(uri: "", headers: {}, params: {}, body: {}, options: {})
|
87
|
-
request(method: :put, uri: uri, params: params, body: body, headers: headers, options: options)
|
88
|
-
end
|
89
|
-
|
90
|
-
def delete(uri: "", headers: {}, params: {}, options: {})
|
91
|
-
request(method: :delete, uri: uri, params: params, body: {}, headers: headers, options: options)
|
92
|
-
end
|
93
|
-
|
94
|
-
def default_headers
|
95
|
-
default_headers = {
|
96
|
-
"accept" => "application/json",
|
97
|
-
"date" => Time.now.httpdate,
|
98
|
-
"host" => URI(self.endpoint).host,
|
99
|
-
"x-acs-signature-nonce" => SecureRandom.hex(16),
|
100
|
-
"x-acs-signature-method" => "HMAC-SHA1",
|
101
|
-
"x-acs-signature-version" => "1.0",
|
102
|
-
"x-acs-version" => self.api_version,
|
103
|
-
"x-sdk-client" => "RUBY(#{RUBY_VERSION})", # FIXME: 如何获取Gem的名称和版本号
|
104
|
-
"user-agent" => DEFAULT_UA
|
105
|
-
}
|
106
|
-
if self.security_token
|
107
|
-
default_headers["x-acs-accesskey-id"] = self.access_key_id
|
108
|
-
default_headers["x-acs-security-token"] = self.security_token
|
109
|
-
end
|
110
|
-
default_headers
|
111
|
-
end
|
112
|
-
|
113
|
-
private
|
114
|
-
def string_to_sign(method, uri, headers, query = {})
|
115
|
-
header_string = [
|
116
|
-
method,
|
117
|
-
headers["accept"],
|
118
|
-
headers["content-md5"] || "",
|
119
|
-
headers["content-type"] || "",
|
120
|
-
headers["date"],
|
121
|
-
].join("\n")
|
122
|
-
"#{header_string}\n#{canonicalized_headers(headers)}#{canonicalized_resource(uri, query)}"
|
123
|
-
end
|
124
|
-
|
125
|
-
def canonicalized_headers(headers)
|
126
|
-
headers.keys.select { |key| key.to_s.start_with? "x-acs-" }
|
127
|
-
.sort.map { |key| "#{key}:#{headers[key].strip}\n" }.join
|
128
|
-
end
|
129
|
-
|
130
|
-
def canonicalized_resource(uri, query_hash = {})
|
131
|
-
query_string = query_hash.sort.map { |key, value| "#{key}=#{value}" }.join("&")
|
132
|
-
query_string.empty? ? uri : "#{uri}?#{query_string}"
|
133
|
-
end
|
134
|
-
|
135
|
-
def authorization(string_to_sign)
|
136
|
-
"acs #{self.access_key_id}:#{signature(string_to_sign)}"
|
137
|
-
end
|
138
|
-
|
139
|
-
def signature(string_to_sign)
|
140
|
-
Base64.encode64(OpenSSL::HMAC.digest("sha1", self.access_key_secret, string_to_sign)).strip
|
141
|
-
end
|
142
|
-
|
143
|
-
def validate(config)
|
144
|
-
raise ArgumentError, 'must pass "config"' unless config
|
145
|
-
raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
|
146
|
-
unless config[:endpoint].start_with?("http://") || config[:endpoint].start_with?("https://")
|
147
|
-
raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
|
148
|
-
end
|
149
|
-
raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
|
150
|
-
raise ArgumentError, 'must pass "config[:access_key_id]"' unless config[:access_key_id]
|
151
|
-
raise ArgumentError, 'must pass "config[:access_key_secret]"' unless config[:access_key_secret]
|
152
|
-
end
|
153
|
-
|
154
|
-
class ACSError < StandardError
|
155
|
-
attr_accessor :code
|
156
|
-
|
157
|
-
def initialize(error)
|
158
|
-
self.code = error["Code"]
|
159
|
-
message = error["Message"]
|
160
|
-
host_id = error["HostId"]
|
161
|
-
request_id = error["RequestId"]
|
162
|
-
super("#{message} host_id: #{host_id}, request_id: #{request_id}")
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "securerandom"
|
5
|
+
require "active_support/all"
|
6
|
+
require "net/http"
|
7
|
+
|
8
|
+
module Net::HTTPHeader
|
9
|
+
def capitalize(name)
|
10
|
+
name
|
11
|
+
end
|
12
|
+
private :capitalize
|
13
|
+
end
|
14
|
+
|
15
|
+
module Aliyun
|
16
|
+
module Rails
|
17
|
+
module Connector
|
18
|
+
class ROAClient
|
19
|
+
attr_accessor :endpoint, :api_version, :access_key_id,
|
20
|
+
:access_key_secret, :security_token, :hostname, :opts
|
21
|
+
|
22
|
+
def initialize(config)
|
23
|
+
validate config
|
24
|
+
|
25
|
+
self.endpoint = config[:endpoint]
|
26
|
+
self.api_version = config[:api_version]
|
27
|
+
self.access_key_id = config[:access_key_id]
|
28
|
+
self.access_key_secret = config[:access_key_secret]
|
29
|
+
self.security_token = config[:security_token]
|
30
|
+
end
|
31
|
+
|
32
|
+
def request(method:, uri:, params: {}, body: {}, headers: {}, options: {})
|
33
|
+
# :"Content-Type" => "application/json" to "content-type" => "application/json"
|
34
|
+
headers.deep_transform_keys! { |key| key.to_s.downcase }
|
35
|
+
mix_headers = default_headers.merge(headers)
|
36
|
+
|
37
|
+
response = connection.send(method.downcase) do |request|
|
38
|
+
request.url uri, params
|
39
|
+
if body
|
40
|
+
if mix_headers["content-type"].start_with? "application/json"
|
41
|
+
request_body = body.to_json
|
42
|
+
elsif mix_headers["content-type"].start_with? "application/x-www-form-urlencoded"
|
43
|
+
request_body = URI.encode_www_form(body)
|
44
|
+
else
|
45
|
+
request_body = body
|
46
|
+
end
|
47
|
+
mix_headers["content-md5"] = Digest::MD5.base64digest request_body
|
48
|
+
mix_headers["content-length"] = request_body.length.to_s
|
49
|
+
request.body = request_body
|
50
|
+
end
|
51
|
+
string2sign = string_to_sign(method, uri, mix_headers, params)
|
52
|
+
mix_headers[:authorization] = authorization(string2sign)
|
53
|
+
mix_headers.each { |key, value| request.headers[key] = value }
|
54
|
+
end
|
55
|
+
|
56
|
+
return response if options.has_key? :raw_body
|
57
|
+
|
58
|
+
response_content_type = response.headers["Content-Type"] || ""
|
59
|
+
if response_content_type.start_with?("application/json")
|
60
|
+
if response.status >= 400
|
61
|
+
result = JSON.parse(response.body)
|
62
|
+
raise StandardError, "code: #{response.status}, #{result['Message']} requestid: #{result['RequestId']}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
if response_content_type.start_with?("text/xml")
|
67
|
+
result = Hash.from_xml(response.body)
|
68
|
+
raise ACSError, result["Error"] if result["Error"]
|
69
|
+
end
|
70
|
+
|
71
|
+
response
|
72
|
+
end
|
73
|
+
|
74
|
+
def connection(adapter = Faraday.default_adapter)
|
75
|
+
Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
|
76
|
+
end
|
77
|
+
|
78
|
+
def get(uri: "", headers: {}, params: {}, options: {})
|
79
|
+
request(method: :get, uri: uri, params: params, body: {}, headers: headers, options: options)
|
80
|
+
end
|
81
|
+
|
82
|
+
def post(uri: "", headers: {}, params: {}, body: {}, options: {})
|
83
|
+
request(method: :post, uri: uri, params: params, body: body, headers: headers, options: options)
|
84
|
+
end
|
85
|
+
|
86
|
+
def put(uri: "", headers: {}, params: {}, body: {}, options: {})
|
87
|
+
request(method: :put, uri: uri, params: params, body: body, headers: headers, options: options)
|
88
|
+
end
|
89
|
+
|
90
|
+
def delete(uri: "", headers: {}, params: {}, options: {})
|
91
|
+
request(method: :delete, uri: uri, params: params, body: {}, headers: headers, options: options)
|
92
|
+
end
|
93
|
+
|
94
|
+
def default_headers
|
95
|
+
default_headers = {
|
96
|
+
"accept" => "application/json",
|
97
|
+
"date" => Time.now.httpdate,
|
98
|
+
"host" => URI(self.endpoint).host,
|
99
|
+
"x-acs-signature-nonce" => SecureRandom.hex(16),
|
100
|
+
"x-acs-signature-method" => "HMAC-SHA1",
|
101
|
+
"x-acs-signature-version" => "1.0",
|
102
|
+
"x-acs-version" => self.api_version,
|
103
|
+
"x-sdk-client" => "RUBY(#{RUBY_VERSION})", # FIXME: 如何获取Gem的名称和版本号
|
104
|
+
"user-agent" => DEFAULT_UA
|
105
|
+
}
|
106
|
+
if self.security_token
|
107
|
+
default_headers["x-acs-accesskey-id"] = self.access_key_id
|
108
|
+
default_headers["x-acs-security-token"] = self.security_token
|
109
|
+
end
|
110
|
+
default_headers
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def string_to_sign(method, uri, headers, query = {})
|
115
|
+
header_string = [
|
116
|
+
method,
|
117
|
+
headers["accept"],
|
118
|
+
headers["content-md5"] || "",
|
119
|
+
headers["content-type"] || "",
|
120
|
+
headers["date"],
|
121
|
+
].join("\n")
|
122
|
+
"#{header_string}\n#{canonicalized_headers(headers)}#{canonicalized_resource(uri, query)}"
|
123
|
+
end
|
124
|
+
|
125
|
+
def canonicalized_headers(headers)
|
126
|
+
headers.keys.select { |key| key.to_s.start_with? "x-acs-" }
|
127
|
+
.sort.map { |key| "#{key}:#{headers[key].strip}\n" }.join
|
128
|
+
end
|
129
|
+
|
130
|
+
def canonicalized_resource(uri, query_hash = {})
|
131
|
+
query_string = query_hash.sort.map { |key, value| "#{key}=#{value}" }.join("&")
|
132
|
+
query_string.empty? ? uri : "#{uri}?#{query_string}"
|
133
|
+
end
|
134
|
+
|
135
|
+
def authorization(string_to_sign)
|
136
|
+
"acs #{self.access_key_id}:#{signature(string_to_sign)}"
|
137
|
+
end
|
138
|
+
|
139
|
+
def signature(string_to_sign)
|
140
|
+
Base64.encode64(OpenSSL::HMAC.digest("sha1", self.access_key_secret, string_to_sign)).strip
|
141
|
+
end
|
142
|
+
|
143
|
+
def validate(config)
|
144
|
+
raise ArgumentError, 'must pass "config"' unless config
|
145
|
+
raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
|
146
|
+
unless config[:endpoint].start_with?("http://") || config[:endpoint].start_with?("https://")
|
147
|
+
raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
|
148
|
+
end
|
149
|
+
raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
|
150
|
+
raise ArgumentError, 'must pass "config[:access_key_id]"' unless config[:access_key_id]
|
151
|
+
raise ArgumentError, 'must pass "config[:access_key_secret]"' unless config[:access_key_secret]
|
152
|
+
end
|
153
|
+
|
154
|
+
class ACSError < StandardError
|
155
|
+
attr_accessor :code
|
156
|
+
|
157
|
+
def initialize(error)
|
158
|
+
self.code = error["Code"]
|
159
|
+
message = error["Message"]
|
160
|
+
host_id = error["HostId"]
|
161
|
+
request_id = error["RequestId"]
|
162
|
+
super("#{message} host_id: #{host_id}, request_id: #{request_id}")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -1,122 +1,117 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "set"
|
4
|
-
require "openssl"
|
5
|
-
require "faraday"
|
6
|
-
require "erb"
|
7
|
-
require "active_support/all"
|
8
|
-
|
9
|
-
module Aliyun
|
10
|
-
module Rails
|
11
|
-
module Connector
|
12
|
-
class RPCClient
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
params
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
require "openssl"
|
5
|
+
require "faraday"
|
6
|
+
require "erb"
|
7
|
+
require "active_support/all"
|
8
|
+
|
9
|
+
module Aliyun
|
10
|
+
module Rails
|
11
|
+
module Connector
|
12
|
+
class RPCClient
|
13
|
+
attr_accessor :endpoint, :api_version, :access_key_id, :access_key_secret,
|
14
|
+
:security_token, :codes, :opts, :verbose
|
15
|
+
|
16
|
+
# 对象初始化属性
|
17
|
+
def initialize(config = {}, verbose = false)
|
18
|
+
validate config
|
19
|
+
|
20
|
+
self.endpoint = config[:endpoint]
|
21
|
+
self.api_version = config[:api_version]
|
22
|
+
self.access_key_id = config[:access_key_id] || Aliyun::Rails.access_key_id
|
23
|
+
self.access_key_secret = config[:access_key_secret] || Aliyun::Rails.access_key_secret
|
24
|
+
self.security_token = config[:security_token]
|
25
|
+
self.opts = config[:opts] || {}
|
26
|
+
self.verbose = verbose.instance_of?(TrueClass) && verbose
|
27
|
+
self.codes = Set.new [200, "200", "OK", "Success"]
|
28
|
+
self.codes.merge config[:codes] if config[:codes]
|
29
|
+
end
|
30
|
+
|
31
|
+
# 通用请求接口
|
32
|
+
def request(action:, params: {}, opts: {})
|
33
|
+
opts = self.opts.merge(opts)
|
34
|
+
action = action.upcase_first if opts[:format_action]
|
35
|
+
params = format_params(params) unless opts[:format_params]
|
36
|
+
defaults = default_params
|
37
|
+
params = { Action: action }.merge(defaults).merge(params)
|
38
|
+
method = (opts[:method] || "GET").upcase
|
39
|
+
sign = "#{method}&#{encode('/')}&#{encode(params.to_query)}"
|
40
|
+
secret = "#{self.access_key_secret}&"
|
41
|
+
signature = Base64.encode64(OpenSSL::HMAC.digest("sha1", secret, sign)).strip
|
42
|
+
params["Signature"] = signature
|
43
|
+
|
44
|
+
# 转换为 query 样式
|
45
|
+
query_string = params.to_query
|
46
|
+
|
47
|
+
# 特殊处理 POST
|
48
|
+
uri = opts[:method] == "POST" ? "/" : "/?#{query_string}"
|
49
|
+
|
50
|
+
# 初始化会话
|
51
|
+
response = connection.send(method.downcase, uri) do |r|
|
52
|
+
if opts[:method] == "POST"
|
53
|
+
r.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
54
|
+
r.body = query_string
|
55
|
+
end
|
56
|
+
r.headers["User-Agent"] = DEFAULT_UA
|
57
|
+
end
|
58
|
+
|
59
|
+
# 解析接口响应
|
60
|
+
response_body = JSON.parse(response.body)
|
61
|
+
if response_body["Code"] && !response_body["Code"].to_s.empty? && !self.codes.include?(response_body["Code"])
|
62
|
+
raise StandardError, "Code: #{response_body['Code']}, Message: #{response_body['Message']}, URL: #{uri}"
|
63
|
+
end
|
64
|
+
|
65
|
+
response_body
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def connection(adapter = Faraday.default_adapter)
|
70
|
+
Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
|
71
|
+
end
|
72
|
+
|
73
|
+
# 设置缺省参数
|
74
|
+
def default_params
|
75
|
+
params = {
|
76
|
+
Format: "JSON",
|
77
|
+
SignatureMethod: "HMAC-SHA1",
|
78
|
+
SignatureNonce: SecureRandom.hex(8),
|
79
|
+
SignatureVersion: "1.0",
|
80
|
+
Timestamp: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
81
|
+
AccessKeyId: self.access_key_id,
|
82
|
+
Version: self.api_version,
|
83
|
+
}
|
84
|
+
params[:SecurityToken] = self.security_token if self.security_token.present?
|
85
|
+
params
|
86
|
+
end
|
87
|
+
|
88
|
+
# 消息签名需要
|
89
|
+
def encode(input)
|
90
|
+
ERB::Util.url_encode input
|
91
|
+
end
|
92
|
+
|
93
|
+
# 转换 HASH key 样式
|
94
|
+
def format_params(param_hash)
|
95
|
+
param_hash.keys.each { |key| param_hash[(key.to_s.upcase_first).to_sym] = param_hash.delete key }
|
96
|
+
param_hash
|
97
|
+
end
|
98
|
+
|
99
|
+
def validate(config)
|
100
|
+
config.with_indifferent_access
|
101
|
+
raise ArgumentError, 'must pass "config"' unless config
|
102
|
+
raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
|
103
|
+
unless config[:endpoint].match?(/^http[s]?:/i)
|
104
|
+
raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
|
105
|
+
end
|
106
|
+
raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
|
107
|
+
unless config[:access_key_id] || Aliyun::Rails.access_key_id
|
108
|
+
raise ArgumentError, 'must pass "config[:access_key_id]" or define "Aliyun::Rails.access_key_id"'
|
109
|
+
end
|
110
|
+
unless config[:access_key_secret] || Aliyun::Rails.access_key_secret
|
111
|
+
raise ArgumentError, 'must pass "config[:access_key_secret]" or define "Aliyun::Rails.access_key_secret"'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
data/lib/aliyun/rails/dysms.rb
CHANGED
@@ -1,30 +1,27 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Aliyun
|
4
|
-
module Rails
|
5
|
-
class Dysms < Aliyun::Rails::Connector::RPCClient
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aliyun
|
4
|
+
module Rails
|
5
|
+
class Dysms < Aliyun::Rails::Connector::RPCClient
|
6
|
+
# 本产品(Dysmsapi/2017-05-25)的OpenAPI采用RPC签名风格,签名细节参见签名机制说明。
|
7
|
+
# 我们已经为开发者封装了常见编程语言的SDK,开发者可通过下载SDK直接调用本产品OpenAPI而无需关心技术细节。
|
8
|
+
def initialize(config = {}, verbose = nil)
|
9
|
+
config[:endpoint] ||= "http://dysmsapi.aliyuncs.com"
|
10
|
+
config[:api_version] ||= "2017-05-25"
|
11
|
+
super(config, verbose)
|
12
|
+
end
|
13
|
+
|
14
|
+
# 发送短信,发送前要申请短信签名和短信模板,并确保签名和模板已审核通过。
|
15
|
+
def send_sms(phone_numbers, template_code, template_param, sign_name = "")
|
16
|
+
params = {
|
17
|
+
PhoneNumbers: phone_numbers,
|
18
|
+
SignName: sign_name,
|
19
|
+
TemplateCode: template_code,
|
20
|
+
TemplateParam: template_param.to_json
|
21
|
+
}
|
22
|
+
opts = { method: "POST", timeout: 15000 }
|
23
|
+
request(action: "SendSms", params: params, opts: opts)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/aliyun/rails/dyvms.rb
CHANGED
@@ -1,30 +1,27 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Aliyun
|
4
|
-
module Rails
|
5
|
-
class Dyvms < Aliyun::Rails::Connector::RPCClient
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aliyun
|
4
|
+
module Rails
|
5
|
+
class Dyvms < Aliyun::Rails::Connector::RPCClient
|
6
|
+
# 本产品(Dyvmsapi/2017-05-25)的OpenAPI采用RPC签名风格,签名细节参见签名机制说明。
|
7
|
+
# 我们已经为开发者封装了常见编程语言的SDK,开发者可通过下载SDK直接调用本产品OpenAPI而无需关心技术细节。
|
8
|
+
def initialize(config = {}, verbose = nil)
|
9
|
+
config[:endpoint] ||= "http://dyvmsapi.aliyuncs.com"
|
10
|
+
config[:api_version] ||= "2017-05-25"
|
11
|
+
super(config, verbose)
|
12
|
+
end
|
13
|
+
|
14
|
+
# 调用SingleCallByTts接口向指定号码发送语音验证码和带参数变量的语音通知
|
15
|
+
def single_call_by_tts(called_show_number, called_number, tts_code, tts_param)
|
16
|
+
params = {
|
17
|
+
CalledShowNumber: called_show_number,
|
18
|
+
CalledNumber: called_number,
|
19
|
+
TtsCode: tts_code,
|
20
|
+
TtsParam: tts_param.to_json
|
21
|
+
}
|
22
|
+
opts = { method: "POST", timeout: 15000 }
|
23
|
+
request(action: "SendSms", params: params, opts: opts)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/aliyun/rails/version.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Aliyun
|
4
|
-
module Rails
|
5
|
-
VERSION = "0.1.
|
6
|
-
DEFAULT_UA = "AlibabaCloud (#{Gem::Platform.local.os}; " +
|
7
|
-
"#{Gem::Platform.local.cpu}) Ruby/#{RUBY_VERSION} Core/#{VERSION}"
|
8
|
-
end
|
9
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Aliyun
|
4
|
+
module Rails
|
5
|
+
VERSION = "0.1.12"
|
6
|
+
DEFAULT_UA = "AlibabaCloud (#{Gem::Platform.local.os}; " +
|
7
|
+
"#{Gem::Platform.local.cpu}) Ruby/#{RUBY_VERSION} Core/#{VERSION}"
|
8
|
+
end
|
9
|
+
end
|
data/lib/aliyun/rails.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rails/version"
|
4
|
+
require_relative "rails/connector/roa_client"
|
5
|
+
require_relative "rails/connector/rpc_client"
|
6
|
+
require_relative "rails/dyvms"
|
7
|
+
require_relative "rails/dysms"
|
8
|
+
|
9
|
+
module Aliyun
|
10
|
+
module Rails
|
11
|
+
class Error < StandardError; end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :access_key_id, :access_key_secret
|
15
|
+
def config
|
16
|
+
yield self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Your code goes here...
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aliyun-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WENWU.YAN
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -47,7 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- README.md
|
49
49
|
- Rakefile
|
50
|
-
- lib/aliyun
|
50
|
+
- lib/aliyun/rails.rb
|
51
51
|
- lib/aliyun/rails/connector/roa_client.rb
|
52
52
|
- lib/aliyun/rails/connector/rpc_client.rb
|
53
53
|
- lib/aliyun/rails/dysms.rb
|
@@ -60,7 +60,7 @@ metadata:
|
|
60
60
|
homepage_uri: https://github.com/ciscolive/aliyun-rails
|
61
61
|
source_code_uri: https://github.com/ciscolive/aliyun-rails
|
62
62
|
changelog_uri: https://github.com/ciscolive/aliyun-rails/blob/main/README.md
|
63
|
-
post_install_message:
|
63
|
+
post_install_message:
|
64
64
|
rdoc_options: []
|
65
65
|
require_paths:
|
66
66
|
- lib
|
@@ -75,8 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.3.
|
79
|
-
signing_key:
|
78
|
+
rubygems_version: 3.3.7
|
79
|
+
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: 非阿里云官方SDK,本GEM主要实现RAILS项目轻松调用 ** aliyun ** 相关接口
|
82
82
|
test_files: []
|
data/lib/aliyun-rails.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "aliyun/rails/version"
|
4
|
-
require "aliyun/rails/connector/roa_client"
|
5
|
-
require "aliyun/rails/connector/rpc_client"
|
6
|
-
require "aliyun/rails/dysms"
|
7
|
-
require "aliyun/rails/dyvms"
|
8
|
-
|
9
|
-
module Aliyun
|
10
|
-
module Rails
|
11
|
-
class Error < StandardError; end
|
12
|
-
|
13
|
-
# 模块方法接受代码块初始化参数
|
14
|
-
class << self
|
15
|
-
def config(&block)
|
16
|
-
Aliyun::Rails::Connector::RPCClient.configure(&block)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Your code goes here...
|
21
|
-
end
|
22
|
-
end
|