toptranslation_api 2.5.1 → 4.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9c933ca38119ae195157a317a8936ce02ec8f0675bcc99b6fb68a47c46cbf99
4
- data.tar.gz: b534c5d248203cd7e603d4c7f74e7d42d8f0090925e9f1c938455e8babac910c
3
+ metadata.gz: 700cce0881675c0dafc87f6b0164c4de80495fc146ff9487584c1c2d7d1abf02
4
+ data.tar.gz: 23384b7748432e0d96ac60f45dba8e6e1c0219fe6c0ed53fdd70c54b7a2e3388
5
5
  SHA512:
6
- metadata.gz: 9b59f466315b59fce39022b2456347b04976e85f49fe947398bb923efad31b57d60b637cafbc05d1c8576864073888a4ea1bf165ef35095a850a86b78c8009b3
7
- data.tar.gz: 4d837be4accace42ffc05bbf1d4718c30c62ce52a8e60db174a7ea19818b468df44975c01ace9638e223a67583b94bba7beb081048dccdde04a94e7de3159295
6
+ metadata.gz: 770ef06cea32101858e51b52c0ac6322315174ed484f01ee16809d44242a4d639d12bb18861cb9fbc4ffb0959605095a310ef0255749f595ec8361ea0409617a
7
+ data.tar.gz: 710217d985fd2fd1094de9f4d3152cdb8452599b9b414303ad063c326ce6d1b818ef2fa1875a586d23e0c89e658574f35a5463edbea9f584a8c14ed33296e3ec
@@ -1,6 +1,8 @@
1
1
  module Toptranslation
2
- class Connection # rubocop:disable Metrics/ClassLength
3
- attr_accessor :upload_token, :verbose, :access_token
2
+ class Connection
3
+ attr_accessor :verbose, :access_token
4
+
5
+ API_VERSION = 'v2'.freeze
4
6
 
5
7
  def initialize(options = {})
6
8
  @base_url = options[:base_url] || 'https://api.toptranslation.com'
@@ -11,15 +13,15 @@ module Toptranslation
11
13
  end
12
14
 
13
15
  def get(path, options = {})
14
- transform_response(request(:get, path, options), options)
16
+ transform_response(request(:get, path, options))
15
17
  end
16
18
 
17
19
  def post(path, options = {})
18
- transform_response(request(:post, path, options), options)
20
+ transform_response(request(:post, path, options))
19
21
  end
20
22
 
21
23
  def patch(path, options = {})
22
- transform_response(request(:patch, path, options), options)
24
+ transform_response(request(:patch, path, options))
23
25
  end
24
26
 
25
27
  def download(url, path, &block)
@@ -28,10 +30,12 @@ module Toptranslation
28
30
  download_uri(uri, path, &block)
29
31
  end
30
32
 
31
- def upload(filepath, type, &block)
32
- uri = URI.parse("#{@files_url}/documents")
33
+ # Uploads go directly to the document store (castor): the response
34
+ # carries the document token that authorizes further use of the file.
35
+ def upload(filepath, _type = 'document', &block)
36
+ uri = URI.parse("#{@files_url}/v2/documents")
33
37
  file = File.new(filepath)
34
- upload_file(file, type, uri, &block)
38
+ upload_file(file, uri, &block)
35
39
  end
36
40
 
37
41
  def sign_in!(options)
@@ -43,7 +47,7 @@ module Toptranslation
43
47
  application_id: 'pollux'
44
48
  }.merge(options)
45
49
 
46
- @access_token = post('/auth/sign_in', sign_in_options.merge(version: 2))['access_token']
50
+ @access_token = post('/auth/sign_in', sign_in_options)['access_token']
47
51
 
48
52
  puts "# Requested access token #{@access_token}" if @verbose
49
53
 
@@ -52,12 +56,8 @@ module Toptranslation
52
56
 
53
57
  private
54
58
 
55
- def version(options)
56
- options[:version] || 0
57
- end
58
-
59
59
  def request(method, path, options)
60
- url = "#{@base_url}/v#{version(options)}#{path}"
60
+ url = "#{@base_url}/#{API_VERSION}#{path}"
61
61
  puts "# #{method}-request #{url}" if @verbose
62
62
  puts "options: #{prepare_request_options(options, method)}" if @verbose
63
63
  RestClient.send(method, url, prepare_request_options(options, method))
@@ -66,30 +66,13 @@ module Toptranslation
66
66
  raise e
67
67
  end
68
68
 
69
- def upload_token
70
- @upload_token ||= request_upload_token
71
- end
72
-
73
- def request_upload_token
74
- puts '# Requesting upload-token' if @verbose
75
- token = post('/upload_tokens')['upload_token']
76
- puts "# Upload-token retrieved: #{token}" if @verbose
77
- token
78
- end
79
-
80
- def transform_response(response, options)
69
+ def transform_response(response)
81
70
  puts response if @verbose
82
- parsed = JSON.parse(response)
83
- if version(options) == 2
84
- parsed
85
- else
86
- parsed['data']
87
- end
71
+ JSON.parse(response)
88
72
  end
89
73
 
90
74
  def prepare_request_options(options, method)
91
75
  request_options = options.dup
92
- request_options.delete(:version)
93
76
  if [:post, :patch].include? method
94
77
  request_options.merge(auth_params)
95
78
  else
@@ -139,22 +122,17 @@ module Toptranslation
139
122
  end
140
123
  end
141
124
 
142
- def upload_file(file, type, uri)
143
- last_upload_size = 0
144
-
125
+ def upload_file(file, uri)
145
126
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
146
127
  request = Net::HTTP::Post.new(uri)
147
- request.set_form({ 'file' => file, 'token' => upload_token, 'type' => type }, 'multipart/form-data')
148
-
149
- Net::HTTP::UploadProgress.new(request) do |progress|
150
- yield progress.upload_size - last_upload_size if block_given?
151
- last_upload_size = progress.upload_size
152
- end
128
+ # stable per upload (not per attempt): the document store recognizes
129
+ # retries and returns the already created document instead of a duplicate
130
+ request.set_form({ 'file' => file, 'idempotency_key' => SecureRandom.uuid }, 'multipart/form-data')
153
131
 
154
132
  http.request(request)
155
133
  end
156
134
 
157
- transform_response(response.body, version: 0)
135
+ transform_response(response.body)
158
136
  end
159
137
  end
160
138
  end
@@ -8,17 +8,6 @@ module Toptranslation::Resource
8
8
  update_from_response(options)
9
9
  end
10
10
 
11
- def add_translation(filepath, locale_code, options = {})
12
- upload = Upload.new(@connection).upload(filepath)
13
-
14
- response = @connection.post("/documents/#{@identifier}/translations", options.merge(
15
- document_store_id: upload.document_store_id,
16
- document_token: upload.document_token,
17
- locale_code: locale_code,
18
- sha1: upload.sha1
19
- ))
20
- end
21
-
22
11
  def download_url(locale_code, options = {})
23
12
  params = { file_format: options[:file_format], locale_code: locale_code }.compact
24
13
  @connection.get("/documents/#{@identifier}/download", params: params)['download_url']
@@ -36,24 +25,8 @@ module Toptranslation::Resource
36
25
  @connection.download(download_url(locale_code, options), download_path, &block)
37
26
  end
38
27
 
39
- def save
40
- response = @connection.patch("/documents/#{@identifier}", remote_hash)
41
- update_and_return_from_response(response)
42
- end
43
-
44
- def strings
45
- StringList.new(@connection, document_identifier: @identifier)
46
- end
47
-
48
28
  private
49
29
 
50
- def update_and_return_from_response(response)
51
- if response
52
- update_from_response(response)
53
- self
54
- end
55
- end
56
-
57
30
  def update_from_response(response)
58
31
  @identifier = response['identifier'] if response['identifier']
59
32
  @name = response['name'] if response['name']
@@ -70,10 +43,5 @@ module Toptranslation::Resource
70
43
  end
71
44
  end
72
45
 
73
- def remote_hash
74
- hash = {}
75
- hash[:name] = @name if @name
76
- hash
77
- end
78
46
  end
79
47
  end
@@ -7,11 +7,6 @@ module Toptranslation::Resource
7
7
  @options = options
8
8
  end
9
9
 
10
- def find(identifier)
11
- result = @connection.get("/documents/#{identifier}")
12
- Document.new(@connection, result)
13
- end
14
-
15
10
  def each
16
11
  documents.each { |document| yield Document.new(@connection, document) }
17
12
  end
@@ -33,10 +33,6 @@ module Toptranslation::Resource
33
33
  ProjectDocumentList.new(@connection, options)
34
34
  end
35
35
 
36
- def strings
37
- StringList.new(@connection, project_identifier: @identifier)
38
- end
39
-
40
36
  def save
41
37
  response = @identifier ? update_remote_project : create_remote_project
42
38
  update_and_return_from_response(response)
@@ -62,11 +58,13 @@ module Toptranslation::Resource
62
58
  def update_from_response(response)
63
59
  @identifier = response['identifier'] if response['identifier']
64
60
  @created_at = DateTime.parse(response['created_at']) if response['created_at']
65
- @locales = response['locales'].inject([]) do |accu, locale_data|
61
+ @locales = Array(response['locales']).inject([]) do |accu, locale_data|
66
62
  locale = Locale.new(locale_data)
67
63
  @source_locale = locale if locale_data['is_source_locale'] # Set source locale of the project
68
64
  accu << locale
69
65
  end
66
+ # API v2 liefert die Ausgangssprache zusätzlich als eigenes Feld
67
+ @source_locale = Locale.new(response['source_locale']) if response['source_locale']
70
68
  @name = response['name'] if response['name']
71
69
  end
72
70
 
@@ -1,5 +1,11 @@
1
1
  module Toptranslation::Resource
2
2
  class ProjectDocumentList < DocumentList
3
+ # projektbezogener Lookup — API v2 kennt kein unscoped GET /documents/:id
4
+ def find(identifier)
5
+ result = @connection.get("/projects/#{@options[:project_identifier]}/documents/#{identifier}")
6
+ Document.new(@connection, result)
7
+ end
8
+
3
9
  def create(name, path, options = {})
4
10
  response = @connection.post("/projects/#{@options[:project_identifier]}/documents", options.merge(name: name, path: path))
5
11
  Document.new(@connection, response)
@@ -19,7 +19,7 @@ module Toptranslation::Resource
19
19
  private
20
20
 
21
21
  def translations
22
- @connection.get("/orders/#{@options[:order_identifier]}/translations") if @options[:order_identifier]
22
+ @connection.get('/translations', params: { order_identifier: @options[:order_identifier] }) if @options[:order_identifier]
23
23
  end
24
24
  end
25
25
  end
@@ -12,7 +12,8 @@ module Toptranslation::Resource
12
12
  response = @connection.upload(filepath, type, &block)
13
13
 
14
14
  @document_store_id = response['identifier']
15
- @document_token = response['document_token']
15
+ # Document-Store v2 nennt das Feld token
16
+ @document_token = response['token'] || response['document_token']
16
17
  @sha1 = response['sha1']
17
18
 
18
19
  self
@@ -1,3 +1,3 @@
1
1
  module Toptranslation
2
- VERSION = '2.5.1'.freeze
2
+ VERSION = '4.0.0'.freeze
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'json'
2
+ require 'securerandom'
2
3
  require 'rest-client'
3
- require 'net/http/uploadprogress'
4
4
  require 'toptranslation/client'
5
5
  require 'toptranslation/connection'
6
6
  require 'toptranslation/resource/document'
@@ -14,8 +14,6 @@ require 'toptranslation/resource/project'
14
14
  require 'toptranslation/resource/project_list'
15
15
  require 'toptranslation/resource/quote'
16
16
  require 'toptranslation/resource/reference_document'
17
- require 'toptranslation/resource/string_list'
18
- require 'toptranslation/resource/string'
19
17
  require 'toptranslation/resource/translation'
20
18
  require 'toptranslation/resource/translation_list'
21
19
  require 'toptranslation/resource/upload'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toptranslation_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toptranslation GmbH
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-07 00:00:00.000000000 Z
11
+ date: 2026-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
- - !ruby/object:Gem::Dependency
28
- name: net-http-uploadprogress
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '2.0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '2.0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rest-client
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -98,30 +84,30 @@ dependencies:
98
84
  name: rubocop
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
- - - "~>"
87
+ - - ">="
102
88
  - !ruby/object:Gem::Version
103
- version: 0.74.0
89
+ version: '0'
104
90
  type: :development
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
- - - "~>"
94
+ - - ">="
109
95
  - !ruby/object:Gem::Version
110
- version: 0.74.0
96
+ version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: rubocop-rspec
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
- - - "~>"
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
- version: 1.35.0
103
+ version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
- - - "~>"
108
+ - - ">="
123
109
  - !ruby/object:Gem::Version
124
- version: 1.35.0
110
+ version: '0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: vcr
127
113
  requirement: !ruby/object:Gem::Requirement
@@ -170,8 +156,6 @@ files:
170
156
  - lib/toptranslation/resource/project_list.rb
171
157
  - lib/toptranslation/resource/quote.rb
172
158
  - lib/toptranslation/resource/reference_document.rb
173
- - lib/toptranslation/resource/string.rb
174
- - lib/toptranslation/resource/string_list.rb
175
159
  - lib/toptranslation/resource/translation.rb
176
160
  - lib/toptranslation/resource/translation_list.rb
177
161
  - lib/toptranslation/resource/upload.rb
@@ -181,8 +165,9 @@ files:
181
165
  homepage: https://github.com/toptranslation/toptranslation_ruby
182
166
  licenses:
183
167
  - MIT
184
- metadata: {}
185
- post_install_message:
168
+ metadata:
169
+ rubygems_mfa_required: 'true'
170
+ post_install_message:
186
171
  rdoc_options: []
187
172
  require_paths:
188
173
  - lib
@@ -190,15 +175,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
175
  requirements:
191
176
  - - ">="
192
177
  - !ruby/object:Gem::Version
193
- version: '0'
178
+ version: 3.0.0
194
179
  required_rubygems_version: !ruby/object:Gem::Requirement
195
180
  requirements:
196
181
  - - ">="
197
182
  - !ruby/object:Gem::Version
198
183
  version: '0'
199
184
  requirements: []
200
- rubygems_version: 3.0.3
201
- signing_key:
185
+ rubygems_version: 3.5.9
186
+ signing_key:
202
187
  specification_version: 4
203
188
  summary: A ruby client for the Toptranslation API
204
189
  test_files: []
@@ -1,64 +0,0 @@
1
- module Toptranslation::Resource
2
- class String
3
- attr_reader :identifier, :updated_at, :created_at
4
- attr_accessor :value, :state, :project_identifier, :document_identifier, :key, :comment, :context, :array_index, :plural_form, :locale_code
5
-
6
- def initialize(connection, options = {})
7
- @connection = connection
8
- @options = options
9
-
10
- update_from_response(options)
11
- end
12
-
13
- def save
14
- response = @identifier ? update_remote_string : create_remote_string
15
- update_and_return_from_response(response)
16
- end
17
-
18
- private
19
-
20
- def update_remote_string
21
- @connection.patch("/strings/#{@identifier}", remote_hash)
22
- end
23
-
24
- def create_remote_string
25
- @connection.post('/strings', remote_hash)
26
- end
27
-
28
- def update_and_return_from_response(response)
29
- if response
30
- update_from_response(response)
31
- self
32
- end
33
- end
34
-
35
- def update_from_response(response)
36
- @identifier = response['identifier'] if response['identifier']
37
- @key = response['key'] if response['key']
38
- @value = response['value'] if response['value']
39
- @state = response['state'] if response['state']
40
- @comment = response['comment'] if response['comment']
41
- @context = response['context'] if response['context']
42
- @array_index = response['array_index'] if response['array_index']
43
- @plural_form = response['plural_form'] if response['plural_form']
44
- @locale_code = response['locale_code'] if response['locale_code']
45
- @project_identifier = response['project_identifier'] if response['project_identifier']
46
- @document_identifier = response['document_identifier'] if response['document_identifier']
47
- @updated_at = DateTime.parse(response['updated_at']) if response['updated_at']
48
- @created_at = DateTime.parse(response['created_at']) if response['created_at']
49
- end
50
-
51
- def remote_hash
52
- hash = {}
53
- hash[:key] = @key if @key
54
- hash[:value] = @value if @value
55
- hash[:state] = @state if @state
56
- hash[:comment] = @comment if @comment
57
- hash[:locale_code] = @locale_code if @locale_code
58
- hash[:plural_form] = @plural_form if @plural_form
59
- hash[:project_identifier] = @project_identifier if @project_identifier
60
- hash[:document_identifier] = @document_identifier if @document_identifier
61
- hash
62
- end
63
- end
64
- end
@@ -1,32 +0,0 @@
1
- module Toptranslation::Resource
2
- class StringList
3
- include Enumerable
4
-
5
- def initialize(connection, options = {})
6
- @connection = connection
7
- @options = options
8
- end
9
-
10
- def find(identifier)
11
- result = @connection.get("/strings/#{identifier}")
12
- Document.new(@connection, result)
13
- end
14
-
15
- def each
16
- strings.each { |string| yield String.new(@connection, string) }
17
- end
18
-
19
- def create(options = {})
20
- project_identifier = options['project_identifier'] || @options[:project_identifier]
21
- document_identifier = options['document_identifier'] || @options[:document_identifier]
22
-
23
- String.new(@connection, options.merge('project_identifier' => project_identifier, 'document_identifier' => document_identifier))
24
- end
25
-
26
- private
27
-
28
- def strings
29
- @connection.get('/strings', params: { project_identifier: @options[:project_identifier], document_identifier: @options[:document_identifier] })
30
- end
31
- end
32
- end