toptranslation_api 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c90725ea66e01f1d6fa6fb551da1460fa29b8c250e5c42bd959512b7b08509b9
4
- data.tar.gz: 8f8420ffecc9e9fa6efb65f1aba26cf13fedcc0a81c93eb07dfcaf1b2029bc41
3
+ metadata.gz: 1c33323a6ceaeea5084a3bad1e038611b20986e2aa6fd79d1d5bf5b7cd38104b
4
+ data.tar.gz: ce619d7ceeb69ed269b56507e2fea4983a0862ac0bf42dabdc0a3a3908658ab9
5
5
  SHA512:
6
- metadata.gz: 01e933d2d8295b0395a9455852ea8b3e2e678828b24d4190bb9233243cca2fedbf732dd9d91df7c419d4f3fecf02542f389ff39a8b86bb3291e05448f5bcb533
7
- data.tar.gz: 3013cf1f325d7401b1e32e03a599c3ff828bcf8d5455729a902253161301ba568d469de410da604cc0ebd56b6afac4e82ed1d6d0be7443d8b19d2fe506a1fa81
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
- raw = RestClient::Request.execute(method: :get, url: url, raw_response: true)
27
- raw.file
26
+ uri = URI.parse(url)
27
+ download_uri(uri, path, &block)
28
28
  end
29
29
 
30
- def upload(filepath, type)
31
- response = RestClient.post(
32
- "#{@files_url}/documents",
33
- file: File.new(filepath),
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
- @connection.download(download_url(locale_code, options))
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 ? update_remote_document : create_remote_document
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 = DateTime.parse(response['updated_at']) if response['updated_at']
66
- @created_at = DateTime.parse(response['created_at']) if response['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
- @connection.download(download_url)
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']
@@ -1,3 +1,3 @@
1
1
  module Toptranslation
2
- VERSION = '2.2.0'.freeze
2
+ VERSION = '2.3.0'.freeze
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'rest-client'
3
+ require 'net/http/uploadprogress'
3
4
  require 'toptranslation/client'
4
5
  require 'toptranslation/connection'
5
6
  require 'toptranslation/resource/document'
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.2.0
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-14 00:00:00.000000000 Z
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