go_translator 0.1.0 → 0.2.0

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: e611efdc736085a61b4abbb7ba9044eaa6f6308b
4
- data.tar.gz: 7795528fdf379a5e4e770eec7ffa0a8c67c2b19e
3
+ metadata.gz: 36e1d231ab887f3ac461531ad42e6eecef310188
4
+ data.tar.gz: 3b2cc6275d2f8dfee07c1c68e97d323fc89fe110
5
5
  SHA512:
6
- metadata.gz: 4c5a76982e2dd5c7afb93dc1a78cde98d4fa062368bbcea98042b872c16d6afcb60eb5f3d7323683b8d9bf98c6ed40a2fb1b262cb3355ac12fd6ce7685382f5e
7
- data.tar.gz: aff425fd0565d28d9e256947d3289e061a9d981b24938520bcc95460bb7f130cfd7555a2f461caff7a237bfd7337131f20972017f1dc02507da20dc2cee06997
6
+ metadata.gz: 9007eeded26dfdbd96bcb64e3482f6a5610da5c09fd6fa0dc996b825d8cf913aa35987652dbcfa0cda5f75280b129eb0d216960a7d499889ec14add7498b6723
7
+ data.tar.gz: bd47de38fbbb62f7b0a72555ad7d68e012ff30f567bee4dc7e23a4c833e571dbfe5e118f3cb1201bcb92b9985b787e65202ce8df0a3980891ed833daa8cb27a9
data/README.md CHANGED
@@ -29,9 +29,25 @@ require 'go_translate'
29
29
  Example of using GoTranslate:
30
30
 
31
31
  ```ruby
32
- GoTranslate.translate('Hello World', from: 'en', to: 'ja') # => "こんにちは"
32
+ GoTranslate.translate('hello', from: 'en', to: 'ja') # => "こんにちは"
33
33
  ```
34
34
 
35
+ Example of translating from detect language to another language
36
+
37
+ ```ruby
38
+ GoTranslate.translate('hello', to: 'ja') # => "こんにちは"
39
+ ```
40
+
41
+ ## List of options
42
+
43
+ `:from` : Source language. Default: `:auto`
44
+
45
+ `:to` : Target language. Default: `:en`
46
+
47
+ `:in_encoding` : Source encoding. Default `'UTF-8'`
48
+
49
+ `:out_encoding` : Target encoding. Default `'UTF-8'`
50
+
35
51
  Please refer [list of supported languages](https://cloud.google.com/translate/v2/using_rest#language-params) for more options.
36
52
 
37
53
  ## Development
@@ -42,7 +58,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
42
58
 
43
59
  ## Contributing
44
60
 
45
- 1. Fork it ( https://github.com/[my-github-username]/go_translator/fork )
61
+ 1. Fork it ( https://github.com/hoangphanbk10/go_translator/fork )
46
62
  2. Create your feature branch (`git checkout -b my-new-feature`)
47
63
  3. Commit your changes (`git commit -am 'Add some feature'`)
48
64
  4. Push to the branch (`git push origin my-new-feature`)
@@ -0,0 +1,8 @@
1
+ module GoTranslator
2
+ GOOGLE_TRANSLATE_URL = 'https://translate.google.com/translate_a'
3
+ DEFAULT_FROM = 'auto'
4
+ DEFAULT_TO = 'en'
5
+ DEFAULT_IN_ENCODING = 'UTF-8'
6
+ DEFAULT_OUT_ENCODING = 'UTF-8'
7
+ SPLITTER = ','
8
+ end
@@ -1,3 +1,3 @@
1
1
  module GoTranslator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/go_translator.rb CHANGED
@@ -1,19 +1,24 @@
1
1
  require "go_translator/version"
2
+ require "go_translator/constants"
2
3
  require "net/http"
3
4
  require "uri"
4
5
 
5
6
  module GoTranslator
6
- GOOGLE_TRANSLATE_URL = 'https://translate.google.com/translate_a'
7
- DEFAULT_FROM = 'en'
8
- DEFAULT_TO = 'vi'
9
- SPLITTER = ','
10
-
11
7
  def self.translate(text, options = {})
12
- options = { from: DEFAULT_FROM, to: DEFAULT_TO }.merge(options)
13
- uri = URI.parse("#{GOOGLE_TRANSLATE_URL}/single?client=z&sl=#{options[:from]}&tl=#{options[:to]}&ie=UTF-8&oe=UTF-8&dt=t&dt=rm&q=#{text}")
8
+ options = default_options.merge(options)
9
+ uri = URI.parse("#{GOOGLE_TRANSLATE_URL}/single?client=z&sl=#{options[:from]}&tl=#{options[:to]}&ie=#{options[:in_encoding]}&oe=#{options[:out_encoding]}&dt=t&dt=rm&q=#{text}")
14
10
  response = Net::HTTP.get(uri)
15
11
  response.nil? || response.empty? ? '' : response.split(SPLITTER)[0].gsub(/[\[|\"]/, '')
16
12
  rescue
17
13
  ''
18
14
  end
15
+
16
+ def self.default_options
17
+ {
18
+ from: DEFAULT_FROM,
19
+ to: DEFAULT_TO,
20
+ in_encoding: DEFAULT_IN_ENCODING,
21
+ out_encoding: DEFAULT_OUT_ENCODING
22
+ }
23
+ end
19
24
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoTranslator do
4
+ it 'has a version number' do
5
+ expect(GoTranslator::VERSION).not_to be nil
6
+ end
7
+
8
+ describe '.translate' do
9
+ context 'valid response from server' do
10
+ it 'translates successfully' do
11
+ expect(Net::HTTP).to receive(:get).and_return("[[[\"\xE4\xBD\xA0\xE5\xA5\xBD\xE4\xB8\x96\xE7\x95\x8C\",\"hello world\",,,0],[,,\"N\xC7\x90 h\xC7\x8Eo sh\xC3\xACji\xC3\xA8\"]],,\"en\"]")
12
+ expect(GoTranslator.translate('random')).to eq "你好世界"
13
+ end
14
+ end
15
+
16
+ context 'no response from server' do
17
+ it 'returns empty string' do
18
+ expect(Net::HTTP).to receive(:get).and_return('')
19
+ expect(GoTranslator.translate('random')).to eq ''
20
+ end
21
+ end
22
+
23
+ context 'there is an exception from server' do
24
+ it 'returns empty string' do
25
+ expect(Net::HTTP).to receive(:get).and_raise
26
+ expect(GoTranslator.translate('random')).to eq ''
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '.default_options' do
32
+ let(:expected_result) do
33
+ {
34
+ from: GoTranslator::DEFAULT_FROM,
35
+ to: GoTranslator::DEFAULT_TO,
36
+ in_encoding: GoTranslator::DEFAULT_IN_ENCODING,
37
+ out_encoding: GoTranslator::DEFAULT_OUT_ENCODING
38
+ }
39
+ end
40
+
41
+ it 'has valid default_options' do
42
+ expect(GoTranslator.default_options).to eq expected_result
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'go_translator'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hoang Phan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-07 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,7 +59,10 @@ files:
59
59
  - bin/setup
60
60
  - go_translator.gemspec
61
61
  - lib/go_translator.rb
62
+ - lib/go_translator/constants.rb
62
63
  - lib/go_translator/version.rb
64
+ - spec/go_translator_spec.rb
65
+ - spec/spec_helper.rb
63
66
  homepage: http://rubygems.org/gems/go_translator
64
67
  licenses:
65
68
  - MIT
@@ -80,8 +83,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
83
  version: '0'
81
84
  requirements: []
82
85
  rubyforge_project:
83
- rubygems_version: 2.4.6
86
+ rubygems_version: 2.1.9
84
87
  signing_key:
85
88
  specification_version: 4
86
89
  summary: Free, easy google translator
87
- test_files: []
90
+ test_files:
91
+ - spec/go_translator_spec.rb
92
+ - spec/spec_helper.rb