ggtranslate 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
  SHA256:
3
- metadata.gz: c2153cc4eddd3350f19ab724bb6f0a0fd18dad1104cdb42d5aa5631b7e48cc19
4
- data.tar.gz: 370840434670315fa5f922a9ba26f08c5d6a52eca6f2a02c8fc50584ae4db165
3
+ metadata.gz: 7841a8a567623e55ce3fa8c63aa310cf9935177938a8f7eb6f3e9494c39271fb
4
+ data.tar.gz: a99f3ef5bae22626c51948991dbaf280e5ababdf580272a4b96d1dc76e7020f5
5
5
  SHA512:
6
- metadata.gz: a9caa19039a5ca6757d02c081e69799152b28ff70d07cbef90d9bb33184864d81a5c6e0f786a0e6b8cf3e5315017be3b29a473687e0f874f09c2414adbb5c685
7
- data.tar.gz: c8c1c4108aa63f7342e02daed1fc75dae4cdf4bdc9f327d942639c2e2ced5d70fdc510c5d55ec81c69f02b8ae5a73abf1e200b20ea8da38293ce3c3271292429
6
+ metadata.gz: b3b16b4f83e7c1d9cf92446cf9a548bc8e721796019585f0f48e2f8917b82d635902b6a895fb1cb5bc93d454648991be1a9d8a7de50ea4a3b01d79fde8080236
7
+ data.tar.gz: 04f263c723b409234a745b941f84dd910b5bd5d6b82405230229af7fed68907e0ce12514269da34e0771ba9dfdab79d16eb90bf67e1f04ba22ad21229eb9bb9e
data/Gemfile.lock CHANGED
@@ -1,11 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ggtranslate (0.1.0)
4
+ ggtranslate (0.1.1)
5
5
  addressable
6
- byebug (~> 11.1.3)
7
- charlock_holmes
8
- faraday
9
6
  rest-client (~> 2.1)
10
7
 
11
8
  GEM
@@ -14,14 +11,9 @@ GEM
14
11
  addressable (2.8.0)
15
12
  public_suffix (>= 2.0.2, < 5.0)
16
13
  byebug (11.1.3)
17
- charlock_holmes (0.7.7)
18
14
  diff-lcs (1.5.0)
19
15
  domain_name (0.5.20190701)
20
16
  unf (>= 0.0.5, < 1.0.0)
21
- faraday (2.1.0)
22
- faraday-net_http (~> 2.0)
23
- ruby2_keywords (>= 0.0.4)
24
- faraday-net_http (2.0.1)
25
17
  http-accept (1.7.0)
26
18
  http-cookie (1.0.4)
27
19
  domain_name (~> 0.5)
@@ -49,7 +41,6 @@ GEM
49
41
  diff-lcs (>= 1.2.0, < 2.0)
50
42
  rspec-support (~> 3.10.0)
51
43
  rspec-support (3.10.3)
52
- ruby2_keywords (0.0.5)
53
44
  unf (0.1.4)
54
45
  unf_ext
55
46
  unf_ext (0.0.8)
data/README.md CHANGED
@@ -24,7 +24,7 @@ Start with translate 1 simple sentence from English to Vietnamese
24
24
 
25
25
  ```ruby
26
26
  require 'ggtranslate'
27
- result = Ggtranslate::Translator.translate("hello", "en", "vi")
27
+ result = Ggtranslate.translate("hello", "en", "vi")
28
28
  result.translated_text
29
29
  # => "xin chào"
30
30
  ```
@@ -38,7 +38,7 @@ sentences = [
38
38
  %q{chào buổi tối},
39
39
  ]
40
40
 
41
- result = Ggtranslate::Translator.translate(sentences, "auto", "en")
41
+ result = Ggtranslate.translate(sentences, "auto", "en")
42
42
  result.translated_text
43
43
 
44
44
  # => ["hello everyone", "good morning", "good evening"]
@@ -46,6 +46,13 @@ result.translated_text
46
46
 
47
47
  you can use `auto` to let Google Translate auto detect source / dest language
48
48
 
49
+ To get all supported languages
50
+
51
+ ```ruby
52
+ Ggtranslate::LANGUAGES
53
+ ```
54
+
55
+
49
56
 
50
57
  # 📝 License
51
58
  The gem is available as open source under the terms of the MIT License.
data/ggtranslate.gemspec CHANGED
@@ -39,7 +39,6 @@ Gem::Specification.new do |spec|
39
39
  spec.add_development_dependency "rake", "~> 10.0"
40
40
  spec.add_development_dependency "rspec", "~> 3.2"
41
41
  spec.add_development_dependency "byebug", "~> 11.1.3"
42
- spec.add_dependency "byebug", "~> 11.1.3"
43
42
  spec.add_dependency "rest-client", "~> 2.1"
44
43
  spec.add_dependency "addressable"
45
44
  end
@@ -0,0 +1,7 @@
1
+ module Ggtranslate
2
+ class ApiCallException < StandardError
3
+ end
4
+
5
+ class ResponseParsingException < StandardError
6
+ end
7
+ end
@@ -0,0 +1,57 @@
1
+ module Ggtranslate
2
+ LANGUAGES = {
3
+ "af" => "afrikaans",
4
+ "sq" => "albanian",
5
+ "ar" => "arabic",
6
+ "be" => "belarusian",
7
+ "bg" => "bulgarian",
8
+ "ca" => "catalan",
9
+ "zh-CN" => "chinese_simplified",
10
+ "zh-TW" => "chinese_traditional",
11
+ "hr" => "croatian",
12
+ "cs" => "czech",
13
+ "da" => "danish",
14
+ "nl" => "dutch",
15
+ "en" => "english",
16
+ "et" => "estonian",
17
+ "tl" => "filipino",
18
+ "fi" => "finnish",
19
+ "fr" => "french",
20
+ "gl" => "galician",
21
+ "de" => "german",
22
+ "el" => "greek",
23
+ "iw" => "hebrew",
24
+ "hi" => "hindi",
25
+ "hu" => "hungarian",
26
+ "is" => "icelandic",
27
+ "id" => "indonesian",
28
+ "ga" => "irish",
29
+ "it" => "italian",
30
+ "ja" => "japanese",
31
+ "ko" => "korean",
32
+ "la" => "latin",
33
+ "lv" => "latvian",
34
+ "lt" => "lithuanian",
35
+ "mk" => "macedonian",
36
+ "ms" => "malay",
37
+ "mt" => "maltese",
38
+ "no" => "norwegian",
39
+ "fa" => "persian",
40
+ "pl" => "polish",
41
+ "pt" => "portuguese",
42
+ "ro" => "romanian",
43
+ "ru" => "russian",
44
+ "sr" => "serbian",
45
+ "sk" => "slovak",
46
+ "sl" => "slovenian",
47
+ "es" => "spanish",
48
+ "sw" => "swahili",
49
+ "sv" => "swedish",
50
+ "th" => "thai",
51
+ "tr" => "turkish",
52
+ "uk" => "ukrainian",
53
+ "vi" => "vietnamese",
54
+ "cy" => "welsh",
55
+ "yi" => "yiddish",
56
+ }
57
+ end
@@ -1,3 +1,5 @@
1
+ require_relative "./exceptions"
2
+
1
3
  module Ggtranslate
2
4
  class ResultObject
3
5
  def initialize(http_response)
@@ -11,12 +13,14 @@ module Ggtranslate
11
13
  result = @response_data.dig("sentences", 0, "trans")
12
14
  elsif @response_data.is_a? Array
13
15
  result = @response_data.first
14
- else
15
- raise "NO RESUL FOUND"
16
16
  end
17
17
 
18
+ raise "EMPTY RESULT" unless result
19
+
18
20
  result = result.split("----------")
19
21
  result.count == 1 ? result.first : result
22
+ rescue => e
23
+ raise Ggtranslate::ResponseParsingException.new("Fail to parse response body")
20
24
  end
21
25
 
22
26
  def source_language
@@ -47,7 +51,11 @@ module Ggtranslate
47
51
  private
48
52
 
49
53
  def process_http_response
50
- raise "TRANSLATE_FAILED" unless is_success?
54
+ unless is_success?
55
+ error_message = "Fail to call api with http code #{@http_response.code}"
56
+ raise Ggtranslate::ApiCallException.new(error_message)
57
+ end
58
+
51
59
  @response_data = JSON.load(@http_response.body)
52
60
  end
53
61
  end
@@ -4,8 +4,8 @@ require "addressable"
4
4
  require_relative "./result_object"
5
5
 
6
6
  module Ggtranslate
7
- class Translator
8
- def self.translate(text_query, source_language = "auto", to_language = "en")
7
+ module Translator
8
+ def translate(text_query, source_language = "auto", to_language = "en")
9
9
  endpoint = "https://clients5.google.com/translate_a/t?"
10
10
  # to translate multiple sentences
11
11
  text_query = text_query.join("----------") if text_query.is_a? Array
@@ -23,7 +23,7 @@ module Ggtranslate
23
23
  ResultObject.new(response)
24
24
  end
25
25
 
26
- def self.build_headers
26
+ def build_headers
27
27
  {
28
28
 
29
29
  accept: "*/*",
@@ -1,3 +1,3 @@
1
1
  module Ggtranslate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/ggtranslate.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  require "ggtranslate/version"
2
2
  require_relative "ggtranslate/translator"
3
3
  require_relative "ggtranslate/result_object"
4
+ require_relative "ggtranslate/languages"
4
5
 
5
6
  module Ggtranslate
6
- class Error < StandardError; end
7
+ autoload :ApiCallException, "ggtranslate/exceptions"
8
+ autoload :ResponseParsingException, "ggtranslate/exceptions"
9
+ autoload :LANGUAGES, "ggtranslate/languages"
10
+
11
+ extend Translator
12
+
13
+ class << self
14
+ end
7
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ggtranslate
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
  - Thien Tran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-24 00:00:00.000000000 Z
11
+ date: 2022-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 11.1.3
69
- - !ruby/object:Gem::Dependency
70
- name: byebug
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 11.1.3
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 11.1.3
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: rest-client
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -115,7 +101,6 @@ executables: []
115
101
  extensions: []
116
102
  extra_rdoc_files: []
117
103
  files:
118
- - ".byebug_history"
119
104
  - ".gitignore"
120
105
  - ".ruby-version"
121
106
  - CODE_OF_CONDUCT.md
@@ -128,6 +113,8 @@ files:
128
113
  - bin/setup
129
114
  - ggtranslate.gemspec
130
115
  - lib/ggtranslate.rb
116
+ - lib/ggtranslate/exceptions.rb
117
+ - lib/ggtranslate/languages.rb
131
118
  - lib/ggtranslate/result_object.rb
132
119
  - lib/ggtranslate/translator.rb
133
120
  - lib/ggtranslate/version.rb
data/.byebug_history DELETED
@@ -1,111 +0,0 @@
1
- c
2
- result.translated_text
3
- c
4
- build_headers
5
- RestClient.get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=en&q=xin%20ch%C3%A0o%20m%E1%BB%8Di%20ng%C6%B0%E1%BB%9Di", headers = build_headers)
6
- RestClient.get(endpoint + uri.query, headers = build_headers)
7
- endpoint + uri.query
8
- response
9
- c
10
- result.class
11
- result.translated_text
12
- result
13
- result.to_json
14
- c
15
- response = RestClient.get(endpoint + uri.query, headers = build_headers)
16
- response = RestClient.get(endpoint + uri.query_values, headers = build_headers)
17
- c
18
- require 'charlock_holmes/string'
19
- CharlockHolmes::EncodingDetector.detect("'«''ịeth World Cup 2011, Nhat Bản Ä'ang Ä'ứng tha»© 13 FIFA - 19 matches»›i Việt Nam ")
20
- result.translated_text
21
- c
22
- CharlockHolmes::EncodingDetector.detect(@http_response.body)
23
- require 'charlock_holmes'
24
- c
25
- CharDet.detect(@http_response.body)
26
- require "rchardet"
27
- CharDet.detect(@http_response.body)
28
- c
29
- @http_response.body.force_encoding("Windows-1252")
30
- @http_response.body.force_encoding("UTF-8")
31
- @http_response.body.force_encoding("Windows-1252")
32
- @http_response.body.force_encoding("Windows-1252").encode("UTF-8")
33
- c
34
- @http_response.body.encode("UTF-8", "Windows-1252")
35
- c
36
- @http_response.body
37
- @http_response.body.encode("UTF-8", "Windows-1252")
38
- @http_response.body.encode("ISO-8859-1")
39
- Encoding::toUTF8()
40
- @http_response.body.encoding
41
- @http_response.body
42
- @http_response.response
43
- @http_response
44
- result.translated_text
45
- "mỗi ngày tôi chọn 1 niềm vui"
46
- "mỗi"
47
- c
48
- result.translated_text
49
- result
50
- c
51
- @response_data = JSON.load(@http_response.body)
52
- c
53
- rr
54
- rr.encoding
55
- @http_response.methods
56
- @http_response.uri
57
- @http_response.url
58
- @http_response.response
59
- @http_response.class
60
- @http_response
61
- rr.encoding.force_encode("UTF-8")
62
- rr.encoding.force_encoding("UTF-8")
63
- rr.encoding
64
- rr = @response_data.dig("sentences", 0, "trans")
65
- @response_data.dig("sentences", 0, "trans")
66
- @http_response.headers
67
- @http_response
68
- @response_data = JSON.load(@http_response.body)
69
- c
70
- @http_response.uri
71
- @http_response.response
72
- @http_response.read_header
73
- @http_response.inspect
74
- @http_response.class
75
- @http_response.body
76
- @http_response.class
77
- @http_response.header
78
- @http_response.headers
79
- @http_response.header
80
- @http_response.header.as_json
81
- @http_response.header.to_json
82
- @http_response.header.to_s
83
- @http_response.header
84
- @http_response.head
85
- @http_response.body.force_encoding("UTF-8").encode("UTF-8")
86
- @http_response.body.force_encoding("UTF-8")
87
- c
88
- result
89
- c
90
- result
91
- c
92
- result
93
- c
94
- @http_response.body.force_encoding("UTF-8")
95
- @http_response.body.force_encoding("UTF-8").encode("UTF-8")
96
- @http_response.body.force_encoding("ISO-8859-1").encode("UTF-8")
97
- @http_response.body.encode("UTF-8")
98
- @http_response.body.encoding.encode("UTF-8")
99
- @http_response.body.encoding
100
- c
101
- result.translated_text.force_encoding("utf-8")
102
- result.translated_text.encode("iso-8859-1").force_encoding("utf-8")
103
- result.translated_text.class
104
- result.translated_text.to_utf8
105
- result.translated_text.encoding
106
- result.translated_text
107
- c
108
- result.translated_text
109
- result
110
- results
111
- c