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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d27fad7d037927ae84ba2f2832b82392ec5e467
4
- data.tar.gz: 3b31c3f25953fbfb6391cbdf46a52b175dc547d2
3
+ metadata.gz: 0e247d8f671aa5de5ba22bff4983a709089ac676
4
+ data.tar.gz: b411301de1f800723f85758062ebf0bbb7e91a83
5
5
  SHA512:
6
- metadata.gz: a595156cf238c01dae2e95a9fcebd97aa01c2e3d9a4b17481c793a562e7dfaed80b25d7e70a6d55730d4ca7b418c840876fd0fb06b96933ef7c14cc1085e913a
7
- data.tar.gz: 4d655c241f521430d8e8ad91b2c61a48110617c1e96c5afe3036dbe2a3adecab51d5fc992aae8ff93fdca6c99ec2f047707826c32ad95ef5b2b4c2126480a478
6
+ metadata.gz: 2bff4e97af6b4e69b74d026a36e99802ac78262e3c61d1219ca49f019cdb741d5ae5f6821d16257bed0f57211b99e6e58776f60951201e46da46a51faa64d711
7
+ data.tar.gz: ac8025775c4e9c241ff2b441ab64829a0017bf259e87ddb8f601db7d4453a4b1274a01565269b73ac831cbd9290e09a75d9500804f835c7a3a69f51eadfdec55
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
11
+ .byebug_history
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 on GitHub at https://github.com/[USERNAME]/baidu_translate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+ Bug reports and pull requests are welcome!
41
43
 
42
44
 
43
45
  ## License
@@ -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
- 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
-
19
- salt = rand(32768..65536)
20
- api_params = Hash.new
21
-
22
- api_params[:q] = text
23
- api_params[:from] = from
24
- api_params[:to] = to
25
-
26
- api_params[:appid] = @app_id
27
-
28
- api_params[:salt] = salt
29
- api_params[:sign] = sign(text, salt)
30
-
31
- response = Faraday.get(API_URL, api_params)
32
- translation = JSON.parse(response.body)['trans_result'][0]['dst']
33
- return translation
34
- end
35
-
36
- def sign(text, salt)
37
- undigested_sign = @app_id.to_s + text + salt.to_s + @key
38
- md5 = Digest::MD5.new
39
- md5 << undigested_sign
40
- md5.hexdigest
41
- end
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
@@ -1,3 +1,3 @@
1
1
  module BaiduTranslate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -2,10 +2,10 @@ require "baidu_translate/version"
2
2
  require "baidu_translate/translation"
3
3
 
4
4
  module BaiduTranslate
5
-
5
+
6
6
  extend Translation
7
7
 
8
- class << self
9
- end
8
+ class << self
9
+ end
10
10
 
11
- end
11
+ end
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.0
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-20 00:00:00.000000000 Z
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