baidu_translate 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/.gitignore +2 -0
- data/README.md +3 -1
- data/baidu_translate.gemspec +1 -0
- data/lib/baidu_translate/translation.rb +34 -37
- data/lib/baidu_translate/version.rb +1 -1
- data/lib/baidu_translate.rb +4 -4
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e247d8f671aa5de5ba22bff4983a709089ac676
|
4
|
+
data.tar.gz: b411301de1f800723f85758062ebf0bbb7e91a83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bff4e97af6b4e69b74d026a36e99802ac78262e3c61d1219ca49f019cdb741d5ae5f6821d16257bed0f57211b99e6e58776f60951201e46da46a51faa64d711
|
7
|
+
data.tar.gz: ac8025775c4e9c241ff2b441ab64829a0017bf259e87ddb8f601db7d4453a4b1274a01565269b73ac831cbd9290e09a75d9500804f835c7a3a69f51eadfdec55
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -35,9 +35,11 @@ BaiduTranslate.translate('Apple', :en, :zh)
|
|
35
35
|
# => "苹果"
|
36
36
|
```
|
37
37
|
|
38
|
+
4. Check http://api.fanyi.baidu.com/api/trans/product/apidoc for the list of available languages and their codes and a list of possible errors.
|
39
|
+
|
38
40
|
## Contributing
|
39
41
|
|
40
|
-
Bug reports and pull requests are welcome
|
42
|
+
Bug reports and pull requests are welcome!
|
41
43
|
|
42
44
|
|
43
45
|
## License
|
data/baidu_translate.gemspec
CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
32
32
|
spec.add_development_dependency "minitest", "~> 5.0"
|
33
33
|
spec.add_development_dependency "vcr"
|
34
|
+
spec.add_development_dependency "byebug"
|
34
35
|
spec.add_development_dependency "webmock"
|
35
36
|
|
36
37
|
spec.add_dependency "faraday"
|
@@ -5,40 +5,37 @@ require 'uri'
|
|
5
5
|
API_URL = "http://api.fanyi.baidu.com/api/trans/vip/translate"
|
6
6
|
|
7
7
|
module BaiduTranslate
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
end
|
44
|
-
end
|
8
|
+
module Translation
|
9
|
+
|
10
|
+
attr_accessor :app_id, :key
|
11
|
+
|
12
|
+
def setup(app_id, key)
|
13
|
+
@app_id = app_id
|
14
|
+
@key = key
|
15
|
+
end
|
16
|
+
|
17
|
+
def translate(text, from, to)
|
18
|
+
raise Exception.new("AppID and/or Key missing. Use BaiduTranslate.setup() to initialize.") unless @app_id && @key
|
19
|
+
|
20
|
+
salt = rand(32768..65536)
|
21
|
+
api_params = { q: text,
|
22
|
+
from: from,
|
23
|
+
to: to,
|
24
|
+
appid: @app_id,
|
25
|
+
salt: salt,
|
26
|
+
sign: sign(text, salt) }
|
27
|
+
|
28
|
+
response = Faraday.get(API_URL, api_params).body
|
29
|
+
parsed_response = JSON.parse(response)
|
30
|
+
raise Exception.new(parsed_response.to_s) if parsed_response.key? "error_code"
|
31
|
+
parsed_response['trans_result'][0]['dst']
|
32
|
+
end
|
33
|
+
|
34
|
+
def sign(text, salt)
|
35
|
+
undigested_sign = @app_id.to_s + text + salt.to_s + @key
|
36
|
+
md5 = Digest::MD5.new
|
37
|
+
md5 << undigested_sign
|
38
|
+
md5.hexdigest
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/baidu_translate.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baidu_translate
|
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
|
- Simon Nizov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: webmock
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|