google-translate-free 1.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cc72fe0c76488b83866129f10701348e32119ea42b3dfd42ec535682e1afbd9
4
- data.tar.gz: 75f4ff9e1e54ad9dbae00937d05d622b9ea080484c23fdc2acf9aaa2e83c5675
3
+ metadata.gz: 1d3fd60f4568dc83874d13438966ba1f023478c17fc66aac29ed2be8d49d53d6
4
+ data.tar.gz: 725f4b0cfbddd7f5f9b9e8f5053e24dc6db36e383f7d23b0f2132957ab25f727
5
5
  SHA512:
6
- metadata.gz: 041a4b9dae5ec62be5d6de6d1d667058f59a1aff9045024cbe0d09246245c806a6b7ba27cc41976eb675064550e1b484f7015f942763b06c02bcc65e1f9dad8b
7
- data.tar.gz: 65f41fd1f0b04f752cfd0c4986953f1ed679624ba697e264865e79990ede898c99c6db7476ed9ee0eda6a8863db1b1c2d5c7ef20ba699579e53df911f206101e
6
+ metadata.gz: 7b3778029f925f80a3a3f2e454e0f99cc7403e2ef572d4bfb339f844e512678d26222373e7272678f3615e83f5066e7f3e6e88823958af3cd4de478ef135d96e
7
+ data.tar.gz: 754166a4969111a33f373f04dea81f4e05e9e19c01aace59afefe6ba7a494d2cfb80a8f0ec54e09968ffcf3b699bfc3c30b39d3cc190413c8609a175b58e17f6
data/CHANGELOG.md CHANGED
@@ -1 +1,6 @@
1
1
  # Changelog
2
+
3
+ ### v1.0.2
4
+ * Refactored code to follow a more object-oriented approach.
5
+ * Created `BaseClient` class to handle HTTP requests.
6
+ * Updated `Translator` class to inherit from `BaseClient`.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Let's read the [documents](https://api.datpmt.com) before using it.
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'google-translate-free', '~> 1.0'
12
+ gem 'google-translate-free', '~> 1.0.1'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -31,6 +31,26 @@ Without third params, Google Translate will auto-detect your source language.
31
31
  Translate.translate('summer', :vi)
32
32
  # => "mùa hè"
33
33
  ```
34
+ For alternate Translations
35
+ ```ruby
36
+ Translate.alternate_translations('summer', :vi, :en)
37
+ # =>
38
+ #[
39
+ # "mùa hè",
40
+ # "mùa hạ",
41
+ # "hè"
42
+ #]
43
+ ```
44
+ Without third params, Google Translate will auto-detect your source language.
45
+ ```ruby
46
+ Translate.alternate_translations('summer', :vi)
47
+ # =>
48
+ #[
49
+ # "mùa hè",
50
+ # "mùa hạ",
51
+ # "hè"
52
+ #]
53
+ ```
34
54
  For definitions
35
55
  ```ruby
36
56
  Translate.definitions('summer')
@@ -1,124 +1,52 @@
1
1
  require 'cgi'
2
2
  require 'net/http'
3
3
  require 'json'
4
+ require_relative 'translate_lib/translator'
4
5
  require_relative 'translate_lib/validation'
5
6
 
6
7
  module Translate
7
8
  def self.translate(string, to_lang, from_lang = :auto)
8
- validate_language_code(from_lang, to_lang)
9
- encode = encode_url(string)
10
- uri = URI("https://translate.googleapis.com/translate_a/t?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=t&q=#{encode}")
11
- result = net_get(uri)&.first
12
- from_lang.to_sym == :auto ? result&.first : result
9
+ translator = TranslateLib::Translator.new
10
+ translator.translate(string, to_lang, from_lang)
13
11
  end
14
12
 
15
13
  def self.alternate_translations(string, to_lang, from_lang = :auto)
16
- validate_language_code(from_lang, to_lang)
17
- uri = URI(url(:at, encode_url(string), from_lang, to_lang))
18
- result = net_get(uri).dig(5, 0, 2)
19
- return if result.nil?
20
-
21
- result.map { |arr| arr[0].downcase }.uniq
14
+ translator = TranslateLib::Translator.new
15
+ translator.alternate_translations(string, to_lang, from_lang)
22
16
  end
23
17
 
24
18
  def self.definitions(keyword)
25
- validate_keyword(keyword)
26
- # [ preposition: [desc ex sysnonyms] ]
27
- uri = URI(url(:md, keyword, :auto))
28
- result = net_get(uri)[12]
29
- return if result.nil?
30
-
31
- result.map do |preposition|
32
- pre = preposition[0]
33
- {
34
- "#{pre}": preposition[1].map do |arr|
35
- code = arr.last
36
- [arr[0], examples(keyword, code)&.first, synonyms(pre, keyword, code)]
37
- end
38
- }
39
- end
19
+ translator = TranslateLib::Translator.new
20
+ translator.definitions(keyword)
40
21
  end
41
22
 
42
23
  def self.synonyms(pre, keyword, code)
43
- uri = URI(url(:ss, keyword, :auto))
44
- result = net_get(uri)[11]
45
- return if result.nil?
46
-
47
- rs_response = ''
48
-
49
- result.each do |preposition|
50
- rs_response = preposition[1].map { |arr| arr[0] if arr[1] == code }.compact.join(', ') if pre == preposition[0]
51
- end
52
-
53
- rs_response == '' ? nil : rs_response
24
+ translator = TranslateLib::Translator.new
25
+ translator.synonyms(pre, keyword, code)
54
26
  end
55
27
 
56
28
  def self.examples(keyword, code = nil)
57
- validate_keyword(keyword)
58
- uri = URI(url(:ex, keyword, :auto))
59
- result = net_get(uri)[13]
60
- return unless result
61
-
62
- last_rs = if code.nil?
63
- result[0].map { |ex| ex[0] }
64
- else
65
- result[0].map { |ex| ex[0] if ex.last == code }.compact
66
- end
67
- last_rs.map do |ex|
68
- "#{ex}."
69
- end
29
+ translator = TranslateLib::Translator.new
30
+ translator.examples(keyword, code)
70
31
  end
71
32
 
72
33
  def self.transliteration(keyword)
73
- validate_keyword(keyword)
74
- uri = URI(url(:rm, keyword, :auto))
75
- net_get(uri)&.dig(0, 0, 3)
34
+ translator = TranslateLib::Translator.new
35
+ translator.transliteration(keyword)
76
36
  end
77
37
 
78
38
  def self.suggest(string)
79
- encode = encode_url(string)
80
- uri = URI(url(:qca, encode, :auto))
81
- net_get(uri)&.dig(7, 1) || string
39
+ translator = TranslateLib::Translator.new
40
+ translator.suggest(string)
82
41
  end
83
42
 
84
43
  def self.see_more(keyword)
85
- # ex [kids -> kid, tables -> table]
86
- validate_keyword(keyword)
87
- uri = URI(url(:rw, keyword, :auto))
88
- net_get(uri)&.dig(14, 0, 0) || keyword
44
+ translator = TranslateLib::Translator.new
45
+ translator.see_more(keyword)
89
46
  end
90
47
 
91
48
  def self.detection(string)
92
- uri = URI("https://translate.google.com/translate_a/single?client=gtx&sl=auto&q=#{encode_url(string)}")
93
- response = net_get(uri)
94
- [response&.dig(2), response&.dig(6)]
95
- end
96
-
97
- def self.url(dt_value, q_value, from_lang, to_lang = nil)
98
- "https://translate.googleapis.com/translate_a/single?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=#{dt_value}&q=#{q_value}"
99
- end
100
-
101
- def self.encode_url(string)
102
- TranslateLib::Validation.validate_string(string)
103
- CGI.escape(string)
104
- end
105
-
106
- def self.net_get(uri)
107
- http = Net::HTTP.new(uri.host, uri.port)
108
- http.use_ssl = true
109
- request = Net::HTTP::Get.new(uri.request_uri)
110
-
111
- response = http.request(request)
112
- JSON.parse(response.body)
113
- rescue StandardError => e
114
- raise StandardError, e.message
115
- end
116
-
117
- def self.validate_language_code(from_lang, to_lang)
118
- TranslateLib::Validation.validate_language_code(from_lang, to_lang)
119
- end
120
-
121
- def self.validate_keyword(keyword)
122
- TranslateLib::Validation.validate_keyword(keyword)
49
+ translator = TranslateLib::Translator.new
50
+ translator.detection(string)
123
51
  end
124
52
  end
@@ -0,0 +1,26 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module TranslateLib
5
+ class BaseClient
6
+ CLIENT = 'gtx'.freeze
7
+ COMMON_TYPE = 'translate_a/single'.freeze
8
+ TRANSLATE_TYPE = 'translate_a/t'.freeze
9
+ BASE_URL = 'https://translate.googleapis.com'.freeze
10
+
11
+ def translate_uri(**args)
12
+ URI("#{BASE_URL}/#{args[:type]}?client=#{CLIENT}&sl=#{args[:sl]}&tl=#{args[:tl]}&dt=#{args[:dt]}&q=#{args[:q]}")
13
+ end
14
+
15
+ def get(uri)
16
+ http = Net::HTTP.new(uri.host, uri.port)
17
+ http.use_ssl = true
18
+ request = Net::HTTP::Get.new(uri.request_uri)
19
+
20
+ response = http.request(request)
21
+ JSON.parse(response.body)
22
+ rescue StandardError => e
23
+ raise StandardError, e.message
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,102 @@
1
+ require 'cgi'
2
+ require_relative 'base_client'
3
+ require_relative 'validation'
4
+
5
+ module TranslateLib
6
+ class Translator < BaseClient
7
+ def translate(string, to_lang, from_lang = :auto)
8
+ Validation.validate_language_code(from_lang, to_lang)
9
+ encode = encode_url(string)
10
+ uri = translate_uri(type: TRANSLATE_TYPE, sl: from_lang, tl: to_lang, dt: :t, q: encode)
11
+ result = get(uri)&.first
12
+ from_lang.to_sym == :auto ? result&.first : result
13
+ end
14
+
15
+ def alternate_translations(string, to_lang, from_lang = :auto)
16
+ Validation.validate_language_code(from_lang, to_lang)
17
+ uri = translate_uri(type: COMMON_TYPE, sl: from_lang, tl: to_lang, dt: :at, q: encode_url(string))
18
+ result = get(uri).dig(5, 0, 2)
19
+ return if result.nil?
20
+
21
+ result.map { |arr| arr[0].downcase }.uniq
22
+ end
23
+
24
+ def definitions(keyword)
25
+ Validation.validate_keyword(keyword)
26
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :md, q: keyword)
27
+ result = get(uri)[12]
28
+ return if result.nil?
29
+
30
+ result.map do |preposition|
31
+ pre = preposition[0]
32
+ {
33
+ "#{pre}": preposition[1].map do |arr|
34
+ code = arr.last
35
+ [arr[0], examples(keyword, code)&.first, synonyms(pre, keyword, code)]
36
+ end
37
+ }
38
+ end
39
+ end
40
+
41
+ def synonyms(pre, keyword, code)
42
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :ss, q: keyword)
43
+ result = get(uri)[11]
44
+ return if result.nil?
45
+
46
+ rs_response = ''
47
+
48
+ result.each do |preposition|
49
+ rs_response = preposition[1].map { |arr| arr[0] if arr[1] == code }.compact.join(', ') if pre == preposition[0]
50
+ end
51
+
52
+ rs_response == '' ? nil : rs_response
53
+ end
54
+
55
+ def examples(keyword, code = nil)
56
+ Validation.validate_keyword(keyword)
57
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :ex, q: keyword)
58
+ result = get(uri)[13]
59
+ return unless result
60
+
61
+ last_rs = if code.nil?
62
+ result[0].map { |ex| ex[0] }
63
+ else
64
+ result[0].map { |ex| ex[0] if ex.last == code }.compact
65
+ end
66
+ last_rs.map do |ex|
67
+ "#{ex}."
68
+ end
69
+ end
70
+
71
+ def transliteration(keyword)
72
+ Validation.validate_keyword(keyword)
73
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :rm, q: keyword)
74
+ get(uri)&.dig(0, 0, 3)
75
+ end
76
+
77
+ def suggest(string)
78
+ encode = encode_url(string)
79
+ uri = translate_uri(type: COMMON_TYPE, sl: detection(string).first, tl: detection(string).first == 'en' ? :vi : :en, dt: :qca, q: encode)
80
+ get(uri)&.dig(7, 1) || string
81
+ end
82
+
83
+ def see_more(keyword)
84
+ Validation.validate_keyword(keyword)
85
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :rw, q: keyword)
86
+ get(uri)&.dig(14, 0, 0) || keyword
87
+ end
88
+
89
+ def detection(string)
90
+ uri = translate_uri(type: COMMON_TYPE, sl: :auto, tl: :auto, dt: :rm, q: encode_url(string))
91
+ response = get(uri)
92
+ [response&.dig(2), response&.dig(6)]
93
+ end
94
+
95
+ private
96
+
97
+ def encode_url(string)
98
+ Validation.validate_string(string)
99
+ CGI.escape(string)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Translate
4
+ VERSION = '1.0.2'.freeze
5
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-translate-free
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - datpmt
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-05-27 00:00:00.000000000 Z
10
+ date: 2025-01-09 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Translation service designed to help you with a variety of language-based
14
13
  features including direct translations, alternate translations, definitions, examples,
@@ -23,15 +22,17 @@ files:
23
22
  - LICENSE
24
23
  - README.md
25
24
  - lib/google-translate-free.rb
25
+ - lib/translate_lib/base_client.rb
26
26
  - lib/translate_lib/exception.rb
27
+ - lib/translate_lib/translator.rb
27
28
  - lib/translate_lib/validation.rb
29
+ - lib/translate_lib/version.rb
28
30
  homepage: https://rubygems.org/gems/google-translate-free
29
31
  licenses:
30
32
  - MIT
31
33
  metadata:
32
34
  source_code_uri: https://github.com/datpmt/google-translate-free
33
35
  changelog_uri: https://github.com/datpmt/google-translate-free/blob/main/CHANGELOG.md
34
- post_install_message:
35
36
  rdoc_options: []
36
37
  require_paths:
37
38
  - lib
@@ -46,8 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
- rubygems_version: 3.2.3
50
- signing_key:
50
+ rubygems_version: 3.6.2
51
51
  specification_version: 4
52
52
  summary: Supports APIs related to Google Translate and more!
53
53
  test_files: []