aliyun_green 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/Gemfile.lock +12 -1
- data/aliyun_green.gemspec +1 -5
- data/lib/aliyun_green/client.rb +110 -0
- data/lib/aliyun_green/configuration.rb +23 -0
- data/lib/aliyun_green/error.rb +18 -0
- data/lib/aliyun_green/text.rb +24 -0
- data/lib/aliyun_green/version.rb +1 -1
- data/lib/aliyun_green.rb +23 -3
- metadata +20 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51f696160ff4a798786b7aeb95ab6f311408f6110a43eab6135be4cf049b998a
|
4
|
+
data.tar.gz: 9380d90805a1c12a820a238eb0fdd8c44015ecce3bac434f8b2a5e5b55926111
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9b0b9297d363b52c750070ff99c43ed369685d9ee85c74b46f6c286fb9cbb8a0e5a8eb0e926d9cb1d5abc72d150a3fb1fb5afa63711f6a9a49f1415eb5cb243
|
7
|
+
data.tar.gz: 77ce87b6864fbdebc6f481657265919921d9267b14aa52c4714e0c05caf85f34809030eca00e7c8bcbb53f7a7b554de05dc42783d6ea96cdbf57d7c4a8908264
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,25 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
aliyun_green (0.1.
|
4
|
+
aliyun_green (0.1.1)
|
5
|
+
httpx
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
9
10
|
ast (2.4.2)
|
11
|
+
coderay (1.1.3)
|
10
12
|
diff-lcs (1.4.4)
|
13
|
+
http-2-next (0.5.0)
|
14
|
+
httpx (0.18.1)
|
15
|
+
http-2-next (>= 0.4.1)
|
16
|
+
method_source (1.0.0)
|
11
17
|
parallel (1.21.0)
|
12
18
|
parser (3.0.3.1)
|
13
19
|
ast (~> 2.4.1)
|
20
|
+
pry (0.14.1)
|
21
|
+
coderay (~> 1.1)
|
22
|
+
method_source (~> 1.0)
|
14
23
|
rainbow (3.0.0)
|
15
24
|
rake (13.0.6)
|
16
25
|
regexp_parser (2.2.0)
|
@@ -47,6 +56,8 @@ PLATFORMS
|
|
47
56
|
|
48
57
|
DEPENDENCIES
|
49
58
|
aliyun_green!
|
59
|
+
bundler
|
60
|
+
pry (~> 0.14.1)
|
50
61
|
rake (~> 13.0)
|
51
62
|
rspec (~> 3.0)
|
52
63
|
rubocop (~> 1.7)
|
data/aliyun_green.gemspec
CHANGED
@@ -31,9 +31,5 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
32
|
spec.require_paths = ["lib"]
|
33
33
|
|
34
|
-
|
35
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
36
|
-
|
37
|
-
# For more information and examples about making a new gem, checkout our
|
38
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
spec.add_dependency 'httpx'
|
39
35
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require "httpx"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
# TODO: validate config
|
5
|
+
# TODO: 支持clientInfo
|
6
|
+
|
7
|
+
module AliyunGreen
|
8
|
+
class Client
|
9
|
+
attr_accessor :endpoint, :api_version, :access_key_id,:access_key_secret, :enable_internal
|
10
|
+
|
11
|
+
def initialize(config)
|
12
|
+
self.endpoint = config[:endpoint]
|
13
|
+
self.api_version = config[:api_version]
|
14
|
+
self.access_key_id = config[:access_key_id]
|
15
|
+
self.access_key_secret = config[:access_key_secret]
|
16
|
+
self.enable_internal = config[:enable_internal]
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(uri , tasks, params = {})
|
20
|
+
payload = {
|
21
|
+
bizType: 'default',
|
22
|
+
scenes: ["antispam"],
|
23
|
+
tasks: tasks
|
24
|
+
}
|
25
|
+
|
26
|
+
url = "https://#{get_host(self.endpoint)}#{uri}"
|
27
|
+
|
28
|
+
mix_headers = default_headers
|
29
|
+
request_body = payload.to_json
|
30
|
+
|
31
|
+
mix_headers['content-md5'] = Digest::MD5.base64digest request_body
|
32
|
+
mix_headers['content-length'] = request_body.length.to_s
|
33
|
+
|
34
|
+
string2sign = string_to_sign('/green/text/scan', mix_headers, params)
|
35
|
+
mix_headers.merge!(authorization: authorization(string2sign))
|
36
|
+
|
37
|
+
response = HTTPX.with(headers: mix_headers).post(url, body: payload.to_json)
|
38
|
+
|
39
|
+
r = JSON.parse(response)
|
40
|
+
|
41
|
+
raise AliyunGreen::Error::SignatureDoesNotMatchError if r["Code"] == 'SignatureDoesNotMatch'
|
42
|
+
raise AliyunGreen::Error::ClientError.new(r["msg"], r["code"]) if r["code"] != 200
|
43
|
+
|
44
|
+
r
|
45
|
+
end
|
46
|
+
|
47
|
+
def string_to_sign(uri, headers, query = {})
|
48
|
+
header_string = [
|
49
|
+
'POST',
|
50
|
+
headers['accept'],
|
51
|
+
headers['content-md5'] || '',
|
52
|
+
headers['content-type'] || '',
|
53
|
+
headers['date'],
|
54
|
+
].join("\n")
|
55
|
+
"#{header_string}\n#{canonicalized_headers(headers)}#{canonicalized_resource(uri, query)}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def canonicalized_headers(headers)
|
59
|
+
headers.keys.select { |key| key.to_s.start_with? 'x-acs-' }
|
60
|
+
.sort.map { |key| "#{key}:#{headers[key].strip}\n" }.join
|
61
|
+
end
|
62
|
+
|
63
|
+
def canonicalized_resource(uri, query_hash = {})
|
64
|
+
query_string = query_hash.sort.map { |key, value| "#{key}=#{value}" }.join('&')
|
65
|
+
query_string.empty? ? uri : "#{uri}?#{query_string}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def authorization(string_to_sign)
|
69
|
+
"acs #{self.access_key_id}:#{signature(string_to_sign)}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def signature(string_to_sign)
|
73
|
+
Base64.encode64(OpenSSL::HMAC.digest('sha1', self.access_key_secret, string_to_sign)).strip
|
74
|
+
end
|
75
|
+
|
76
|
+
def default_headers
|
77
|
+
default_headers = {
|
78
|
+
'accept' => 'application/json',
|
79
|
+
'content-type' => 'application/json',
|
80
|
+
'date' => Time.now.httpdate,
|
81
|
+
'host' => get_host(self.endpoint),
|
82
|
+
'x-acs-version' => self.api_version,
|
83
|
+
'x-acs-signature-nonce' => SecureRandom.hex(16),
|
84
|
+
'x-acs-signature-version' => '1.0',
|
85
|
+
'x-acs-signature-method' => 'HMAC-SHA1',
|
86
|
+
}
|
87
|
+
default_headers
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_host(endpoint)
|
91
|
+
externals = {
|
92
|
+
'cn-shanghai' => 'green.cn-shanghai.aliyuncs.com',
|
93
|
+
'cn-beijing' => 'green.cn-beijing.aliyuncs.com',
|
94
|
+
'cn-shenzhen' => 'green.cn-shenzhen.aliyuncs.com',
|
95
|
+
'ap-southeast-1' => 'green.ap-southeast-1.aliyuncs.com'
|
96
|
+
}
|
97
|
+
internals = {
|
98
|
+
'cn-shanghai' => 'green-vpc.cn-shanghai.aliyuncs.com',
|
99
|
+
'cn-beijing' => 'green-vpc.cn-beijing.aliyuncs.com',
|
100
|
+
'cn-shenzhen' => 'green-vpc.cn-shenzhen.aliyuncs.com',
|
101
|
+
'ap-southeast-1' => 'green-vpc.ap-southeast-1.aliyuncs.com'
|
102
|
+
}
|
103
|
+
if self.enable_internal
|
104
|
+
internals[endpoint]
|
105
|
+
else
|
106
|
+
externals[endpoint]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module AliyunGreen
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :access_key_id, :access_key_secret,
|
4
|
+
:region_id, :api_version, :enable_internal
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@access_key_id = ""
|
8
|
+
@access_key_secret = ""
|
9
|
+
@endpoint = "cn-beijing"
|
10
|
+
@api_version = "2018-05-09"
|
11
|
+
@enable_internal = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_hash
|
15
|
+
{
|
16
|
+
endpoint: @endpoint,
|
17
|
+
api_version: @api_version,
|
18
|
+
access_key_id: @access_key_id,
|
19
|
+
access_key_secret: @access_key_secret,
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module AliyunGreen
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :code
|
4
|
+
ClientError = Class.new(self)
|
5
|
+
|
6
|
+
# 596 账号未授权、账号欠费、账号未开通、账号被禁等原因,具体可以参考返回的msg。
|
7
|
+
PermissionDenyError = Class.new(ClientError)
|
8
|
+
|
9
|
+
# SignatureDoesNotMatch
|
10
|
+
SignatureDoesNotMatchError = Class.new(ClientError)
|
11
|
+
|
12
|
+
def initialize(message = '', code = nil)
|
13
|
+
super(message)
|
14
|
+
|
15
|
+
@code = code
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module AliyunGreen
|
2
|
+
module Text
|
3
|
+
def self.scan(content, data_id = nil)
|
4
|
+
tasks = [
|
5
|
+
{
|
6
|
+
dataId: data_id,
|
7
|
+
content: content
|
8
|
+
}
|
9
|
+
]
|
10
|
+
|
11
|
+
response = AliyunGreen.client.post('/green/text/scan', tasks)
|
12
|
+
|
13
|
+
response
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.bulk_scan
|
17
|
+
# TODO
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.feedback
|
21
|
+
# TODO
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/aliyun_green/version.rb
CHANGED
data/lib/aliyun_green.rb
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
2
|
require_relative "aliyun_green/version"
|
3
|
+
require_relative "aliyun_green/text"
|
4
|
+
require_relative "aliyun_green/error"
|
5
|
+
require_relative "aliyun_green/client"
|
6
|
+
require_relative "aliyun_green/configuration"
|
4
7
|
|
5
8
|
module AliyunGreen
|
6
|
-
|
7
|
-
|
9
|
+
attr_writer :configuration
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.client
|
20
|
+
AliyunGreen::Client.new(
|
21
|
+
endpoint: 'cn-beijing',
|
22
|
+
api_version: '2018-05-09',
|
23
|
+
access_key_id: AliyunGreen.configuration.access_key_id,
|
24
|
+
access_key_secret: AliyunGreen.configuration.access_key_secret,
|
25
|
+
enable_internal: AliyunGreen.configuration.enable_internal
|
26
|
+
)
|
27
|
+
end
|
8
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aliyun_green
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 42up
|
@@ -9,7 +9,21 @@ autorequire:
|
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
11
|
date: 2021-12-07 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httpx
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: Write a longer description or delete this line.
|
14
28
|
email:
|
15
29
|
- foobar@v2up.com
|
@@ -29,6 +43,10 @@ files:
|
|
29
43
|
- bin/console
|
30
44
|
- bin/setup
|
31
45
|
- lib/aliyun_green.rb
|
46
|
+
- lib/aliyun_green/client.rb
|
47
|
+
- lib/aliyun_green/configuration.rb
|
48
|
+
- lib/aliyun_green/error.rb
|
49
|
+
- lib/aliyun_green/text.rb
|
32
50
|
- lib/aliyun_green/version.rb
|
33
51
|
homepage: https://github.com/42up/aliyun_green
|
34
52
|
licenses:
|