dolphin_kit 1.0.1 → 1.0.2

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: 9598dc2fca3c29472762261af13675e33eb10728f268c868448c0a4bd0e3bf84
4
- data.tar.gz: ea45cde34433d8caf5261a5c7bc273c600360edd0b1d10a43dad447450c72219
3
+ metadata.gz: 545aad4e725c12ada156f91ccbe06c090daf9f7751f161e50d607f3a22fc70f9
4
+ data.tar.gz: 2bb1d5112a145349ade8bd9f457b82704840229d9331f4472c22651df19eaec7
5
5
  SHA512:
6
- metadata.gz: 357b907a45d9f57e26bc7f68f4c4c5f011841e1fb9479ff44e73fa6ee2a98d28ab1b4c7fa4a7346cc12bc7e397bf4f597832891d4418f2ad355960c564d3c516
7
- data.tar.gz: a604711cb81eb24b9311c18f490e2f76f61f47f343b6de154b6b125c017bde5d327bb1f38561e40262d3c03dd5c7e9138217298a829acc8212ec74422ab2f2eb
6
+ metadata.gz: 5077713b1df1d38d55d661fce2fc144afd6824d86aabf68aee4ee33379a38f46a8cab87df0d66c2b1d2e0041bac96cfde56d185a5eacdd8ed7d2d8be340b9228
7
+ data.tar.gz: c96a70c4bb7739a6de9d632fc8a3c0b50c438e64a6480d83af02d1be6e4a25be161b7bc1c3f1ebb741944adcb75b40c214e4873e98b9d6051da1c1406f504d60
@@ -0,0 +1,33 @@
1
+ module DolphinKit
2
+ class Service
3
+ attr_reader :name
4
+
5
+ def initialize(name = 'youdao', client_id: nil, client_secret: nil, api_key: '')
6
+ @name, @client_id, @client_secret = name.to_sym, client_id, client_secret
7
+
8
+ if @client_id.nil? and @client_secret.nil?
9
+ @client_id, @client_secret = api_key.split(':')
10
+ end
11
+
12
+ @instance = build_instance
13
+ end
14
+
15
+ def call(**args)
16
+ @instance.call(**args)
17
+ end
18
+
19
+ private
20
+
21
+ def build_instance
22
+ case @name
23
+ when :baidu
24
+ BaiduService.new(app_id: @client_id, app_key: @client_secret)
25
+ when :qcloud
26
+ QCloudService.new(app_id: @client_id, app_key: @client_secret)
27
+ else
28
+ @name = :youdao
29
+ YoudaoService.new(keyfrom: @client_id, key: @client_secret)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,84 @@
1
+ require 'digest/md5'
2
+
3
+ module DolphinKit
4
+ class BaiduService
5
+ BASE_URL = 'https://fanyi-api.baidu.com/api/trans'
6
+
7
+ def initialize(app_id: nil, app_key: nil, endpoint: nil)
8
+ @endpoint = "#{BASE_URL}/vip/translate"
9
+ @app_id, @app_key = app_id, app_key
10
+ end
11
+
12
+ def call(text:, source: 'auto', target: nil)
13
+ target ||= (LANGUAGES.keys - [source.to_sym]).first
14
+
15
+ params = { from: source, to: LANGUAGES[target.to_sym] }
16
+ params.merge!(build_params(text))
17
+
18
+ response = HTTP.post(@endpoint, form: params)
19
+ result = JSON.parse(response.body)
20
+
21
+ if (code = result['error_code'].to_i) > 52000
22
+ @failure = FAILURES[code]
23
+ else
24
+ @failure = nil
25
+ end
26
+
27
+ result
28
+ end
29
+
30
+ private
31
+
32
+ def build_params(value)
33
+ salt = Time.now.to_i.to_s
34
+ args = [@app_id, value, salt, @app_key]
35
+
36
+ {
37
+ appid: @app_id,
38
+ sign: sign(*args),
39
+ salt: salt,
40
+ q: value
41
+ }
42
+ end
43
+
44
+ def sign(*args)
45
+ Digest::MD5.hexdigest(args.join)
46
+ end
47
+
48
+ LANGUAGES = {
49
+ en: 'en',
50
+ zh: 'zh',
51
+ ja: 'jp',
52
+ ko: 'kor',
53
+ es: 'spa',
54
+ de: 'de',
55
+ fr: 'fra',
56
+ vi: 'vie',
57
+ th: 'th',
58
+ ar: 'ara',
59
+ hi: 'hi',
60
+ id: 'id',
61
+ it: 'it',
62
+ ms: 'may',
63
+ pt: 'pt',
64
+ ru: 'ru',
65
+ tr: 'tr',
66
+ 'zh-TW': 'cht'
67
+ }
68
+
69
+ FAILURES = {
70
+ 52001 => '请求超时',
71
+ 52002 => '系统错误',
72
+ 52003 => '未授权用户',
73
+ 54000 => '必填参数为空',
74
+ 54001 => '签名错误',
75
+ 54003 => '访问频率受限',
76
+ 54004 => '账户余额不足',
77
+ 54005 => 'Query 请求频繁',
78
+ 58000 => '客户端 IP 非法',
79
+ 58001 => '译文语言方向不支持',
80
+ 58002 => '服务当前已关闭',
81
+ 90107 => '认证未通过或未生效'
82
+ }
83
+ end
84
+ end
@@ -0,0 +1,60 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module DolphinKit
5
+ class QCloudService
6
+ BASE_URL = 'https://tmt.tencentcloudapi.com'
7
+
8
+ def initialize(app_id: nil, app_key: nil, endpoint: nil)
9
+ @secret_id, @secret_key = app_id, app_key
10
+ @endpoint = endpoint || BASE_URL
11
+ end
12
+
13
+ def call(text:, source: 'auto', target: nil)
14
+ target ||= (LANGUAGES - [source.to_sym]).first
15
+ params = { Source: source, Target: target }.merge(build_params(text))
16
+ params[:Signature] = sign(@secret_key, build_data(params))
17
+
18
+ response = HTTP.get(@endpoint, params: params)
19
+ result = JSON.parse(response.body)['Response']
20
+
21
+ @failure = result.dig('Error', 'Message')
22
+ result
23
+ end
24
+
25
+ private
26
+
27
+ def build_params(value)
28
+ {
29
+ Action: 'TextTranslate',
30
+ Region: 'ap-guangzhou',
31
+ Version: '2018-03-21',
32
+ ProjectId: '0',
33
+ SignatureMethod: 'HmacSHA256',
34
+ Timestamp: Time.now.to_i.to_s,
35
+ Nonce: rand(1000..9999).to_s,
36
+ SecretId: @secret_id,
37
+ SourceText: value
38
+ }
39
+ end
40
+
41
+ def build_data(params = {})
42
+ result = params
43
+ .keys
44
+ .sort
45
+ .map { |key| "#{key}=#{params[key]}" }
46
+ .join('&')
47
+
48
+ "GET#{URI(BASE_URL).host}/?#{result}"
49
+ end
50
+
51
+ def sign(key, data, algo = 'SHA256')
52
+ Base64.encode64(OpenSSL::HMAC.digest(algo, key, data))
53
+ end
54
+
55
+ LANGUAGES = %i{
56
+ en zh ja ko es de fr vi th
57
+ ar hi id it ms pt ru tr zh-TW
58
+ }
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ module DolphinKit
2
+ class YoudaoService
3
+ BASE_URL = 'https://fanyi.youdao.com'
4
+
5
+ def initialize(keyfrom: nil, key: nil, endpoint: nil)
6
+ @endpoint = "#{BASE_URL}/openapi.do"
7
+ @keyfrom, @key = keyfrom, key
8
+ end
9
+
10
+ def call(text:)
11
+ response = HTTP.get(@endpoint, params: build_params(text))
12
+ result = JSON.parse(response.body)
13
+
14
+ if (code = result['errorCode'].to_i) > 0
15
+ @failure = FAILURES[code]
16
+ else
17
+ @failure = nil
18
+ end
19
+
20
+ result
21
+ end
22
+
23
+ private
24
+
25
+ def build_params(value)
26
+ {
27
+ type: 'data',
28
+ doctype: 'json',
29
+ version: '1.1',
30
+ keyfrom: @keyfrom,
31
+ key: @key,
32
+ q: value
33
+ }
34
+ end
35
+
36
+ FAILURES = {
37
+ 20 => '要翻译的文本过长',
38
+ 30 => '无法进行有效的翻译',
39
+ 40 => '不支持的语言类型',
40
+ 50 => '无效的 Key',
41
+ 60 => '无词典结果,仅在获取词典结果生效'
42
+ }
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module DolphinKit
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
data/lib/dolphin_kit.rb CHANGED
@@ -1,2 +1,20 @@
1
+ require 'json'
2
+ require 'http'
3
+
4
+ require 'dolphin_kit/version'
5
+ require 'dolphin_kit/service'
6
+ require 'dolphin_kit/services/baidu_service'
7
+ require 'dolphin_kit/services/qcloud_service'
8
+ require 'dolphin_kit/services/youdao_service'
9
+
1
10
  module DolphinKit
11
+ module_function
12
+
13
+ def create_service(name, **args)
14
+ if args.empty?
15
+ args[:api_key] = ENV["#{name}_FANYI_API_KEY".upcase].to_s
16
+ end
17
+
18
+ Service.new(name, **args)
19
+ end
2
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolphin_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - razeos at tossdev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-12 00:00:00.000000000 Z
11
+ date: 2023-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -31,6 +31,10 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/dolphin_kit.rb
34
+ - lib/dolphin_kit/service.rb
35
+ - lib/dolphin_kit/services/baidu_service.rb
36
+ - lib/dolphin_kit/services/qcloud_service.rb
37
+ - lib/dolphin_kit/services/youdao_service.rb
34
38
  - lib/dolphin_kit/version.rb
35
39
  homepage: https://github.com/storeos/dolphin_kit
36
40
  licenses: