Skip to content

Commit 8550539

Browse files
committed
test: Add tests and docs for context parameter
1 parent ea18394 commit 8550539

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77

88
## [Unreleased]
9+
### Added
10+
* Added doc example and tests for context parameter
911
### Fixed
1012
* Fix metadata displayed on RubyGems.org for this library.
1113
* Fixed library version sent in the `User-Agent` string.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ puts translation.text
127127
# => "<p>Una muestra</p>"
128128
```
129129

130+
To translate with context, simply supply the `context` parameter:
131+
132+
```rb
133+
translation = DeepL.translate 'That is hot!', 'EN', 'ES',
134+
context: 'He did not like the jalapenos in his meal.'
135+
136+
puts translation.text
137+
# => "¡Eso es picante!"
138+
```
139+
130140
The following parameters will be automatically converted:
131141

132142
| Parameter | Conversion
@@ -139,6 +149,7 @@ The following parameters will be automatically converted:
139149
| `ignore_tags` | Converts arrays to strings joining by commas
140150
| `formality` | No conversion applied
141151
| `glossary_id` | No conversion applied
152+
| `context` | No conversion applied
142153

143154
### Glossaries
144155

spec/fixtures/vcr_cassettes/translate_texts.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10146,4 +10146,95 @@ http_interactions:
1014610146
encoding: UTF-8
1014710147
string: ''
1014810148
recorded_at: Tue, 09 Jul 2024 10:04:32 GMT
10149+
- request:
10150+
method: post
10151+
uri: https://api.deepl.com/v2/translate
10152+
body:
10153+
encoding: UTF-8
10154+
string: '{"text":["That is hot!"],"source_lang":"EN","target_lang":"ES","context":""}'
10155+
headers:
10156+
Authorization:
10157+
- DeepL-Auth-Key VALID_TOKEN
10158+
User-Agent:
10159+
- deepl-ruby/3.0.2 (darwin22) ruby/3.2.1
10160+
Content-Type:
10161+
- application/json
10162+
Accept-Encoding:
10163+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
10164+
Accept:
10165+
- "*/*"
10166+
response:
10167+
status:
10168+
code: 200
10169+
message: OK
10170+
headers:
10171+
Date:
10172+
- Wed, 25 Sep 2024 16:07:59 GMT
10173+
Content-Type:
10174+
- application/json
10175+
Transfer-Encoding:
10176+
- chunked
10177+
Vary:
10178+
- Accept-Encoding
10179+
Access-Control-Allow-Origin:
10180+
- "*"
10181+
Strict-Transport-Security:
10182+
- max-age=63072000; includeSubDomains; preload
10183+
Server-Timing:
10184+
- l7_lb_tls;dur=88, l7_lb_idle;dur=0, l7_lb_receive;dur=0, l7_lb_total;dur=126
10185+
Access-Control-Expose-Headers:
10186+
- Server-Timing, X-Trace-ID
10187+
X-Trace-Id:
10188+
- 53712edb2fc24c7f9d59e341bd80977a
10189+
body:
10190+
encoding: ASCII-8BIT
10191+
string: !binary |-
10192+
eyJ0cmFuc2xhdGlvbnMiOlt7ImRldGVjdGVkX3NvdXJjZV9sYW5ndWFnZSI6IkVOIiwidGV4dCI6IsKhRXNvIGVzdMOhIGNhbGllbnRlISJ9XX0=
10193+
recorded_at: Wed, 25 Sep 2024 16:07:59 GMT
10194+
- request:
10195+
method: post
10196+
uri: https://api.deepl.com/v2/translate
10197+
body:
10198+
encoding: UTF-8
10199+
string: '{"text":["That is hot!"],"source_lang":"EN","target_lang":"ES","context":"He
10200+
did not like the jalapenos in his meal."}'
10201+
headers:
10202+
Authorization:
10203+
- DeepL-Auth-Key VALID_TOKEN
10204+
User-Agent:
10205+
- deepl-ruby/3.0.2 (darwin22) ruby/3.2.1
10206+
Content-Type:
10207+
- application/json
10208+
Accept-Encoding:
10209+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
10210+
Accept:
10211+
- "*/*"
10212+
response:
10213+
status:
10214+
code: 200
10215+
message: OK
10216+
headers:
10217+
Date:
10218+
- Wed, 25 Sep 2024 16:08:00 GMT
10219+
Content-Type:
10220+
- application/json
10221+
Transfer-Encoding:
10222+
- chunked
10223+
Vary:
10224+
- Accept-Encoding
10225+
Access-Control-Allow-Origin:
10226+
- "*"
10227+
Strict-Transport-Security:
10228+
- max-age=63072000; includeSubDomains; preload
10229+
Server-Timing:
10230+
- l7_lb_tls;dur=67, l7_lb_idle;dur=0, l7_lb_receive;dur=0, l7_lb_total;dur=124
10231+
Access-Control-Expose-Headers:
10232+
- Server-Timing, X-Trace-ID
10233+
X-Trace-Id:
10234+
- 1704af9968bf4570b155c12cc0ae0665
10235+
body:
10236+
encoding: ASCII-8BIT
10237+
string: !binary |-
10238+
eyJ0cmFuc2xhdGlvbnMiOlt7ImRldGVjdGVkX3NvdXJjZV9sYW5ndWFnZSI6IkVOIiwidGV4dCI6IsKhRXNvIGVzIHBpY2FudGUhIn1dfQ==
10239+
recorded_at: Wed, 25 Sep 2024 16:08:00 GMT
1014910240
recorded_with: VCR 6.2.0

spec/requests/translate_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@
292292
end
293293
end
294294

295+
context 'when performing a valid request with context' do
296+
let(:text) { 'That is hot!' }
297+
298+
context 'when context is empty' do
299+
let(:options) { { context: '' } }
300+
301+
it 'translates correctly with empty context' do
302+
res = translate.request
303+
expect(res).to be_a(DeepL::Resources::Text)
304+
expect(res.text).to eq('¡Eso está caliente!')
305+
end
306+
end
307+
308+
context 'when context is set' do
309+
let(:options) { { context: 'He did not like the jalapenos in his meal.' } }
310+
311+
it 'translates correctly with context taken into account' do
312+
res = translate.request
313+
expect(res).to be_a(DeepL::Resources::Text)
314+
expect(res.text).to eq('¡Eso es picante!')
315+
end
316+
end
317+
end
318+
295319
context 'when performing a bad request' do
296320
context 'when using an invalid token' do
297321
let(:api) do

0 commit comments

Comments
 (0)