toptranslation_api 2.2.0 → 2.3.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 +4 -4
- data/lib/toptranslation/connection.rb +63 -13
- data/lib/toptranslation/resource/document.rb +13 -15
- data/lib/toptranslation/resource/project.rb +2 -2
- data/lib/toptranslation/resource/reference_document.rb +10 -2
- data/lib/toptranslation/resource/upload.rb +2 -2
- data/lib/toptranslation/version.rb +1 -1
- data/lib/toptranslation_api.rb +1 -0
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c33323a6ceaeea5084a3bad1e038611b20986e2aa6fd79d1d5bf5b7cd38104b
|
4
|
+
data.tar.gz: ce619d7ceeb69ed269b56507e2fea4983a0862ac0bf42dabdc0a3a3908658ab9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f580cf9866c741c75f747ef0274406a2662af88046914be4fcb6933e1ac80bb93248019e5ccdab5d508379a27ec18c94add07407ab127c73e997f4c8772dcff9
|
7
|
+
data.tar.gz: 8f0798de6ea5121d54df6b4acc2a98fba9993340d996e4d9de678d96b10bfe8675292656cd16c4bd4db30255dfab8ea9fb789aa4cb77af190e4d4e699d2d4e6f
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Toptranslation
|
2
|
-
class Connection
|
2
|
+
class Connection # rubocop:disable Metrics/ClassLength
|
3
3
|
attr_accessor :upload_token, :verbose
|
4
4
|
|
5
5
|
def initialize(options = {})
|
@@ -21,21 +21,16 @@ module Toptranslation
|
|
21
21
|
transform_response(request(:patch, path, options), options)
|
22
22
|
end
|
23
23
|
|
24
|
-
def download(url)
|
24
|
+
def download(url, path, &block)
|
25
25
|
puts "# downloading #{url}" if @verbose
|
26
|
-
|
27
|
-
|
26
|
+
uri = URI.parse(url)
|
27
|
+
download_uri(uri, path, &block)
|
28
28
|
end
|
29
29
|
|
30
|
-
def upload(filepath, type)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
type: type,
|
35
|
-
token: upload_token
|
36
|
-
)
|
37
|
-
|
38
|
-
transform_response(response, version: 0)
|
30
|
+
def upload(filepath, type, &block)
|
31
|
+
uri = URI.parse("#{@files_url}/documents")
|
32
|
+
file = File.new(filepath)
|
33
|
+
upload_file(file, type, uri, &block)
|
39
34
|
end
|
40
35
|
|
41
36
|
private
|
@@ -103,5 +98,60 @@ module Toptranslation
|
|
103
98
|
def auth_params
|
104
99
|
{ access_token: @access_token }
|
105
100
|
end
|
101
|
+
|
102
|
+
def download_content_length(http, uri)
|
103
|
+
sleep_time = 0.5
|
104
|
+
attempts = 0
|
105
|
+
total = nil
|
106
|
+
|
107
|
+
loop do
|
108
|
+
raise 'File not available' if attempts >= 10
|
109
|
+
|
110
|
+
head_response = http.request_head(uri.request_uri)
|
111
|
+
total = head_response['content-length'].to_i
|
112
|
+
break if head_response.code == '200'
|
113
|
+
|
114
|
+
attempts += 1
|
115
|
+
sleep sleep_time
|
116
|
+
sleep_time += sleep_time * 0.5
|
117
|
+
end
|
118
|
+
|
119
|
+
total
|
120
|
+
end
|
121
|
+
|
122
|
+
def download_uri(uri, path)
|
123
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
124
|
+
total = download_content_length(http, uri)
|
125
|
+
yield nil, total if block_given?
|
126
|
+
|
127
|
+
FileUtils.mkpath(File.dirname(path))
|
128
|
+
file = File.open(path, 'w')
|
129
|
+
http.request_get(uri.request_uri) do |response|
|
130
|
+
response.read_body do |data|
|
131
|
+
file.write(data)
|
132
|
+
yield data.length, total if block_given?
|
133
|
+
end
|
134
|
+
end
|
135
|
+
file
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def upload_file(file, type, uri)
|
140
|
+
last_upload_size = 0
|
141
|
+
|
142
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
143
|
+
request = Net::HTTP::Post.new(uri)
|
144
|
+
request.set_form({ 'file' => file, 'token' => upload_token, 'type' => type }, 'multipart/form-data')
|
145
|
+
|
146
|
+
Net::HTTP::UploadProgress.new(request) do |progress|
|
147
|
+
yield progress.upload_size - last_upload_size if block_given?
|
148
|
+
last_upload_size = progress.upload_size
|
149
|
+
end
|
150
|
+
|
151
|
+
http.request(request)
|
152
|
+
end
|
153
|
+
|
154
|
+
transform_response(response.body, version: 0)
|
155
|
+
end
|
106
156
|
end
|
107
157
|
end
|
@@ -5,8 +5,6 @@ module Toptranslation::Resource
|
|
5
5
|
|
6
6
|
def initialize(connection, options = {})
|
7
7
|
@connection = connection
|
8
|
-
@options = options
|
9
|
-
|
10
8
|
update_from_response(options)
|
11
9
|
end
|
12
10
|
|
@@ -25,12 +23,20 @@ module Toptranslation::Resource
|
|
25
23
|
@connection.get("/documents/#{@identifier}/download", params: params)['download_url']
|
26
24
|
end
|
27
25
|
|
28
|
-
def download(locale_code, options = {})
|
29
|
-
|
26
|
+
def download(locale_code, options = {}, &block)
|
27
|
+
download_path = if options[:path]
|
28
|
+
options[:path]
|
29
|
+
else
|
30
|
+
tempfile = Tempfile.new
|
31
|
+
temp_path = tempfile.path
|
32
|
+
tempfile.close
|
33
|
+
temp_path
|
34
|
+
end
|
35
|
+
@connection.download(download_url(locale_code, options), download_path, &block)
|
30
36
|
end
|
31
37
|
|
32
38
|
def save
|
33
|
-
response = @identifier
|
39
|
+
response = @connection.patch("/documents/#{@identifier}", remote_hash)
|
34
40
|
update_and_return_from_response(response)
|
35
41
|
end
|
36
42
|
|
@@ -40,14 +46,6 @@ module Toptranslation::Resource
|
|
40
46
|
|
41
47
|
private
|
42
48
|
|
43
|
-
def update_remote_document
|
44
|
-
@connection.patch("/documents/#{@identifier}", remote_hash)
|
45
|
-
end
|
46
|
-
|
47
|
-
def create_remote_document
|
48
|
-
@connection.post('/documents', remote_hash)
|
49
|
-
end
|
50
|
-
|
51
49
|
def update_and_return_from_response(response)
|
52
50
|
if response
|
53
51
|
update_from_response(response)
|
@@ -62,8 +60,8 @@ module Toptranslation::Resource
|
|
62
60
|
@string_count = response['string_count'] if response['string_count']
|
63
61
|
@has_missing_strings = response['has_missing_strings'] if response['has_missing_strings']
|
64
62
|
@sha1 = response['sha1'] if response['sha1']
|
65
|
-
@updated_at =
|
66
|
-
@created_at =
|
63
|
+
@updated_at = Time.parse(response['updated_at']) if response['updated_at']
|
64
|
+
@created_at = Time.parse(response['created_at']) if response['created_at']
|
67
65
|
if response['translations']
|
68
66
|
@translations = response['translations'].inject([]) do |accu, translation|
|
69
67
|
accu << Translation.new(@connection, translation)
|
@@ -10,8 +10,8 @@ module Toptranslation::Resource
|
|
10
10
|
update_from_response(options)
|
11
11
|
end
|
12
12
|
|
13
|
-
def upload_document(filepath, locale_code, options = {})
|
14
|
-
upload = Upload.new(@connection).upload(filepath)
|
13
|
+
def upload_document(filepath, locale_code, options = {}, &block)
|
14
|
+
upload = Upload.new(@connection).upload(filepath, &block)
|
15
15
|
|
16
16
|
attr_hash = {
|
17
17
|
document_store_id: upload.document_store_id,
|
@@ -8,8 +8,16 @@ module Toptranslation::Resource
|
|
8
8
|
update_from_response(options)
|
9
9
|
end
|
10
10
|
|
11
|
-
def download
|
12
|
-
|
11
|
+
def download(options = {})
|
12
|
+
download_path = if options[:path]
|
13
|
+
options[:path]
|
14
|
+
else
|
15
|
+
tempfile = Tempfile.new
|
16
|
+
temp_path = tempfile.path
|
17
|
+
tempfile.close
|
18
|
+
temp_path
|
19
|
+
end
|
20
|
+
@connection.download(download_url, download_path)
|
13
21
|
end
|
14
22
|
|
15
23
|
def download_url
|
@@ -7,9 +7,9 @@ module Toptranslation::Resource
|
|
7
7
|
@options = options
|
8
8
|
end
|
9
9
|
|
10
|
-
def upload(filepath, type = 'document')
|
10
|
+
def upload(filepath, type = 'document', &block)
|
11
11
|
puts "# Uploading: #{filepath}" if @connection.verbose
|
12
|
-
response = @connection.upload(filepath, type)
|
12
|
+
response = @connection.upload(filepath, type, &block)
|
13
13
|
|
14
14
|
@document_store_id = response['identifier']
|
15
15
|
@document_token = response['document_token']
|
data/lib/toptranslation_api.rb
CHANGED
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.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toptranslation GmbH
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -24,6 +24,20 @@ 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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rest-client
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +122,20 @@ dependencies:
|
|
108
122
|
- - "~>"
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: 1.25.1
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: vcr
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '4.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '4.0'
|
111
139
|
- !ruby/object:Gem::Dependency
|
112
140
|
name: webmock
|
113
141
|
requirement: !ruby/object:Gem::Requirement
|