aliyun-rails 0.1.18 → 0.1.19

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
  SHA256:
3
- metadata.gz: 2e27be7e1a623946c583fc2d8dfd8144c76352b1370f37954f07d1a92379903d
4
- data.tar.gz: 73ce2236eacb879c9a4648a1b03701a2f33dfd5ab3060a854fbbb51c54fb50ad
3
+ metadata.gz: cb272181c60129e57686d1086eb34e5b4cb20776926c600ca9865e8a8e7537d3
4
+ data.tar.gz: 345f2c7bfc4135d42c4f8d9a0c4caab0218a5c419bde70fb4e13ae5f44414ac0
5
5
  SHA512:
6
- metadata.gz: 6db85298bf709c4688a92b7c45c34376c043156ae00443882030e1382b6ae36a5eef5456b1c3f317074799440d7edce17751afb1b560a98ad7cef09425014bfb
7
- data.tar.gz: 5041a64210b45a9cf036e4573bb33a9cbc8a652c2f9fcf167f634477cf816a5daf4709e9e3836718a7bc306e2f424edc9b99f21d686e53579c929e03ddf8b49f
6
+ metadata.gz: 62c17e6d850d702b250aefab7c0934990271a244ff97cfcc1e2f27870117f5ccbee62445e95fb0e3cec7207fedbc226986bd7e40ffd50da951e9bbdbad36ee46
7
+ data.tar.gz: 234b8e75fb51905c926ac4204c6ee5259a9f854ad0f8efb6b2b482a0870abc51fae36ce5203d131c970175474ccc06e4e3dddb0480f03d27d2b0a5471c8e1ba2
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.14] - 2022-04-01
4
+
5
+ - 优化 rpc_client 代码
6
+ - 优化 roa_client 代码
7
+ - 完善代码范例
8
+
9
+ ## [0.1.0] - 2022-03-31
10
+
11
+ - 首次发布代码
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at careline@foxmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1 @@
1
+ ## 待添加
@@ -1,116 +1,147 @@
1
- # frozen_string_literal: true
2
-
3
1
  require "set"
4
2
  require "openssl"
5
3
  require "faraday"
6
- require "erb"
7
4
  require "active_support/all"
8
5
 
9
6
  module Aliyun
10
- module Connector
11
- class RPCClient
12
- attr_accessor :endpoint, :api_version, :access_key_id, :access_key_secret,
13
- :security_token, :codes, :opts, :verbose
14
-
15
- # 对象初始化属性
16
- def initialize(config = {}, verbose = false)
17
- validate config
18
-
19
- self.endpoint = config[:endpoint]
20
- self.api_version = config[:api_version]
21
- self.access_key_id = config[:access_key_id] || Aliyun.access_key_id
22
- self.access_key_secret = config[:access_key_secret] || Aliyun.access_key_secret
23
- self.security_token = config[:security_token]
24
- self.opts = config[:opts] || {}
25
- self.verbose = verbose.instance_of?(TrueClass) && verbose
26
-
27
- self.codes = Set.new [200, "200", "OK", "Success"]
28
- self.codes.merge config[:codes] if config[:codes]
29
- end
30
7
 
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
8
+ class RPCClient
9
+
10
+ attr_accessor :endpoint, :api_version, :access_key_id, :access_key_secret,
11
+ :security_token, :codes, :opts, :verbose
12
+
13
+ # 对象初始化属性
14
+ def initialize(config, verbose = false)
15
+
16
+ validate config
17
+
18
+ self.endpoint = config[:endpoint]
19
+ self.api_version = config[:api_version]
20
+ self.access_key_id = config[:access_key_id] || Aliyun.access_key_id
21
+ self.access_key_secret = config[:access_key_secret] || Aliyun.access_key_secret
22
+ self.security_token = config[:security_token]
23
+ self.opts = config[:opts] || {}
24
+ self.verbose = verbose.instance_of?(TrueClass) && verbose
25
+
26
+ self.codes = Set.new [200, "200", "OK", "Success"]
27
+ self.codes.merge config[:codes] if config[:codes]
28
+ end
58
29
 
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}"
30
+ # 通用请求接口
31
+ def request(action:, params: {}, opts: {})
32
+ opts = self.opts.merge(opts)
33
+ action = action.upcase_first if opts[:format_action]
34
+ params = format_params(params) unless opts[:format_params]
35
+ defaults = default_params
36
+ params = { Action: action }.merge(defaults).merge(params)
37
+ method = (opts[:method] || "GET").upcase
38
+ normalized = normalize(params)
39
+ canonicalized = canonicalize(normalized)
40
+ string_to_sign = "#{method}&#{encode('/')}&#{encode(canonicalized)}"
41
+ secret = "#{self.access_key_secret}&"
42
+ signature = Base64.encode64(OpenSSL::HMAC.digest("sha1", secret, string_to_sign)).strip
43
+ normalized.push(["Signature", encode(signature)])
44
+
45
+ # 转换为 query 样式
46
+ querystring = canonicalize(normalized)
47
+
48
+ # 特殊处理 POST
49
+ uri = opts[:method] == "POST" ? "/" : "/?#{querystring}"
50
+
51
+ # 初始化会话
52
+ response = connection.send(method.downcase, uri) do |request|
53
+ if opts[:method] == "POST"
54
+ request.headers["Content-Type"] = "application/x-www-form-urlencoded"
55
+ request.body = querystring
63
56
  end
57
+ request.headers["User-Agent"] = DEFAULT_UA
58
+ end
64
59
 
65
- response_body
60
+ # 解析接口响应
61
+ response_body = JSON.parse(response.body)
62
+ if response_body["Code"] && !response_body["Code"].to_s.empty? && !self.codes.include?(response_body["Code"])
63
+ raise StandardError, "Code: #{response_body['Code']}, Message: #{response_body['Message']}, URL: #{uri}"
66
64
  end
67
65
 
68
- private
69
- def connection(adapter = Faraday.default_adapter)
70
- Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
71
- end
66
+ response_body
67
+ end
72
68
 
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
69
+ private
70
+ def connection(adapter = Faraday.default_adapter)
71
+ Faraday.new(url: self.endpoint) { |f| f.adapter adapter }
72
+ end
87
73
 
88
- # 消息签名需要
89
- def encode(input)
90
- ERB::Util.url_encode input
91
- end
74
+ # 设置缺省参数
75
+ def default_params
76
+ default_params = {
77
+ "Format" => "JSON",
78
+ "SignatureMethod" => "HMAC-SHA1",
79
+ "SignatureNonce" => SecureRandom.hex(16),
80
+ "SignatureVersion" => "1.0",
81
+ "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
82
+ "AccessKeyId" => self.access_key_id,
83
+ "Version" => self.api_version,
84
+ }
85
+ default_params["SecurityToken"] = self.security_token if self.security_token
86
+ default_params
87
+ end
92
88
 
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
89
+ # 消息签名需要
90
+ def encode(string)
91
+ ERB::Util.url_encode(string)
92
+ end
98
93
 
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.access_key_id
108
- raise ArgumentError, 'must pass "config[:access_key_id]" or define "Aliyun.access_key_id"'
94
+ # 转换 HASH key 样式
95
+ def format_params(param_hash)
96
+ param_hash.keys.each { |key| param_hash[(key.to_s.upcase_first).to_sym] = param_hash.delete key }
97
+ param_hash
98
+ end
99
+
100
+ def replace_repeat_list(target, key, repeat)
101
+ repeat.each_with_index do |item, index|
102
+ if item && item.instance_of?(Hash)
103
+ item.each_key { |k| target["#{key}.#{index.next}.#{k}"] = item[k] }
104
+ else
105
+ target["#{key}.#{index.next}"] = item
109
106
  end
110
- unless config[:access_key_secret] || Aliyun.access_key_secret
111
- raise ArgumentError, 'must pass "config[:access_key_secret]" or define "Aliyun.access_key_secret"'
107
+ end
108
+ target
109
+ end
110
+
111
+ def flat_params(params)
112
+ target = {}
113
+ params.each do |key, value|
114
+ if value.instance_of?(Array)
115
+ replace_repeat_list(target, key, value)
116
+ else
117
+ target[key.to_s] = value
112
118
  end
113
119
  end
114
- end
120
+ target
121
+ end
122
+
123
+ def normalize(params)
124
+ flat_params(params).sort.to_h.map { |key, value| [encode(key), encode(value)] }
125
+ end
126
+
127
+ def canonicalize(normalized)
128
+ normalized.map { |element| "#{element.first}=#{element.last}" }.join("&")
129
+ end
130
+
131
+ def validate(config = {})
132
+ config.with_indifferent_access
133
+ raise ArgumentError, 'must pass "config"' unless config
134
+ raise ArgumentError, 'must pass "config[:endpoint]"' unless config[:endpoint]
135
+ unless config[:endpoint].match?(/^http[s]?:/i)
136
+ raise ArgumentError, '"config.endpoint" must starts with \'https://\' or \'http://\'.'
137
+ end
138
+ raise ArgumentError, 'must pass "config[:api_version]"' unless config[:api_version]
139
+ unless config[:access_key_id] || Aliyun.access_key_id
140
+ raise ArgumentError, 'must pass "config[:access_key_id]" or define "Aliyun.access_key_id"'
141
+ end
142
+ unless config[:access_key_secret] || Aliyun.access_key_secret
143
+ raise ArgumentError, 'must pass "config[:access_key_secret]" or define "Aliyun.access_key_secret"'
144
+ end
145
+ end
115
146
  end
116
147
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aliyun
4
- VERSION = "0.1.18"
4
+ VERSION = "0.1.19"
5
5
  DEFAULT_UA = "AlibabaCloud (#{Gem::Platform.local.os}; " +
6
6
  "#{Gem::Platform.local.cpu}) Ruby/#{RUBY_VERSION} Core/#{VERSION}"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
@@ -45,6 +45,9 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - CHANGELOG.md
49
+ - CODE_OF_CONDUCT.md
50
+ - CONTRIBUTING.md
48
51
  - LICENSE.txt
49
52
  - README.md
50
53
  - Rakefile