lingvanex-unofficial-rb 0.1.0
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 +7 -0
- data/CHANGELOG.md +15 -0
- data/LICENSE +21 -0
- data/README.md +311 -0
- data/Rakefile +11 -0
- data/lib/lingvanex/client.rb +125 -0
- data/lib/lingvanex/configuration.rb +18 -0
- data/lib/lingvanex/errors.rb +23 -0
- data/lib/lingvanex/version.rb +5 -0
- data/lib/lingvanex.rb +34 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 72b293cb5db2a5d48108b80e5b1ae121434d19185f9a7c4ab3bf2da08f87fafb
|
|
4
|
+
data.tar.gz: b05a3b136d71ff5cdf2f2f5e21c5f07ed14ec43dc701f82d7e38e6f7e03d7998
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 499c4042f6c89d7a02045f38a3e172326d3d038b5980f6f420ff169cd387d419c94acbbe734731398502a5f4101c7c435bfc4f9c9e0437935c17caf2956f0155
|
|
7
|
+
data.tar.gz: 12ee1c0c8c39fb216492ab26c3736650be5bb8a38027e0f5ab2dc4dfd207b6f0333f99f9cc4b81b30b5b7736238738948d3f0028b5106ee4710b1b3cccec2cd1
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2025-11-04
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Initial release of lingvanex-unofficial-rb gem
|
|
12
|
+
|
|
13
|
+
### Notes
|
|
14
|
+
- This is an unofficial gem, not affiliated with Lingvanex
|
|
15
|
+
- Module name remains `Lingvanex` for convenience
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
# Lingvanex (Unofficial Ruby Gem)
|
|
2
|
+
|
|
3
|
+
An unofficial Ruby gem for the [Lingvanex Translation API](https://lingvanex.com/products/translationapi/). Translate text between 109+ languages with automatic language detection, HTML translation, and transliteration support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'lingvanex-unofficial-rb'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
gem install lingvanex-unofficial-rb
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Getting Started
|
|
26
|
+
|
|
27
|
+
### Obtaining an API Key
|
|
28
|
+
|
|
29
|
+
1. Sign up for an account at [Lingvanex](https://lingvanex.com/)
|
|
30
|
+
2. Navigate to the Cloud API tab
|
|
31
|
+
3. Fill out the billing address data
|
|
32
|
+
4. Complete the payment process to generate your API key
|
|
33
|
+
|
|
34
|
+
### Configuration
|
|
35
|
+
|
|
36
|
+
You can configure Lingvanex globally:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
require 'lingvanex'
|
|
40
|
+
|
|
41
|
+
Lingvanex.configure do |config|
|
|
42
|
+
config.api_key = 'your_api_key_here'
|
|
43
|
+
config.timeout = 30 # optional, defaults to 30 seconds
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or configure per-client:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
client = Lingvanex::Client.new(api_key: 'your_api_key_here')
|
|
51
|
+
|
|
52
|
+
# Or with a block
|
|
53
|
+
client = Lingvanex::Client.new do |config|
|
|
54
|
+
config.api_key = 'your_api_key_here'
|
|
55
|
+
config.timeout = 60
|
|
56
|
+
end
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
### Translating Text
|
|
62
|
+
|
|
63
|
+
Basic translation with automatic language detection:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
client = Lingvanex.client(api_key: 'your_api_key')
|
|
67
|
+
|
|
68
|
+
result = client.translate('Hello, world!', to: 'es_ES')
|
|
69
|
+
puts result['result'] # => "¡Hola, mundo!"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Specify source language:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
result = client.translate('Hello, world!', from: 'en_GB', to: 'fr_FR')
|
|
76
|
+
puts result['result'] # => "Bonjour, le monde!"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Translate multiple texts at once:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
texts = ['Hello', 'Goodbye', 'Thank you']
|
|
83
|
+
result = client.translate(texts, to: 'de_DE')
|
|
84
|
+
# Returns translations for all texts
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### HTML Translation
|
|
88
|
+
|
|
89
|
+
Preserve HTML structure while translating:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
html = '<h1>Welcome</h1><p>This is a <strong>paragraph</strong>.</p>'
|
|
93
|
+
result = client.translate(html, to: 'it_IT', translate_mode: 'html')
|
|
94
|
+
# HTML tags remain intact, only text content is translated
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Transliteration
|
|
98
|
+
|
|
99
|
+
Enable transliteration in the response:
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
result = client.translate('Hello', to: 'uk_UA', enable_transliteration: true)
|
|
103
|
+
# Response includes transliteration fields
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Getting Available Languages
|
|
107
|
+
|
|
108
|
+
Fetch the list of supported languages:
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
result = client.get_languages
|
|
112
|
+
puts result['result']
|
|
113
|
+
# => [
|
|
114
|
+
# {"full_code"=>"en_GB", "name"=>"English"},
|
|
115
|
+
# {"full_code"=>"es_ES", "name"=>"Spanish"},
|
|
116
|
+
# ...
|
|
117
|
+
# ]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Get language names in a specific language:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
# Get language names in German
|
|
124
|
+
result = client.get_languages(code: 'de_DE')
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Language Codes
|
|
128
|
+
|
|
129
|
+
Language codes follow the format `language_COUNTRY`, for example:
|
|
130
|
+
|
|
131
|
+
- `en_GB` - English (UK)
|
|
132
|
+
- `en_US` - English (US)
|
|
133
|
+
- `es_ES` - Spanish (Spain)
|
|
134
|
+
- `fr_FR` - French (France)
|
|
135
|
+
- `de_DE` - German (Germany)
|
|
136
|
+
- `uk_UA` - Ukrainian (Ukraine)
|
|
137
|
+
- `zh_CN` - Chinese (Simplified)
|
|
138
|
+
- `ja_JP` - Japanese (Japan)
|
|
139
|
+
|
|
140
|
+
See the [Lingvanex documentation](https://docs.lingvanex.com/) for the complete list of supported languages.
|
|
141
|
+
|
|
142
|
+
## Error Handling
|
|
143
|
+
|
|
144
|
+
The gem provides specific error classes for different scenarios:
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
begin
|
|
148
|
+
client.translate('Hello', to: 'invalid_code')
|
|
149
|
+
rescue Lingvanex::AuthenticationError => e
|
|
150
|
+
# Handle authentication errors (401)
|
|
151
|
+
puts "Authentication failed: #{e.message}"
|
|
152
|
+
rescue Lingvanex::RateLimitError => e
|
|
153
|
+
# Handle rate limiting (429)
|
|
154
|
+
puts "Rate limit exceeded: #{e.message}"
|
|
155
|
+
rescue Lingvanex::InvalidRequestError => e
|
|
156
|
+
# Handle invalid requests (400-499)
|
|
157
|
+
puts "Invalid request: #{e.message}"
|
|
158
|
+
rescue Lingvanex::APIError => e
|
|
159
|
+
# Handle other API errors
|
|
160
|
+
puts "API error: #{e.message}"
|
|
161
|
+
puts "Status code: #{e.status_code}"
|
|
162
|
+
puts "Response body: #{e.response_body}"
|
|
163
|
+
end
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Error hierarchy:
|
|
167
|
+
|
|
168
|
+
- `Lingvanex::Error` - Base error class
|
|
169
|
+
- `Lingvanex::ConfigurationError` - Configuration errors
|
|
170
|
+
- `Lingvanex::APIError` - API-related errors
|
|
171
|
+
- `Lingvanex::AuthenticationError` - Authentication failures (401)
|
|
172
|
+
- `Lingvanex::RateLimitError` - Rate limit exceeded (429)
|
|
173
|
+
- `Lingvanex::InvalidRequestError` - Invalid request parameters (400-499)
|
|
174
|
+
|
|
175
|
+
## Advanced Configuration
|
|
176
|
+
|
|
177
|
+
### Custom Base URL
|
|
178
|
+
|
|
179
|
+
If you need to use a different API endpoint:
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
Lingvanex.configure do |config|
|
|
183
|
+
config.api_key = 'your_api_key'
|
|
184
|
+
config.base_url = 'https://custom.api.endpoint'
|
|
185
|
+
end
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Timeout Configuration
|
|
189
|
+
|
|
190
|
+
Set custom timeout for API requests:
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
client = Lingvanex::Client.new do |config|
|
|
194
|
+
config.api_key = 'your_api_key'
|
|
195
|
+
config.timeout = 60 # seconds
|
|
196
|
+
end
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Platform Configuration
|
|
200
|
+
|
|
201
|
+
The platform parameter is set to "api" by default, but can be customized:
|
|
202
|
+
|
|
203
|
+
```ruby
|
|
204
|
+
Lingvanex.configure do |config|
|
|
205
|
+
config.api_key = 'your_api_key'
|
|
206
|
+
config.platform = 'api' # default value
|
|
207
|
+
end
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Development
|
|
211
|
+
|
|
212
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
213
|
+
|
|
214
|
+
### Running Tests
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
bundle exec rspec
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Running RuboCop
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
bundle exec rubocop
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Running All Checks
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
bundle exec rake # runs both specs and rubocop
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mykbren/lingvanex-unofficial-rb.
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
239
|
+
|
|
240
|
+
## Resources
|
|
241
|
+
|
|
242
|
+
- [Lingvanex API Documentation](https://docs.lingvanex.com/)
|
|
243
|
+
- [Lingvanex Website](https://lingvanex.com/)
|
|
244
|
+
|
|
245
|
+
## Examples
|
|
246
|
+
|
|
247
|
+
### Simple Translation Script
|
|
248
|
+
|
|
249
|
+
```ruby
|
|
250
|
+
#!/usr/bin/env ruby
|
|
251
|
+
require 'lingvanex'
|
|
252
|
+
|
|
253
|
+
# Configure the gem
|
|
254
|
+
Lingvanex.configure do |config|
|
|
255
|
+
config.api_key = ENV['LINGVANEX_API_KEY']
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Create a client
|
|
259
|
+
client = Lingvanex.client
|
|
260
|
+
|
|
261
|
+
# Translate some text
|
|
262
|
+
puts "Translating 'Hello, world!' to Spanish..."
|
|
263
|
+
result = client.translate('Hello, world!', to: 'es_ES')
|
|
264
|
+
puts result['result']
|
|
265
|
+
|
|
266
|
+
# Get available languages
|
|
267
|
+
puts "\nFetching available languages..."
|
|
268
|
+
languages = client.get_languages
|
|
269
|
+
puts "Total languages available: #{languages['result'].length}"
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Rails Integration
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
# config/initializers/lingvanex.rb
|
|
276
|
+
Lingvanex.configure do |config|
|
|
277
|
+
config.api_key = Rails.application.credentials.lingvanex_api_key
|
|
278
|
+
config.timeout = 30
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# app/services/translation_service.rb
|
|
282
|
+
class TranslationService
|
|
283
|
+
def self.translate(text, target_language)
|
|
284
|
+
client = Lingvanex.client
|
|
285
|
+
result = client.translate(text, to: target_language)
|
|
286
|
+
result['result']
|
|
287
|
+
rescue Lingvanex::Error => e
|
|
288
|
+
Rails.logger.error("Translation failed: #{e.message}")
|
|
289
|
+
nil
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Batch Translation
|
|
295
|
+
|
|
296
|
+
```ruby
|
|
297
|
+
require 'lingvanex'
|
|
298
|
+
|
|
299
|
+
client = Lingvanex.client(api_key: 'your_api_key')
|
|
300
|
+
|
|
301
|
+
# Translate multiple phrases
|
|
302
|
+
phrases = [
|
|
303
|
+
'Good morning',
|
|
304
|
+
'Good afternoon',
|
|
305
|
+
'Good evening',
|
|
306
|
+
'Good night'
|
|
307
|
+
]
|
|
308
|
+
|
|
309
|
+
result = client.translate(phrases, to: 'ja_JP')
|
|
310
|
+
puts result
|
|
311
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
|
+
|
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
8
|
+
RuboCop::RakeTask.new
|
|
9
|
+
|
|
10
|
+
desc 'Run RSpec tests and RuboCop'
|
|
11
|
+
task default: %i[spec rubocop]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'openssl'
|
|
7
|
+
|
|
8
|
+
module Lingvanex
|
|
9
|
+
class Client
|
|
10
|
+
attr_reader :configuration
|
|
11
|
+
|
|
12
|
+
def initialize(api_key: nil)
|
|
13
|
+
@configuration = Configuration.new
|
|
14
|
+
@configuration.api_key = api_key if api_key
|
|
15
|
+
yield(@configuration) if block_given?
|
|
16
|
+
@configuration.validate!
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def translate(text, to:, from: nil, translate_mode: nil, enable_transliteration: false)
|
|
20
|
+
body = {
|
|
21
|
+
platform: configuration.platform,
|
|
22
|
+
to: to,
|
|
23
|
+
data: text
|
|
24
|
+
}
|
|
25
|
+
body[:from] = from if from
|
|
26
|
+
body[:translateMode] = translate_mode if translate_mode
|
|
27
|
+
body[:enableTransliteration] = enable_transliteration if enable_transliteration
|
|
28
|
+
|
|
29
|
+
post('/translate', body)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def get_languages(code: 'en_GB')
|
|
33
|
+
get('/getLanguages', platform: configuration.platform, code: code)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def get(path, **params)
|
|
39
|
+
uri = URI("#{configuration.base_url}#{path}")
|
|
40
|
+
uri.query = URI.encode_www_form(params) unless params.empty?
|
|
41
|
+
|
|
42
|
+
request = Net::HTTP::Get.new(uri)
|
|
43
|
+
request['Authorization'] = configuration.api_key
|
|
44
|
+
request['Content-Type'] = 'application/json'
|
|
45
|
+
|
|
46
|
+
execute_request(uri, request)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def post(path, body)
|
|
50
|
+
uri = URI("#{configuration.base_url}#{path}")
|
|
51
|
+
|
|
52
|
+
request = Net::HTTP::Post.new(uri)
|
|
53
|
+
request['Authorization'] = configuration.api_key
|
|
54
|
+
request['Content-Type'] = 'application/json'
|
|
55
|
+
request.body = body.to_json
|
|
56
|
+
|
|
57
|
+
execute_request(uri, request)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def execute_request(uri, request)
|
|
61
|
+
response = Net::HTTP.start(
|
|
62
|
+
uri.hostname,
|
|
63
|
+
uri.port,
|
|
64
|
+
use_ssl: true,
|
|
65
|
+
verify_mode: OpenSSL::SSL::VERIFY_PEER,
|
|
66
|
+
min_version: OpenSSL::SSL::TLS1_2_VERSION,
|
|
67
|
+
read_timeout: configuration.timeout
|
|
68
|
+
) do |http|
|
|
69
|
+
http.request(request)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
handle_response(response)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def handle_response(response)
|
|
76
|
+
status = response.code.to_i
|
|
77
|
+
return parse_success_response(response) if (200..299).cover?(status)
|
|
78
|
+
|
|
79
|
+
raise_error_for_status(status, response)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def parse_success_response(response)
|
|
83
|
+
JSON.parse(response.body)
|
|
84
|
+
rescue JSON::ParserError => e
|
|
85
|
+
raise_api_error("Invalid JSON response: #{e.message}", response)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def raise_error_for_status(status, response)
|
|
89
|
+
case status
|
|
90
|
+
when 401
|
|
91
|
+
raise_authentication_error(response)
|
|
92
|
+
when 429
|
|
93
|
+
raise_rate_limit_error(response)
|
|
94
|
+
when 400..499
|
|
95
|
+
raise_invalid_request_error(response)
|
|
96
|
+
else
|
|
97
|
+
raise_api_error("API error: #{response.body}", response)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def raise_authentication_error(response)
|
|
102
|
+
raise AuthenticationError.new('Authentication failed',
|
|
103
|
+
status_code: response.code.to_i,
|
|
104
|
+
response_body: response.body)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def raise_rate_limit_error(response)
|
|
108
|
+
raise RateLimitError.new('Rate limit exceeded',
|
|
109
|
+
status_code: response.code.to_i,
|
|
110
|
+
response_body: response.body)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def raise_invalid_request_error(response)
|
|
114
|
+
raise InvalidRequestError.new("Invalid request: #{response.body}",
|
|
115
|
+
status_code: response.code.to_i,
|
|
116
|
+
response_body: response.body)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def raise_api_error(message, response)
|
|
120
|
+
raise APIError.new(message,
|
|
121
|
+
status_code: response.code.to_i,
|
|
122
|
+
response_body: response.body)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lingvanex
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :api_key, :base_url, :timeout, :platform
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@api_key = nil
|
|
9
|
+
@base_url = 'https://api-b2b.backenster.com/b1/api/v3' # Official Lingvanex API endpoint
|
|
10
|
+
@timeout = 30
|
|
11
|
+
@platform = 'api'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validate!
|
|
15
|
+
raise ConfigurationError, 'API key is required' if api_key.nil? || api_key.empty?
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lingvanex
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
class ConfigurationError < Error; end
|
|
7
|
+
|
|
8
|
+
class APIError < Error
|
|
9
|
+
attr_reader :status_code, :response_body
|
|
10
|
+
|
|
11
|
+
def initialize(message, status_code: nil, response_body: nil)
|
|
12
|
+
super(message)
|
|
13
|
+
@status_code = status_code
|
|
14
|
+
@response_body = response_body
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class AuthenticationError < APIError; end
|
|
19
|
+
|
|
20
|
+
class RateLimitError < APIError; end
|
|
21
|
+
|
|
22
|
+
class InvalidRequestError < APIError; end
|
|
23
|
+
end
|
data/lib/lingvanex.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lingvanex/version'
|
|
4
|
+
require_relative 'lingvanex/errors'
|
|
5
|
+
require_relative 'lingvanex/configuration'
|
|
6
|
+
require_relative 'lingvanex/client'
|
|
7
|
+
|
|
8
|
+
module Lingvanex
|
|
9
|
+
class << self
|
|
10
|
+
attr_writer :configuration
|
|
11
|
+
|
|
12
|
+
def configure
|
|
13
|
+
yield(configuration)
|
|
14
|
+
configuration.validate!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def configuration
|
|
18
|
+
@configuration ||= Configuration.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def reset_configuration!
|
|
22
|
+
@configuration = Configuration.new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def client(api_key: nil, &block)
|
|
26
|
+
if api_key || block_given?
|
|
27
|
+
Client.new(api_key: api_key, &block)
|
|
28
|
+
else
|
|
29
|
+
configuration.validate!
|
|
30
|
+
Client.new(api_key: configuration.api_key)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lingvanex-unofficial-rb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mykbren
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-11-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: An unofficial, simple Ruby gem for interacting with the Lingvanex Translation
|
|
14
|
+
API. Supports text translation, language detection, and HTML translation with 109+
|
|
15
|
+
languages.
|
|
16
|
+
email:
|
|
17
|
+
- myk.bren@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- CHANGELOG.md
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- lib/lingvanex.rb
|
|
27
|
+
- lib/lingvanex/client.rb
|
|
28
|
+
- lib/lingvanex/configuration.rb
|
|
29
|
+
- lib/lingvanex/errors.rb
|
|
30
|
+
- lib/lingvanex/version.rb
|
|
31
|
+
homepage: https://github.com/mykbren/lingvanex-unofficial-rb
|
|
32
|
+
licenses:
|
|
33
|
+
- MIT
|
|
34
|
+
metadata:
|
|
35
|
+
homepage_uri: https://github.com/mykbren/lingvanex-unofficial-rb
|
|
36
|
+
source_code_uri: https://github.com/mykbren/lingvanex-unofficial-rb
|
|
37
|
+
changelog_uri: https://github.com/mykbren/lingvanex-unofficial-rb/blob/main/CHANGELOG.md
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.0.0
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.4.6
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Unofficial Ruby wrapper for the Lingvanex Translation API
|
|
58
|
+
test_files: []
|