deepl-rb 1.0.1 → 2.2.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
  SHA1:
3
- metadata.gz: 37163389ddd5655c4c9f1d1e60e0d414e3c35420
4
- data.tar.gz: 881b3877774bc62cf3f42900c72d336c7b32583d
3
+ metadata.gz: 45fd2b316644d0fb624c9bcaadea7524c08933d1
4
+ data.tar.gz: d79aa178b64a3a302e6fd4e82196fdca1a8bb85a
5
5
  SHA512:
6
- metadata.gz: d63335bb2ec23d724e77dba606844c711afbc798afe8c7a64f12a420e14561c33c555f36716f7d3537f8f234bed064d8d72aa659837a99b442952186813718cb
7
- data.tar.gz: 944992d82a26e096f4389cbc50dd076c80b7d6c58113c84401539e20b8620dcac21ac362730fb3a3b862d97658b70e565911f69fc3c13e2b1f6b26580cb697b7
6
+ metadata.gz: 6d28b5d519aa43971e13af3675b67a58825f30674f75af82491dd2f2e7f59727fd95d5860ba4d9638fe88ff286a316420695d75dba65601e4e1f29ab78e79e0f
7
+ data.tar.gz: b33fd1cf048c8ace82ea10d5a15007a2e97b57ec677760da1ba22207655aa190867f81e8c658550963f1d6b0efb03b20d0053b5dd3c95d4f3ea8d6629d1e2d6f
@@ -11,7 +11,7 @@ Metrics/BlockLength:
11
11
  - "**/*_spec.rb"
12
12
 
13
13
  Metrics/LineLength:
14
- Max: 120
14
+ Max: 100
15
15
 
16
16
  Style/Documentation:
17
17
  Enabled: false
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- [![Gem Version](https://badge.fury.io/rb/deepl-rb.svg)](https://badge.fury.io/rb/deepl-rb) [![Dependency Status](https://gemnasium.com/badges/github.com/wikiti/deepl-rb.svg)](https://gemnasium.com/github.com/wikiti/deepl-rb) [![CircleCI](https://circleci.com/gh/wikiti/deepl-rb.svg?style=shield)](https://circleci.com/gh/wikiti/deepl-rb) [![CodeCov](https://codecov.io/gh/wikiti/deepl-rb/branch/master/graph/badge.svg?token=SHLgQNlZ4o)](https://codecov.io/gh/wikiti/deepl-rb)
1
+ [![Gem Version](https://badge.fury.io/rb/deepl-rb.svg)](https://badge.fury.io/rb/deepl-rb) [![CircleCI](https://circleci.com/gh/wikiti/deepl-rb.svg?style=shield)](https://circleci.com/gh/wikiti/deepl-rb) [![CodeCov](https://codecov.io/gh/wikiti/deepl-rb/branch/master/graph/badge.svg?token=SHLgQNlZ4o)](https://codecov.io/gh/wikiti/deepl-rb)
2
2
 
3
3
  # DeepL for ruby
4
4
 
5
- A simple ruby wrapper for the [DeepL translation API (v1)](https://www.deepl.com/docs/api-reference.html).
5
+ A simple ruby wrapper for the [DeepL translation API (v2)](https://www.deepl.com/api.html).
6
6
 
7
7
  ## Installation
8
8
 
@@ -35,12 +35,13 @@ DeepL.configure do |config|
35
35
  end
36
36
  ```
37
37
 
38
- You can also configure the api host:
38
+ You can also configure the API host and the API version:
39
39
 
40
40
  ```rb
41
41
  DeepL.configure do |config|
42
42
  config.auth_key = 'your-api-token'
43
43
  config.host = 'https://test-api.deepl.com' # Default value is 'https://api.deepl.com'
44
+ config.version = 'v1' # Default value is 'v2'
44
45
  end
45
46
  ```
46
47
 
@@ -86,28 +87,55 @@ Here's a list of available language codes:
86
87
  | `DE` | German
87
88
  | `FR` | French
88
89
  | `ES` | Spanish
90
+ | `PT` | Portuguese
89
91
  | `IT` | Italian
90
92
  | `NL` | Dutch
91
93
  | `PL` | Polish
94
+ | `RU` | Russian
92
95
 
93
- You can also use custom query parameters, like `tag_handling`:
96
+ You can also use custom query parameters, like `tag_handling`, `split_sentences`, `non_splitting_tags` or `ignore_tags`:
94
97
 
95
98
  ```rb
96
- translation = DeepL.translate '<p>A sample</p>', 'EN', 'ES', tag_handling: true
99
+ translation = DeepL.translate '<p>A sample</p>', 'EN', 'ES',
100
+ tag_handling: 'xml', split_sentences: false,
101
+ non_splitting_tags: 'h1', ignore_tags: %w[code pre]
97
102
 
98
103
  puts translation.text
99
104
  # => "<p>Una muestra</p>"
100
105
  ```
101
106
 
107
+ The following parameters will be automatically converted:
108
+
109
+ | Parameter | Conversion
110
+ | --------------------- | ---------------
111
+ | `preserve_formatting` | Converts `false` to `'0'` and `true` to `'1'`
112
+ | `split_sentences` | Converts `false` to `'0'` and `true` to `'1'`
113
+ | `non_splitting_tags` | Converts arrays to strings joining by commas
114
+ | `ignore_tags` | Converts arrays to strings joining by commas
115
+
116
+ ### Usage
117
+
118
+ To check current API usage, use:
119
+
120
+ ```rb
121
+ usage = DeepL.usage
122
+
123
+ puts usage.character_count
124
+ # => 180118
125
+ puts usage.character_limit
126
+ # => 1250000
127
+ ```
128
+
102
129
  ### Handle exceptions
103
130
 
104
131
  You can capture and process exceptions that may be raised during API calls. These are all the possible exceptions:
105
132
 
106
- | Exception class | Descripcion |
133
+ | Exception class | Description |
107
134
  | --------------- | ----------- |
108
- | `DeepL::Exceptions::AuthorizationFailed` | The authorization process has failed. Check your auth_key value. |
135
+ | `DeepL::Exceptions::AuthorizationFailed` | The authorization process has failed. Check your `auth_key` value. |
109
136
  | `DeepL::Exceptions::BadRequest` | Something is wrong in your request. Check `exception.message` for more information. |
110
137
  | `DeepL::Exceptions::LimitExceeded` | You've reached the API's call limit. |
138
+ | `DeepL::Exceptions::QuotaExceeded` | You've reached the API's character limit. |
111
139
  | `DeepL::Exceptions::RequestError` | An unkown request error. Check `exception.response` and `exception.request` for more information. |
112
140
 
113
141
  An exampling of handling a generic exception:
@@ -121,9 +149,31 @@ rescue DeepL::Exceptions::RequestError => e
121
149
  puts "Response body: #{e.response.body}"
122
150
  puts "Request body: #{e.request.body}"
123
151
  end
152
+ ```
124
153
 
154
+ ## Integrations
155
+
156
+ ### Ruby on Rails
157
+
158
+ You may use this gem as a standalone service by creating an initializer on your
159
+ `config/initializers` folder with your DeepL configuration. For example:
160
+
161
+ ```rb
162
+ # config/initializers/deepl.rb
163
+ DeepL.configure do |config|
164
+ # Your configuration goes here
165
+ end
125
166
  ```
126
167
 
168
+ Since the DeepL service is defined globally, you can use service anywhere in your code
169
+ (controllers, models, views, jobs, plain ruby objects… you name it).
170
+
171
+ ### i18n-tasks
172
+
173
+ You may also take a look at [`i18n-tasks`](https://github.com/glebm/i18n-tasks), which is a gem
174
+ that helps you find and manage missing and unused translations. `deepl-rb` is used as one the
175
+ backend services to translate content.
176
+
127
177
  ## Development
128
178
 
129
179
  Clone the repository, and install its dependencies:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 2.2.2
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: deepl-rb 1.0.1 ruby lib
5
+ # stub: deepl-rb 2.2.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "deepl-rb".freeze
9
- s.version = "1.0.1"
9
+ s.version = "2.2.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Daniel Herzog".freeze]
14
- s.date = "2018-03-31"
14
+ s.date = "2020-06-18"
15
15
  s.description = "A simple ruby wrapper for the DeepL translation API (v1). For more information, check this: https://www.deepl.com/docs/api-reference.html".freeze
16
16
  s.email = "info@danielherzog.es".freeze
17
17
  s.extra_rdoc_files = [
@@ -35,23 +35,30 @@ Gem::Specification.new do |s|
35
35
  "lib/deepl/exceptions/bad_request.rb",
36
36
  "lib/deepl/exceptions/error.rb",
37
37
  "lib/deepl/exceptions/limit_exceeded.rb",
38
+ "lib/deepl/exceptions/quota_exceeded.rb",
38
39
  "lib/deepl/exceptions/request_error.rb",
39
40
  "lib/deepl/requests/base.rb",
40
- "lib/deepl/requests/translate_text.rb",
41
+ "lib/deepl/requests/translate.rb",
42
+ "lib/deepl/requests/usage.rb",
41
43
  "lib/deepl/resources/base.rb",
42
44
  "lib/deepl/resources/text.rb",
45
+ "lib/deepl/resources/usage.rb",
43
46
  "spec/api/api_spec.rb",
44
47
  "spec/api/configuration_spec.rb",
45
48
  "spec/api/deepl_spec.rb",
46
49
  "spec/fixtures/vcr_cassettes/deepl_translate.yml",
50
+ "spec/fixtures/vcr_cassettes/deepl_usage.yml",
47
51
  "spec/fixtures/vcr_cassettes/translate_texts.yml",
48
- "spec/requests/translate_text_spec.rb",
52
+ "spec/fixtures/vcr_cassettes/usage.yml",
53
+ "spec/requests/translate_spec.rb",
54
+ "spec/requests/usage_spec.rb",
49
55
  "spec/resources/text_spec.rb",
56
+ "spec/resources/usage_spec.rb",
50
57
  "spec/spec_helper.rb"
51
58
  ]
52
59
  s.homepage = "http://github.com/wikiti/deepl-rb".freeze
53
60
  s.licenses = ["MIT".freeze]
54
- s.rubygems_version = "2.6.13".freeze
61
+ s.rubygems_version = "2.6.14".freeze
55
62
  s.summary = "A simple ruby wrapper for the DeepL API".freeze
56
63
 
57
64
  if s.respond_to? :specification_version then
@@ -8,14 +8,17 @@ require 'deepl/exceptions/request_error'
8
8
  require 'deepl/exceptions/authorization_failed'
9
9
  require 'deepl/exceptions/bad_request'
10
10
  require 'deepl/exceptions/limit_exceeded'
11
+ require 'deepl/exceptions/quota_exceeded'
11
12
 
12
13
  # -- Requests
13
14
  require 'deepl/requests/base'
14
- require 'deepl/requests/translate_text'
15
+ require 'deepl/requests/translate'
16
+ require 'deepl/requests/usage'
15
17
 
16
18
  # -- Responses and resources
17
19
  require 'deepl/resources/base'
18
20
  require 'deepl/resources/text'
21
+ require 'deepl/resources/usage'
19
22
 
20
23
  # -- Other wrappers
21
24
  require 'deepl/api'
@@ -33,7 +36,12 @@ module DeepL
33
36
 
34
37
  def translate(text, source_lang, target_lang, options = {})
35
38
  configure if @configuration.nil?
36
- Requests::TranslateText.new(api, text, source_lang, target_lang, options).request
39
+ Requests::Translate.new(api, text, source_lang, target_lang, options).request
40
+ end
41
+
42
+ def usage(options = {})
43
+ configure if @configuration.nil?
44
+ Requests::Usage.new(api, options).request
37
45
  end
38
46
 
39
47
  # -- Configuration
@@ -1,6 +1,6 @@
1
1
  module DeepL
2
2
  class Configuration
3
- ATTRIBUTES = %i[auth_key host].freeze
3
+ ATTRIBUTES = %i[auth_key host version].freeze
4
4
 
5
5
  attr_accessor(*ATTRIBUTES)
6
6
 
@@ -8,6 +8,7 @@ module DeepL
8
8
  data.each { |key, value| send("#{key}=", value) }
9
9
  @auth_key ||= ENV['DEEPL_AUTH_KEY']
10
10
  @host ||= 'https://api.deepl.com'
11
+ @version ||= 'v2'
11
12
  end
12
13
 
13
14
  def validate!
@@ -0,0 +1,9 @@
1
+ module DeepL
2
+ module Exceptions
3
+ class QuotaExceeded < RequestError
4
+ def message
5
+ 'Quota exceeded. The character limit has been reached.'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -9,7 +9,7 @@ module DeepL
9
9
  end
10
10
 
11
11
  def message
12
- 'Unkown error.'
12
+ 'Unknown error.'
13
13
  end
14
14
  end
15
15
  end
@@ -1,8 +1,6 @@
1
1
  module DeepL
2
2
  module Requests
3
3
  class Base
4
- API_VERSION = 'v1'.freeze
5
-
6
4
  attr_reader :api, :response, :options
7
5
 
8
6
  def initialize(api, options = {})
@@ -16,10 +14,22 @@ module DeepL
16
14
 
17
15
  private
18
16
 
17
+ def option?(name)
18
+ options.key?(name.to_s) || options.key?(name.to_sym)
19
+ end
20
+
19
21
  def option(name)
20
22
  options[name.to_s] || options[name.to_sym]
21
23
  end
22
24
 
25
+ def set_option(name, value)
26
+ if options.key?(name.to_sym)
27
+ options[name.to_sym] = value
28
+ else
29
+ options[name.to_s] = value
30
+ end
31
+ end
32
+
23
33
  def post(payload)
24
34
  request = Net::HTTP::Post.new(uri.request_uri)
25
35
  request.set_form_data(payload.reject { |_, v| v.nil? })
@@ -29,6 +39,14 @@ module DeepL
29
39
  [request, response]
30
40
  end
31
41
 
42
+ def get
43
+ request = Net::HTTP::Get.new(uri.request_uri)
44
+ response = http.request(request)
45
+
46
+ validate_response!(request, response)
47
+ [request, response]
48
+ end
49
+
32
50
  def http
33
51
  @http ||= begin
34
52
  http = Net::HTTP.new(uri.host, uri.port)
@@ -44,6 +62,7 @@ module DeepL
44
62
  when '400' then raise Exceptions::BadRequest.new(request, response)
45
63
  when '403' then raise Exceptions::AuthorizationFailed.new(request, response)
46
64
  when '429' then raise Exceptions::LimitExceeded.new(request, response)
65
+ when '456' then raise Exceptions::QuotaExceeded.new(request, response)
47
66
  else raise Exceptions::RequestError.new(request, response)
48
67
  end
49
68
  end
@@ -53,7 +72,7 @@ module DeepL
53
72
  end
54
73
 
55
74
  def url
56
- "#{host}/#{API_VERSION}/#{path}"
75
+ "#{host}/#{api.configuration.version}/#{path}"
57
76
  end
58
77
 
59
78
  def uri
@@ -1,13 +1,24 @@
1
1
  module DeepL
2
2
  module Requests
3
- class TranslateText < Base
4
- attr_reader :text, :source_lang, :target_lang
3
+ class Translate < Base
4
+ BOOLEAN_CONVERSION = { true => '1', false => '0' }.freeze
5
+ ARRAY_CONVERSION = ->(value) { value.is_a?(Array) ? value.join(', ') : value }.freeze
6
+ OPTIONS_CONVERSIONS = {
7
+ split_sentences: BOOLEAN_CONVERSION,
8
+ preserve_formatting: BOOLEAN_CONVERSION,
9
+ non_splitting_tags: ARRAY_CONVERSION,
10
+ ignore_tags: ARRAY_CONVERSION
11
+ }.freeze
12
+
13
+ attr_reader :text, :source_lang, :target_lang, :ignore_tags, :non_splitting_tags
5
14
 
6
15
  def initialize(api, text, source_lang, target_lang, options = {})
7
16
  super(api, options)
8
17
  @text = text
9
18
  @source_lang = source_lang
10
19
  @target_lang = target_lang
20
+
21
+ tweak_parameters!
11
22
  end
12
23
 
13
24
  def request
@@ -17,6 +28,13 @@ module DeepL
17
28
 
18
29
  private
19
30
 
31
+ def tweak_parameters!
32
+ OPTIONS_CONVERSIONS.each do |param, converter|
33
+ next unless option?(param) && converter[option(param)]
34
+ set_option(param, converter[option(param)])
35
+ end
36
+ end
37
+
20
38
  def build_texts(request, response)
21
39
  data = JSON.parse(response.body)
22
40
 
@@ -0,0 +1,24 @@
1
+ module DeepL
2
+ module Requests
3
+ class Usage < Base
4
+ def initialize(api, options = {})
5
+ super(api, options)
6
+ end
7
+
8
+ def request
9
+ build_usage(*get)
10
+ end
11
+
12
+ private
13
+
14
+ def build_usage(request, response)
15
+ data = JSON.parse(response.body)
16
+ Resources::Usage.new(data['character_count'], data['character_limit'], request, response)
17
+ end
18
+
19
+ def path
20
+ 'usage'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module DeepL
2
+ module Resources
3
+ class Usage < Base
4
+ attr_reader :character_count, :character_limit
5
+
6
+ def initialize(character_count, character_limit, *args)
7
+ super(*args)
8
+
9
+ @character_count = character_count
10
+ @character_limit = character_limit
11
+ end
12
+
13
+ def to_s
14
+ "#{character_count} / #{character_limit}"
15
+ end
16
+
17
+ def quota_exceeded?
18
+ character_count >= character_limit
19
+ end
20
+ end
21
+ end
22
+ end
@@ -9,15 +9,17 @@ describe DeepL::Configuration do
9
9
  it 'should use default attributes' do
10
10
  expect(subject.auth_key).to eq(ENV['DEEPL_AUTH_KEY'])
11
11
  expect(subject.host).to eq('https://api.deepl.com')
12
+ expect(subject.version).to eq('v2')
12
13
  end
13
14
  end
14
15
 
15
16
  context 'When using custom configuration attributes' do
16
- let(:attributes) { { auth_key: 'SAMPLE', host: 'http://www.example.org' } }
17
+ let(:attributes) { { auth_key: 'SAMPLE', host: 'http://www.example.org', version: 'v1' } }
17
18
 
18
19
  it 'should use custom attributes' do
19
20
  expect(subject.auth_key).to eq(attributes[:auth_key])
20
21
  expect(subject.host).to eq(attributes[:host])
22
+ expect(subject.version).to eq(attributes[:version])
21
23
  end
22
24
  end
23
25
  end
@@ -15,12 +15,13 @@ describe DeepL do
15
15
 
16
16
  context 'When providing a valid configuration' do
17
17
  let(:configuration) do
18
- DeepL::Configuration.new(auth_key: 'VALID', host: 'http://www.example.org')
18
+ DeepL::Configuration.new(auth_key: 'VALID', host: 'http://www.example.org', version: 'v1')
19
19
  end
20
20
  before do
21
21
  subject.configure do |config|
22
22
  config.auth_key = configuration.auth_key
23
23
  config.host = configuration.host
24
+ config.version = configuration.version
24
25
  end
25
26
  end
26
27
 
@@ -49,7 +50,7 @@ describe DeepL do
49
50
 
50
51
  context 'When translating a text' do
51
52
  it 'should create and call a request object' do
52
- expect(DeepL::Requests::TranslateText).to receive(:new)
53
+ expect(DeepL::Requests::Translate).to receive(:new)
53
54
  .with(subject.api, input, source_lang, target_lang, options).and_call_original
54
55
 
55
56
  text = subject.translate(input, source_lang, target_lang, options)
@@ -57,4 +58,22 @@ describe DeepL do
57
58
  end
58
59
  end
59
60
  end
61
+
62
+ describe '#usage' do
63
+ let(:options) { {} }
64
+
65
+ around do |example|
66
+ VCR.use_cassette('deepl_usage') { example.call }
67
+ end
68
+
69
+ context 'When checking usage' do
70
+ it 'should create and call a request object' do
71
+ expect(DeepL::Requests::Usage).to receive(:new)
72
+ .with(subject.api, options).and_call_original
73
+
74
+ usage = subject.usage(options)
75
+ expect(usage).to be_a(DeepL::Resources::Usage)
76
+ end
77
+ end
78
+ end
60
79
  end
@@ -2,114 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: post
5
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
6
- body:
7
- encoding: US-ASCII
8
- string: source_lang=EN&target_lang=ES
9
- headers:
10
- Accept-Encoding:
11
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
- Accept:
13
- - "*/*"
14
- User-Agent:
15
- - Ruby
16
- Content-Type:
17
- - application/x-www-form-urlencoded
18
- response:
19
- status:
20
- code: 400
21
- message: Bad Request
22
- headers:
23
- Server:
24
- - nginx
25
- Date:
26
- - Sun, 10 Dec 2017 21:20:25 GMT
27
- Content-Length:
28
- - '45'
29
- Connection:
30
- - keep-alive
31
- body:
32
- encoding: UTF-8
33
- string: '{"message":"Parameter ''text'' not specified."}'
34
- http_version:
35
- recorded_at: Sun, 10 Dec 2017 21:20:25 GMT
36
- - request:
37
- method: post
38
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
39
- body:
40
- encoding: US-ASCII
41
- string: text=Abc&source_lang=EN&target_lang=ES
42
- headers:
43
- Accept-Encoding:
44
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
45
- Accept:
46
- - "*/*"
47
- User-Agent:
48
- - Ruby
49
- Content-Type:
50
- - application/x-www-form-urlencoded
51
- response:
52
- status:
53
- code: 200
54
- message: OK
55
- headers:
56
- Server:
57
- - nginx
58
- Date:
59
- - Sun, 10 Dec 2017 21:21:23 GMT
60
- Content-Type:
61
- - application/json
62
- Content-Length:
63
- - '65'
64
- Connection:
65
- - keep-alive
66
- Access-Control-Allow-Origin:
67
- - "*"
68
- body:
69
- encoding: UTF-8
70
- string: '{"translations":[{"detected_source_language":"EN","text":"ABC"}]}'
71
- http_version:
72
- recorded_at: Sun, 10 Dec 2017 21:21:23 GMT
73
- - request:
74
- method: post
75
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
76
- body:
77
- encoding: US-ASCII
78
- string: text=Sample&source_lang=EN&target_lang=ES
79
- headers:
80
- Accept-Encoding:
81
- - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
82
- Accept:
83
- - "*/*"
84
- User-Agent:
85
- - Ruby
86
- Content-Type:
87
- - application/x-www-form-urlencoded
88
- response:
89
- status:
90
- code: 200
91
- message: OK
92
- headers:
93
- Server:
94
- - nginx
95
- Date:
96
- - Sun, 10 Dec 2017 21:22:11 GMT
97
- Content-Type:
98
- - application/json
99
- Content-Length:
100
- - '69'
101
- Connection:
102
- - keep-alive
103
- Access-Control-Allow-Origin:
104
- - "*"
105
- body:
106
- encoding: UTF-8
107
- string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"}]}'
108
- http_version:
109
- recorded_at: Sun, 10 Dec 2017 21:22:11 GMT
110
- - request:
111
- method: post
112
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN&param=fake
5
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN&param=fake
113
6
  body:
114
7
  encoding: US-ASCII
115
8
  string: text=Sample&source_lang=EN&target_lang=ES
@@ -130,7 +23,7 @@ http_interactions:
130
23
  Server:
131
24
  - nginx
132
25
  Date:
133
- - Thu, 04 Jan 2018 18:08:31 GMT
26
+ - Tue, 08 May 2018 16:31:34 GMT
134
27
  Content-Type:
135
28
  - application/json
136
29
  Content-Length:
@@ -142,6 +35,6 @@ http_interactions:
142
35
  body:
143
36
  encoding: UTF-8
144
37
  string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"}]}'
145
- http_version:
146
- recorded_at: Thu, 04 Jan 2018 18:08:31 GMT
38
+ http_version:
39
+ recorded_at: Tue, 08 May 2018 16:31:33 GMT
147
40
  recorded_with: VCR 4.0.0
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.deepl.com/v2/usage?auth_key=VALID_TOKEN
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Server:
22
+ - nginx
23
+ Date:
24
+ - Tue, 08 May 2018 16:31:35 GMT
25
+ Content-Length:
26
+ - '52'
27
+ Connection:
28
+ - keep-alive
29
+ body:
30
+ encoding: UTF-8
31
+ string: '{"character_count":802127,"character_limit":1000000}'
32
+ http_version:
33
+ recorded_at: Tue, 08 May 2018 16:31:33 GMT
34
+ recorded_with: VCR 4.0.0
@@ -2,7 +2,7 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: post
5
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
5
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN
6
6
  body:
7
7
  encoding: US-ASCII
8
8
  string: text=Sample+text&source_lang=EN&target_lang=ES
@@ -23,11 +23,11 @@ http_interactions:
23
23
  Server:
24
24
  - nginx
25
25
  Date:
26
- - Sun, 10 Dec 2017 20:34:26 GMT
26
+ - Tue, 08 May 2018 16:31:36 GMT
27
27
  Content-Type:
28
28
  - application/json
29
29
  Content-Length:
30
- - '78'
30
+ - '74'
31
31
  Connection:
32
32
  - keep-alive
33
33
  Access-Control-Allow-Origin:
@@ -37,10 +37,10 @@ http_interactions:
37
37
  string: '{"translations":[{"detected_source_language":"EN","text":"Texto de
38
38
  muestra"}]}'
39
39
  http_version:
40
- recorded_at: Sun, 10 Dec 2017 20:34:26 GMT
40
+ recorded_at: Tue, 08 May 2018 16:31:34 GMT
41
41
  - request:
42
42
  method: post
43
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
43
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN
44
44
  body:
45
45
  encoding: US-ASCII
46
46
  string: text=Sample&text=Word&source_lang=EN&target_lang=ES
@@ -61,7 +61,7 @@ http_interactions:
61
61
  Server:
62
62
  - nginx
63
63
  Date:
64
- - Sun, 10 Dec 2017 20:34:26 GMT
64
+ - Tue, 08 May 2018 16:31:37 GMT
65
65
  Content-Type:
66
66
  - application/json
67
67
  Content-Length:
@@ -74,10 +74,48 @@ http_interactions:
74
74
  encoding: UTF-8
75
75
  string: '{"translations":[{"detected_source_language":"EN","text":"Muestra"},{"detected_source_language":"EN","text":"Palabra"}]}'
76
76
  http_version:
77
- recorded_at: Sun, 10 Dec 2017 20:34:26 GMT
77
+ recorded_at: Tue, 08 May 2018 16:31:35 GMT
78
78
  - request:
79
79
  method: post
80
- uri: https://api.deepl.com/v1/translate?auth_key=invalid
80
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN
81
+ body:
82
+ encoding: US-ASCII
83
+ string: text=%3Cp%3ESample+text%3C%2Fp%3E&source_lang=EN&target_lang=ES
84
+ headers:
85
+ Accept-Encoding:
86
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
87
+ Accept:
88
+ - "*/*"
89
+ User-Agent:
90
+ - Ruby
91
+ Content-Type:
92
+ - application/x-www-form-urlencoded
93
+ response:
94
+ status:
95
+ code: 200
96
+ message: OK
97
+ headers:
98
+ Server:
99
+ - nginx
100
+ Date:
101
+ - Tue, 08 May 2018 16:31:37 GMT
102
+ Content-Type:
103
+ - application/json
104
+ Content-Length:
105
+ - '85'
106
+ Connection:
107
+ - keep-alive
108
+ Access-Control-Allow-Origin:
109
+ - "*"
110
+ body:
111
+ encoding: UTF-8
112
+ string: '{"translations":[{"detected_source_language":"EN","text":"<p>Texto
113
+ de muestra</p>"}]}'
114
+ http_version:
115
+ recorded_at: Tue, 08 May 2018 16:31:36 GMT
116
+ - request:
117
+ method: post
118
+ uri: https://api.deepl.com/v2/translate?auth_key=invalid
81
119
  body:
82
120
  encoding: US-ASCII
83
121
  string: text=Sample+text&source_lang=EN&target_lang=ES
@@ -98,7 +136,7 @@ http_interactions:
98
136
  Server:
99
137
  - nginx
100
138
  Date:
101
- - Sun, 10 Dec 2017 20:34:27 GMT
139
+ - Tue, 08 May 2018 16:31:38 GMT
102
140
  Content-Length:
103
141
  - '0'
104
142
  Connection:
@@ -107,10 +145,10 @@ http_interactions:
107
145
  encoding: UTF-8
108
146
  string: ''
109
147
  http_version:
110
- recorded_at: Sun, 10 Dec 2017 20:34:27 GMT
148
+ recorded_at: Tue, 08 May 2018 16:31:37 GMT
111
149
  - request:
112
150
  method: post
113
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
151
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN
114
152
  body:
115
153
  encoding: US-ASCII
116
154
  string: source_lang=EN&target_lang=ES
@@ -131,7 +169,7 @@ http_interactions:
131
169
  Server:
132
170
  - nginx
133
171
  Date:
134
- - Sun, 10 Dec 2017 20:34:27 GMT
172
+ - Tue, 08 May 2018 16:31:38 GMT
135
173
  Content-Length:
136
174
  - '45'
137
175
  Connection:
@@ -140,10 +178,10 @@ http_interactions:
140
178
  encoding: UTF-8
141
179
  string: '{"message":"Parameter ''text'' not specified."}'
142
180
  http_version:
143
- recorded_at: Sun, 10 Dec 2017 20:34:27 GMT
181
+ recorded_at: Tue, 08 May 2018 16:31:37 GMT
144
182
  - request:
145
183
  method: post
146
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
184
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN
147
185
  body:
148
186
  encoding: US-ASCII
149
187
  string: text=Sample+text&source_lang=EN
@@ -164,7 +202,7 @@ http_interactions:
164
202
  Server:
165
203
  - nginx
166
204
  Date:
167
- - Sun, 10 Dec 2017 20:34:28 GMT
205
+ - Tue, 08 May 2018 16:31:39 GMT
168
206
  Content-Length:
169
207
  - '52'
170
208
  Connection:
@@ -173,13 +211,13 @@ http_interactions:
173
211
  encoding: UTF-8
174
212
  string: '{"message":"Parameter ''target_lang'' not specified."}'
175
213
  http_version:
176
- recorded_at: Sun, 10 Dec 2017 20:34:28 GMT
214
+ recorded_at: Tue, 08 May 2018 16:31:37 GMT
177
215
  - request:
178
216
  method: post
179
- uri: https://api.deepl.com/v1/translate?auth_key=VALID_TOKEN
217
+ uri: https://api.deepl.com/v2/translate?auth_key=VALID_TOKEN&ignore_tags=code,%20span
180
218
  body:
181
219
  encoding: US-ASCII
182
- string: text=%3Cp%3ESample+text%3C%2Fp%3E&source_lang=EN&target_lang=ES
220
+ string: text=Welcome+and+%3Ccode%3EHello+great+World%3C%2Fcode%3E+Good+Morning%21&source_lang=EN&target_lang=ES
183
221
  headers:
184
222
  Accept-Encoding:
185
223
  - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
@@ -197,19 +235,19 @@ http_interactions:
197
235
  Server:
198
236
  - nginx
199
237
  Date:
200
- - Thu, 04 Jan 2018 18:09:50 GMT
238
+ - Tue, 24 Jul 2018 16:13:51 GMT
201
239
  Content-Type:
202
240
  - application/json
203
241
  Content-Length:
204
- - '85'
242
+ - '119'
205
243
  Connection:
206
244
  - keep-alive
207
245
  Access-Control-Allow-Origin:
208
246
  - "*"
209
247
  body:
210
- encoding: UTF-8
211
- string: '{"translations":[{"detected_source_language":"EN","text":"<p>Texto
212
- de muestra</p>"}]}'
248
+ encoding: ASCII-8BIT
249
+ string: !binary |-
250
+ eyJ0cmFuc2xhdGlvbnMiOlt7ImRldGVjdGVkX3NvdXJjZV9sYW5ndWFnZSI6IkVOIiwidGV4dCI6IkJpZW52ZW5pZG8geSA8Y29kZT5IZWxsbyBncmVhdCBXb3JsZDwvY29kZT4gQnVlbm9zIGTDrWFzISJ9XX0=
213
251
  http_version:
214
- recorded_at: Thu, 04 Jan 2018 18:09:49 GMT
252
+ recorded_at: Tue, 24 Jul 2018 16:13:49 GMT
215
253
  recorded_with: VCR 4.0.0
@@ -0,0 +1,34 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://api.deepl.com/v2/usage?auth_key=VALID_TOKEN
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Server:
22
+ - nginx
23
+ Date:
24
+ - Tue, 08 May 2018 20:42:44 GMT
25
+ Content-Length:
26
+ - '52'
27
+ Connection:
28
+ - keep-alive
29
+ body:
30
+ encoding: UTF-8
31
+ string: '{"character_count":805718,"character_limit":1000000}'
32
+ http_version:
33
+ recorded_at: Tue, 08 May 2018 20:42:43 GMT
34
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,207 @@
1
+ require 'spec_helper'
2
+
3
+ describe DeepL::Requests::Translate do
4
+ let(:tags_str) { 'p, strong, span' }
5
+ let(:tags_array) { %w[p strong span] }
6
+
7
+ let(:api) { build_deepl_api }
8
+ let(:text) { 'Sample text' }
9
+ let(:source_lang) { 'EN' }
10
+ let(:target_lang) { 'ES' }
11
+ let(:options) { {} }
12
+ subject { DeepL::Requests::Translate.new(api, text, source_lang, target_lang, options) }
13
+
14
+ describe '#initialize' do
15
+ context 'When building a request' do
16
+ it 'should create a request object' do
17
+ expect(subject).to be_a(DeepL::Requests::Translate)
18
+ end
19
+ end
20
+
21
+ context 'when using `non_splitting_tags` options' do
22
+ it 'should work with a nil values' do
23
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: nil)
24
+ expect(request.options[:non_splitting_tags]).to eq(nil)
25
+ end
26
+
27
+ it 'should work with a blank list' do
28
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: '')
29
+ expect(request.options[:non_splitting_tags]).to eq('')
30
+ end
31
+
32
+ it 'should work with a comma-separated list' do
33
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
34
+ expect(request.options[:non_splitting_tags]).to eq(tags_str)
35
+ end
36
+
37
+ it 'should convert arrays to strings' do
38
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_array)
39
+ expect(request.options[:non_splitting_tags]).to eq(tags_str)
40
+ end
41
+
42
+ it 'should leave strings as they are' do
43
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
44
+ expect(request.options[:non_splitting_tags]).to eq(tags_str)
45
+ end
46
+ end
47
+
48
+ context 'when using `ignore_tags` options' do
49
+ it 'should work with a nil values' do
50
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: nil)
51
+ expect(request.options[:ignore_tags]).to eq(nil)
52
+ end
53
+
54
+ it 'should work with a blank list' do
55
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: '')
56
+ expect(request.options[:ignore_tags]).to eq('')
57
+ end
58
+
59
+ it 'should work with a comma-separated list' do
60
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
61
+ expect(request.options[:ignore_tags]).to eq(tags_str)
62
+ end
63
+
64
+ it 'should convert arrays to strings' do
65
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_array)
66
+ expect(request.options[:ignore_tags]).to eq(tags_str)
67
+ end
68
+
69
+ it 'should leave strings as they are' do
70
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
71
+ expect(request.options[:ignore_tags]).to eq(tags_str)
72
+ end
73
+ end
74
+
75
+ context 'when using `split_sentences` options' do
76
+ it 'should convert `true` to `1`' do
77
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: true)
78
+ expect(request.options[:split_sentences]).to eq('1')
79
+ end
80
+
81
+ it 'should convert `false` to `0`' do
82
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: false)
83
+ expect(request.options[:split_sentences]).to eq('0')
84
+ end
85
+
86
+ it 'should leave `0` as is' do
87
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '0')
88
+ expect(request.options[:split_sentences]).to eq('0')
89
+ end
90
+
91
+ it 'should leave `1` as is' do
92
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
93
+ expect(request.options[:split_sentences]).to eq('1')
94
+ end
95
+ end
96
+
97
+ context 'when using `preserve_formatting` options' do
98
+ it 'should convert `true` to `1`' do
99
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: true)
100
+ expect(request.options[:preserve_formatting]).to eq('1')
101
+ end
102
+
103
+ it 'should convert `false` to `0`' do
104
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: false)
105
+ expect(request.options[:preserve_formatting]).to eq('0')
106
+ end
107
+
108
+ it 'should leave `0` as is' do
109
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '0')
110
+ expect(request.options[:preserve_formatting]).to eq('0')
111
+ end
112
+
113
+ it 'should leave `1` as is' do
114
+ request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '1')
115
+ expect(request.options[:preserve_formatting]).to eq('1')
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#request' do
121
+ around do |example|
122
+ VCR.use_cassette('translate_texts') { example.call }
123
+ end
124
+
125
+ context 'When performing a valid request with one text' do
126
+ it 'should return a text object' do
127
+ text = subject.request
128
+
129
+ expect(text).to be_a(DeepL::Resources::Text)
130
+ expect(text.text).to eq('Texto de muestra')
131
+ expect(text.detected_source_language).to eq('EN')
132
+ end
133
+ end
134
+
135
+ context 'When performing a valid request with multiple texts' do
136
+ let(:text) { %w[Sample Word] }
137
+
138
+ it 'should return a text object' do
139
+ texts = subject.request
140
+
141
+ expect(texts).to be_a(Array)
142
+ expect(texts.first.text).to eq('Muestra')
143
+ expect(texts.first.detected_source_language).to eq('EN')
144
+
145
+ expect(texts.last.text).to eq('Palabra')
146
+ expect(texts.last.detected_source_language).to eq('EN')
147
+ end
148
+ end
149
+
150
+ context 'When performing a valid request with tag handling' do
151
+ let(:text) { '<p>Sample text</p>' }
152
+
153
+ it 'should return a text object' do
154
+ text = subject.request
155
+
156
+ expect(text).to be_a(DeepL::Resources::Text)
157
+ expect(text.text).to eq('<p>Texto de muestra</p>')
158
+ expect(text.detected_source_language).to eq('EN')
159
+ end
160
+ end
161
+
162
+ context 'When performing a valid request and passing a variable' do
163
+ let(:text) { 'Welcome and <code>Hello great World</code> Good Morning!' }
164
+ let(:options) { { ignore_tags: 'code, span' } }
165
+
166
+ it 'should return a text object' do
167
+ text = subject.request
168
+
169
+ expect(text).to be_a(DeepL::Resources::Text)
170
+ expect(text.text).to eq('Bienvenido y <code>Hello great World</code> Buenos días!')
171
+ expect(text.detected_source_language).to eq('EN')
172
+ end
173
+ end
174
+
175
+ context 'When performing a bad request' do
176
+ context 'When using an invalid token' do
177
+ let(:api) do
178
+ api = build_deepl_api
179
+ api.configuration.auth_key = 'invalid'
180
+ api
181
+ end
182
+
183
+ it 'should raise an unauthorized error' do
184
+ expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
185
+ end
186
+ end
187
+
188
+ context 'When using an invalid text' do
189
+ let(:text) { nil }
190
+
191
+ it 'should raise a bad request error' do
192
+ message = "Parameter 'text' not specified."
193
+ expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
194
+ end
195
+ end
196
+
197
+ context 'When using an invalid target language' do
198
+ let(:target_lang) { nil }
199
+
200
+ it 'should raise a bad request error' do
201
+ message = "Parameter 'target_lang' not specified."
202
+ expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe DeepL::Requests::Usage do
4
+ let(:api) { build_deepl_api }
5
+ let(:options) { {} }
6
+ subject { DeepL::Requests::Usage.new(api, options) }
7
+
8
+ describe '#initialize' do
9
+ context 'When building a request' do
10
+ it 'should create a request object' do
11
+ expect(subject).to be_a(DeepL::Requests::Usage)
12
+ end
13
+ end
14
+ end
15
+
16
+ describe '#request' do
17
+ around do |example|
18
+ VCR.use_cassette('usage') { example.call }
19
+ end
20
+
21
+ context 'When performing a valid request' do
22
+ it 'should return an usage object' do
23
+ usage = subject.request
24
+
25
+ expect(usage).to be_a(DeepL::Resources::Usage)
26
+ expect(usage.character_count).to be_a(Numeric)
27
+ expect(usage.character_limit).to be_a(Numeric)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe DeepL::Resources::Usage do
4
+ subject { DeepL::Resources::Usage.new(3, 5, nil, nil) }
5
+
6
+ describe '#initialize' do
7
+ context 'When building a basic object' do
8
+ it 'should create a resource' do
9
+ expect(subject).to be_a(DeepL::Resources::Usage)
10
+ end
11
+
12
+ it 'should assign the attributes' do
13
+ expect(subject.character_count).to eq(3)
14
+ expect(subject.character_limit).to eq(5)
15
+ end
16
+
17
+ it 'should not exceed the quota' do
18
+ expect(subject.quota_exceeded?).to be_falsey
19
+ end
20
+ end
21
+
22
+ context 'When building a quota exceeded object' do
23
+ subject { DeepL::Resources::Usage.new(5, 5, nil, nil) }
24
+
25
+ it 'should exceed the quota' do
26
+ expect(subject.quota_exceeded?).to be_truthy
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deepl-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Herzog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-31 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: juwelier
@@ -49,18 +49,25 @@ files:
49
49
  - lib/deepl/exceptions/bad_request.rb
50
50
  - lib/deepl/exceptions/error.rb
51
51
  - lib/deepl/exceptions/limit_exceeded.rb
52
+ - lib/deepl/exceptions/quota_exceeded.rb
52
53
  - lib/deepl/exceptions/request_error.rb
53
54
  - lib/deepl/requests/base.rb
54
- - lib/deepl/requests/translate_text.rb
55
+ - lib/deepl/requests/translate.rb
56
+ - lib/deepl/requests/usage.rb
55
57
  - lib/deepl/resources/base.rb
56
58
  - lib/deepl/resources/text.rb
59
+ - lib/deepl/resources/usage.rb
57
60
  - spec/api/api_spec.rb
58
61
  - spec/api/configuration_spec.rb
59
62
  - spec/api/deepl_spec.rb
60
63
  - spec/fixtures/vcr_cassettes/deepl_translate.yml
64
+ - spec/fixtures/vcr_cassettes/deepl_usage.yml
61
65
  - spec/fixtures/vcr_cassettes/translate_texts.yml
62
- - spec/requests/translate_text_spec.rb
66
+ - spec/fixtures/vcr_cassettes/usage.yml
67
+ - spec/requests/translate_spec.rb
68
+ - spec/requests/usage_spec.rb
63
69
  - spec/resources/text_spec.rb
70
+ - spec/resources/usage_spec.rb
64
71
  - spec/spec_helper.rb
65
72
  homepage: http://github.com/wikiti/deepl-rb
66
73
  licenses:
@@ -82,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
89
  version: '0'
83
90
  requirements: []
84
91
  rubyforge_project:
85
- rubygems_version: 2.6.13
92
+ rubygems_version: 2.6.14
86
93
  signing_key:
87
94
  specification_version: 4
88
95
  summary: A simple ruby wrapper for the DeepL API
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DeepL::Requests::TranslateText do
4
- let(:api) { build_deepl_api }
5
- let(:text) { 'Sample text' }
6
- let(:source_lang) { 'EN' }
7
- let(:target_lang) { 'ES' }
8
- subject { DeepL::Requests::TranslateText.new(api, text, source_lang, target_lang) }
9
-
10
- describe '#initialize' do
11
- context 'When building a request' do
12
- it 'should create a request object' do
13
- expect(subject).to be_a(DeepL::Requests::TranslateText)
14
- end
15
- end
16
- end
17
-
18
- describe '#request' do
19
- around do |example|
20
- VCR.use_cassette('translate_texts') { example.call }
21
- end
22
-
23
- context 'When performing a valid request with one text' do
24
- it 'should return a text object' do
25
- text = subject.request
26
-
27
- expect(text).to be_a(DeepL::Resources::Text)
28
- expect(text.text).to eq('Texto de muestra')
29
- expect(text.detected_source_language).to eq('EN')
30
- end
31
- end
32
-
33
- context 'When performing a valid request with multiple texts' do
34
- let(:text) { %w[Sample Word] }
35
-
36
- it 'should return a text object' do
37
- texts = subject.request
38
-
39
- expect(texts).to be_a(Array)
40
- expect(texts.first.text).to eq('Muestra')
41
- expect(texts.first.detected_source_language).to eq('EN')
42
-
43
- expect(texts.last.text).to eq('Palabra')
44
- expect(texts.last.detected_source_language).to eq('EN')
45
- end
46
- end
47
-
48
- context 'When performing a valid request with tag handling' do
49
- let(:text) { '<p>Sample text</p>' }
50
-
51
- it 'should return a text object' do
52
- text = subject.request
53
-
54
- expect(text).to be_a(DeepL::Resources::Text)
55
- expect(text.text).to eq('<p>Texto de muestra</p>')
56
- expect(text.detected_source_language).to eq('EN')
57
- end
58
- end
59
-
60
- context 'When performing a bad request' do
61
- context 'When using an invalid token' do
62
- let(:api) do
63
- api = build_deepl_api
64
- api.configuration.auth_key = 'invalid'
65
- api
66
- end
67
-
68
- it 'should raise an unauthorized error' do
69
- expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
70
- end
71
- end
72
-
73
- context 'When using an invalid text' do
74
- let(:text) { nil }
75
-
76
- it 'should raise a bad request error' do
77
- message = "Parameter 'text' not specified."
78
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
79
- end
80
- end
81
-
82
- context 'When using an invalid target language' do
83
- let(:target_lang) { nil }
84
-
85
- it 'should raise a bad request error' do
86
- message = "Parameter 'target_lang' not specified."
87
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
88
- end
89
- end
90
- end
91
- end
92
- end