google-translate-free 1.0.1 → 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: 4d07599110943c837b830702a549267348e5daafc7e6a37c4b0fd8bba7a2419b
4
- data.tar.gz: 8bd83c3cf57e6d2c34a9ece07939288d0005205c555513d6e43babe0286a5755
3
+ metadata.gz: 1d3fd60f4568dc83874d13438966ba1f023478c17fc66aac29ed2be8d49d53d6
4
+ data.tar.gz: 725f4b0cfbddd7f5f9b9e8f5053e24dc6db36e383f7d23b0f2132957ab25f727
5
5
  SHA512:
6
- metadata.gz: 872978126e335d6f2c4aab8fbe709ce069aff046fe4499d75d4a5ecbe6f28d832f0ae958c32f7448161d347ac009d912d39d66eaba63b8f5f16854ed8c58105f
7
- data.tar.gz: 8670acf54fd88cf533f933532cd8d635f0e6eaa783c32fb39a7a1208cc31acaf3cd801ada3179e679d14e1c798447f800777dfdd411cdda3dded0e47ac10bc9c
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:
@@ -1,126 +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
- VERSION = '1.0.1'.freeze
8
-
9
8
  def self.translate(string, to_lang, from_lang = :auto)
10
- validate_language_code(from_lang, to_lang)
11
- encode = encode_url(string)
12
- uri = URI("https://translate.googleapis.com/translate_a/t?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=t&q=#{encode}")
13
- result = net_get(uri)&.first
14
- from_lang.to_sym == :auto ? result&.first : result
9
+ translator = TranslateLib::Translator.new
10
+ translator.translate(string, to_lang, from_lang)
15
11
  end
16
12
 
17
13
  def self.alternate_translations(string, to_lang, from_lang = :auto)
18
- validate_language_code(from_lang, to_lang)
19
- uri = URI(url(:at, encode_url(string), from_lang, to_lang))
20
- result = net_get(uri).dig(5, 0, 2)
21
- return if result.nil?
22
-
23
- result.map { |arr| arr[0].downcase }.uniq
14
+ translator = TranslateLib::Translator.new
15
+ translator.alternate_translations(string, to_lang, from_lang)
24
16
  end
25
17
 
26
18
  def self.definitions(keyword)
27
- validate_keyword(keyword)
28
- # [ preposition: [desc ex sysnonyms] ]
29
- uri = URI(url(:md, keyword, :auto))
30
- result = net_get(uri)[12]
31
- return if result.nil?
32
-
33
- result.map do |preposition|
34
- pre = preposition[0]
35
- {
36
- "#{pre}": preposition[1].map do |arr|
37
- code = arr.last
38
- [arr[0], examples(keyword, code)&.first, synonyms(pre, keyword, code)]
39
- end
40
- }
41
- end
19
+ translator = TranslateLib::Translator.new
20
+ translator.definitions(keyword)
42
21
  end
43
22
 
44
23
  def self.synonyms(pre, keyword, code)
45
- uri = URI(url(:ss, keyword, :auto))
46
- result = net_get(uri)[11]
47
- return if result.nil?
48
-
49
- rs_response = ''
50
-
51
- result.each do |preposition|
52
- rs_response = preposition[1].map { |arr| arr[0] if arr[1] == code }.compact.join(', ') if pre == preposition[0]
53
- end
54
-
55
- rs_response == '' ? nil : rs_response
24
+ translator = TranslateLib::Translator.new
25
+ translator.synonyms(pre, keyword, code)
56
26
  end
57
27
 
58
28
  def self.examples(keyword, code = nil)
59
- validate_keyword(keyword)
60
- uri = URI(url(:ex, keyword, :auto))
61
- result = net_get(uri)[13]
62
- return unless result
63
-
64
- last_rs = if code.nil?
65
- result[0].map { |ex| ex[0] }
66
- else
67
- result[0].map { |ex| ex[0] if ex.last == code }.compact
68
- end
69
- last_rs.map do |ex|
70
- "#{ex}."
71
- end
29
+ translator = TranslateLib::Translator.new
30
+ translator.examples(keyword, code)
72
31
  end
73
32
 
74
33
  def self.transliteration(keyword)
75
- validate_keyword(keyword)
76
- uri = URI(url(:rm, keyword, :auto))
77
- net_get(uri)&.dig(0, 0, 3)
34
+ translator = TranslateLib::Translator.new
35
+ translator.transliteration(keyword)
78
36
  end
79
37
 
80
38
  def self.suggest(string)
81
- encode = encode_url(string)
82
- uri = URI(url(:qca, encode, :auto, detection(string).first == 'en' ? :vi : :en))
83
- net_get(uri)&.dig(7, 1) || string
39
+ translator = TranslateLib::Translator.new
40
+ translator.suggest(string)
84
41
  end
85
42
 
86
43
  def self.see_more(keyword)
87
- # ex [kids -> kid, tables -> table]
88
- validate_keyword(keyword)
89
- uri = URI(url(:rw, keyword, :auto))
90
- net_get(uri)&.dig(14, 0, 0) || keyword
44
+ translator = TranslateLib::Translator.new
45
+ translator.see_more(keyword)
91
46
  end
92
47
 
93
48
  def self.detection(string)
94
- uri = URI(url(:rm, encode_url(string), :auto))
95
- response = net_get(uri)
96
- [response&.dig(2), response&.dig(6)]
97
- end
98
-
99
- def self.url(dt_value, q_value, from_lang, to_lang = nil)
100
- "https://translate.googleapis.com/translate_a/single?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=#{dt_value}&q=#{q_value}"
101
- end
102
-
103
- def self.encode_url(string)
104
- TranslateLib::Validation.validate_string(string)
105
- CGI.escape(string)
106
- end
107
-
108
- def self.net_get(uri)
109
- http = Net::HTTP.new(uri.host, uri.port)
110
- http.use_ssl = true
111
- request = Net::HTTP::Get.new(uri.request_uri)
112
-
113
- response = http.request(request)
114
- JSON.parse(response.body)
115
- rescue StandardError => e
116
- raise StandardError, e.message
117
- end
118
-
119
- def self.validate_language_code(from_lang, to_lang)
120
- TranslateLib::Validation.validate_language_code(from_lang, to_lang)
121
- end
122
-
123
- def self.validate_keyword(keyword)
124
- TranslateLib::Validation.validate_keyword(keyword)
49
+ translator = TranslateLib::Translator.new
50
+ translator.detection(string)
125
51
  end
126
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.1
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-08-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: []