dolphin_kit 1.0.1 → 1.0.3

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: 29b28c111e89a70ad3080b6f4cdc67dcea705a5992d122185fffb37d01d30df0
4
+ data.tar.gz: fed18ea224b57131a67b4464a4658ed9077300466d26b5c3b215003cc5c51e81
5
5
  SHA512:
6
- metadata.gz: 357b907a45d9f57e26bc7f68f4c4c5f011841e1fb9479ff44e73fa6ee2a98d28ab1b4c7fa4a7346cc12bc7e397bf4f597832891d4418f2ad355960c564d3c516
7
- data.tar.gz: a604711cb81eb24b9311c18f490e2f76f61f47f343b6de154b6b125c017bde5d327bb1f38561e40262d3c03dd5c7e9138217298a829acc8212ec74422ab2f2eb
6
+ metadata.gz: 49f2267df9d14cbc1fd7dd2a9b1eb966e34dfc73cf1c3e0d698a61622936c92f3d5917dac3de9512e60cd2334b7a7cd91580484b319e0823a80332d0d0f5b050
7
+ data.tar.gz: baf923ecde1c32e5b42204ef3c93094f20c0033691a2234098c8cef8857f4266f166e0ad5655ed10a473484838535dd0a10f73880061c73b4153a13d5b22c448
@@ -0,0 +1,18 @@
1
+ require 'net/http'
2
+
3
+ module DolphinKit
4
+ module HTTP
5
+ module_function
6
+
7
+ def get(url, params: {})
8
+ uri = URI(url)
9
+ uri.query = URI.encode_www_form(params) unless params.empty?
10
+ Net::HTTP.get_response(uri)
11
+ end
12
+
13
+ def post(url, form: {})
14
+ uri = URI(url)
15
+ Net::HTTP.post_form(uri, **form)
16
+ end
17
+ end
18
+ end
@@ -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.3'
3
3
  end
data/lib/dolphin_kit.rb CHANGED
@@ -1,2 +1,19 @@
1
+ require 'json'
2
+ require 'dolphin_kit/version'
3
+ require 'dolphin_kit/helpers/http'
4
+ require 'dolphin_kit/services/baidu_service'
5
+ require 'dolphin_kit/services/qcloud_service'
6
+ require 'dolphin_kit/services/youdao_service'
7
+ require 'dolphin_kit/service'
8
+
1
9
  module DolphinKit
10
+ module_function
11
+
12
+ def create_service(name, **args)
13
+ if args.empty?
14
+ args[:api_key] = ENV["#{name}_FANYI_API_KEY".upcase].to_s
15
+ end
16
+
17
+ Service.new(name, **args)
18
+ end
2
19
  end
metadata CHANGED
@@ -1,29 +1,15 @@
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.3
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
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: http
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '5.1'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '5.1'
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description:
28
14
  email:
29
15
  executables: []
@@ -31,6 +17,11 @@ extensions: []
31
17
  extra_rdoc_files: []
32
18
  files:
33
19
  - lib/dolphin_kit.rb
20
+ - lib/dolphin_kit/helpers/http.rb
21
+ - lib/dolphin_kit/service.rb
22
+ - lib/dolphin_kit/services/baidu_service.rb
23
+ - lib/dolphin_kit/services/qcloud_service.rb
24
+ - lib/dolphin_kit/services/youdao_service.rb
34
25
  - lib/dolphin_kit/version.rb
35
26
  homepage: https://github.com/storeos/dolphin_kit
36
27
  licenses: