web_translate_it 3.2.3 → 3.4.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: 660b650bae69f2f4a4282acb69b7abc72296b7e352660adaf3c1047a3bac04ce
4
- data.tar.gz: acf8cc2ef71f6a897d400cd99793b86c33d28672b8aa6049b49d1f3b3a2f3c16
3
+ metadata.gz: d01e00c6188bc51a7520d8a712f988f14125e606fd27a8cd7a8d1576075a433d
4
+ data.tar.gz: 5c1e552315ce4837b6505f2af0e1f911f4a625bdfcc3011f432502766e8c21b3
5
5
  SHA512:
6
- metadata.gz: dc29fee26eb5eb394705c81975adcf21f03dbe3ca7918d0e9a622a791ef0163ee9c7aa00f88d9c608eea462b5a75e8b9e4c7d530af276cfa3449ab7824ef53ec
7
- data.tar.gz: 148e6238f00cb1cd2030a8a9f13c1b155ed82c26baac67f6933d86da68463998a8f91f144339f8d538512fe7eee0ec47cab15b23a2de8bdab27df48f77176b76
6
+ metadata.gz: 6a13defd7a2f234d187d496a4da810b56abd06a98a07458e9110ca81be81aa93388c5d65f7804a5ed7fcacea0d2ddc617d2bc1be66c6baf57d98fa545710bf4f
7
+ data.tar.gz: 888436899b2d2f0e00c734f9bbb20a1692f9b4a3d1aa1cb10ce100215bc3b5d362d19176f2c5855baa70ce312adf5ee5415c489a6ee71ba4ebb0a292266120d4
data/bin/wti CHANGED
@@ -46,6 +46,7 @@ when 'pull'
46
46
  opt :all, 'Pull all files'
47
47
  opt :force, 'Force pull (bypass conditional requests to WTI)'
48
48
  opt :threads, 'Number of threads for parallel downloads', type: :integer, default: 10
49
+ opt :zip, 'Download the project as a zip archive instead of one request per file'
49
50
  opt :config, 'Path to a configuration file', short: '-c', default: '.wti'
50
51
  opt :debug, 'Display debug information'
51
52
  end
data/history.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## Version 3.4.0 / 2026-11-04
2
+
3
+ * **The project API key is now sent in an `Authorization: Bearer` header instead of in the URL.** Requests go to `/api/projects/current/...` rather than `/api/projects/<api key>/...`, so the key no longer ends up in proxy, load balancer and server logs. webtranslateit.com accepts the header since 2026-11-04. Earlier `wti` versions keep working: the API still accepts the key in the URL.
4
+ * Caution: `wti --debug` prints the raw HTTP exchange through Net::HTTP, which now includes the `Authorization` header and therefore your API key. Don't paste debug output into tickets or CI logs.
5
+ * `wti init` keeps the API token out of `.wti` when `WTI_PROJECT_API_KEY` is set: the generated file reads `api_key: <%= ENV['WTI_PROJECT_API_KEY'] %>`, so it can be committed safely. Without parameters, `wti init` uses the token from the environment instead of prompting for it. Otherwise `wti init` still writes the token, and now warns when `.wti` is inside a git repository and not ignored. It also warns when `WTI_PROJECT_API_KEY` is set to a different token than the one passed, since the environment takes precedence.
6
+ * Document `WTI_PROJECT_API_KEY` and ERB in `.wti` in the readme.
7
+
8
+ ## Version 3.3.0 / 2026-09-17
9
+
10
+ * Add `--zip` option to `wti pull`. It downloads the language files through the zip file endpoint: one request per locale instead of one request per file, and a single request when the whole project is pulled. Much faster on projects with many files, for instance when pulling translations in a build. Files already up to date are left alone, only the locales needing an update are requested, and an archive which fails to download fails its own files instead of aborting the pull. #473
11
+ * **`wti pull` now reports download and write failures in its exit code.** `Commands::Base#with_connection` returned the connection rather than the block's value, so `pull` discarded its per-file results and always exited `0`. Builds which were silently green on a partially failed pull will start failing.
12
+ * Support json 3. `multi_json` 1.19 passes parse options positionally, which json 3 rejects, so API error messages came back as raw JSON. Replace `multi_json` with the `json` gem, which ships with Ruby, and run CI against both json 2 and json 3.
13
+
1
14
  ## Version 3.2.3 / 2026-04-09
2
15
 
3
16
  * Handle HTTP 429 (rate limit) errors: add `RateLimitError` class, retry with backoff respecting the `Retry-After` header, and fix garbled error messages when the response body is not JSON.
@@ -19,7 +19,7 @@ module WebTranslateIt
19
19
 
20
20
  def self.find_all(connection, params = {}) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
21
21
  params = params.transform_keys(&:to_s)
22
- url = "/api/projects/#{connection.api_key}/#{resource_path}"
22
+ url = "#{Connection::PROJECT_PATH}/#{resource_path}"
23
23
  url += "?#{HashUtil.to_params(filter_params(params))}" unless params.empty?
24
24
 
25
25
  Concurrency.with_retries do
@@ -43,7 +43,7 @@ module WebTranslateIt
43
43
 
44
44
  def self.find(connection, id)
45
45
  Concurrency.with_retries do
46
- response = connection.get("/api/projects/#{connection.api_key}/#{resource_path}/#{id}")
46
+ response = connection.get("#{Connection::PROJECT_PATH}/#{resource_path}/#{id}")
47
47
  return nil if response.code.to_i == 404
48
48
 
49
49
  record = new(JSON.parse(response.body), connection: connection)
@@ -66,17 +66,17 @@ module WebTranslateIt
66
66
 
67
67
  def delete
68
68
  Concurrency.with_retries do
69
- HttpResponse.handle_response(connection.delete("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}"))
69
+ HttpResponse.handle_response(connection.delete("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}"))
70
70
  end
71
71
  end
72
72
 
73
- def translation_for(locale) # rubocop:todo Metrics/AbcSize
73
+ def translation_for(locale)
74
74
  translation = translations.detect { |t| t.locale == locale }
75
75
  return translation if translation
76
76
  return nil if new_record
77
77
 
78
78
  Concurrency.with_retries do
79
- response = HttpResponse.handle_response(connection.get("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}/locales/#{locale}/translations"))
79
+ response = HttpResponse.handle_response(connection.get("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}/locales/#{locale}/translations"))
80
80
  json = JSON.parse(response)
81
81
  return nil if json.empty?
82
82
 
@@ -106,13 +106,13 @@ module WebTranslateIt
106
106
  end
107
107
 
108
108
  Concurrency.with_retries do
109
- HttpResponse.handle_response(connection.put("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}", body: to_json))
109
+ HttpResponse.handle_response(connection.put("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}", body: to_json))
110
110
  end
111
111
  end
112
112
 
113
113
  def create
114
114
  Concurrency.with_retries do
115
- raw = connection.post("/api/projects/#{connection.api_key}/#{self.class.resource_path}", body: to_json(with_translations: true))
115
+ raw = connection.post("#{Connection::PROJECT_PATH}/#{self.class.resource_path}", body: to_json(with_translations: true))
116
116
  response = JSON.parse(HttpResponse.handle_response(raw))
117
117
  self.id = response['id']
118
118
  self.new_record = false
@@ -123,7 +123,7 @@ module WebTranslateIt
123
123
  def to_json(*_args, with_translations: false)
124
124
  hash = to_hash
125
125
  hash['translations'] = translations.map(&:to_hash) if translations.any? && with_translations
126
- MultiJson.dump(hash)
126
+ JSON.generate(hash)
127
127
  end
128
128
 
129
129
  private
@@ -29,8 +29,12 @@ module WebTranslateIt
29
29
  exit 1
30
30
  end
31
31
 
32
- def with_connection(&block)
33
- WebTranslateIt::Connection.new(configuration.api_key, &block)
32
+ # Yields a connection and returns the block's value. `Connection.new`
33
+ # returns the connection itself, so the block's value has to be captured.
34
+ def with_connection
35
+ result = nil
36
+ WebTranslateIt::Connection.new(configuration.api_key) { |conn| result = yield conn }
37
+ result
34
38
  end
35
39
 
36
40
  def run_hook(hook_command, label)
@@ -6,12 +6,15 @@ module WebTranslateIt
6
6
 
7
7
  class Init < Base
8
8
 
9
+ ENV_API_KEY = 'WTI_PROJECT_API_KEY'
10
+
9
11
  def call
10
12
  puts '# Initializing project'
11
13
  api_key, path = read_parameters
12
14
  ensure_directory_exists(path)
13
15
  project_info = JSON.parse(WebTranslateIt::Project.fetch_info(api_key))['project']
14
16
  write_configuration(path, api_key, project_info)
17
+ warn_if_key_committable(path) unless key_from_env?(api_key)
15
18
  print_success(project_info)
16
19
  true
17
20
  end
@@ -20,12 +23,46 @@ module WebTranslateIt
20
23
 
21
24
  def read_parameters
22
25
  if parameters.any?
26
+ warn_env_takes_precedence if env_api_key && parameters[0] != env_api_key
23
27
  [parameters[0], '.wti']
28
+ elsif env_api_key
29
+ puts " Using the Project API Key from #{ENV_API_KEY}."
30
+ [env_api_key, Prompt.ask(' Path to configuration file:', '.wti')]
24
31
  else
25
32
  [Prompt.ask(' Project API Key:'), Prompt.ask(' Path to configuration file:', '.wti')]
26
33
  end
27
34
  end
28
35
 
36
+ def env_api_key
37
+ ENV.fetch(ENV_API_KEY, nil).to_s.strip.then { |key| key.empty? ? nil : key }
38
+ end
39
+
40
+ # The key came from the environment, so the configuration file can read it
41
+ # from there too and never hold the secret itself.
42
+ def key_from_env?(api_key)
43
+ !env_api_key.nil? && api_key == env_api_key
44
+ end
45
+
46
+ def warn_env_takes_precedence
47
+ puts StringUtil.important(" Warning: #{ENV_API_KEY} is set and takes precedence over the key in the configuration file.")
48
+ end
49
+
50
+ # A `.wti` holding the raw key is safe on disk but not in git history. Only
51
+ # warn inside a git work tree, where the file is about to be committable.
52
+ def warn_if_key_committable(path)
53
+ return unless git?('rev-parse', '--is-inside-work-tree')
54
+ return if git?('check-ignore', '-q', path)
55
+
56
+ puts ''
57
+ puts StringUtil.important(" Warning: `#{path}` contains your Project API Key and is not ignored by git.")
58
+ puts " Add `#{path}` to .gitignore, or keep the key out of the file: set #{ENV_API_KEY}"
59
+ puts " and use `api_key: <%= ENV['#{ENV_API_KEY}'] %>` in `#{path}`."
60
+ end
61
+
62
+ def git?(*args)
63
+ system('git', *args, out: File::NULL, err: File::NULL) == true
64
+ end
65
+
29
66
  def print_success(project_info)
30
67
  puts ''
31
68
  puts " The project #{project_info['name']} was successfully initialized."
@@ -71,12 +108,17 @@ module WebTranslateIt
71
108
  puts ''
72
109
  end
73
110
 
111
+ def api_key_line(api_key)
112
+ key_from_env?(api_key) ? "<%= ENV['#{ENV_API_KEY}'] %>" : api_key
113
+ end
114
+
74
115
  def generate_configuration(api_key, project_info)
75
116
  <<~FILE
76
117
  # Required - The Project API Token from WebTranslateIt.com
77
118
  # More information: https://github.com/webtranslateit/webtranslateit/wiki#configuration-file
119
+ # The #{ENV_API_KEY} environment variable, when set, takes precedence over this value.
78
120
 
79
- api_key: #{api_key}
121
+ api_key: #{api_key_line(api_key)}
80
122
 
81
123
  # Optional - Locales not to sync with WebTranslateIt.
82
124
  # Takes a string, a symbol, or an array of string or symbol.
@@ -13,7 +13,9 @@ module WebTranslateIt
13
13
  if files.empty?
14
14
  puts 'No files to pull.'
15
15
  else
16
- complete_success = pull_files(files)
16
+ # `command_options[:zip]`, not `.zip`: Optimist's option hash falls back
17
+ # to `method_missing`, and `Enumerable#zip` gets there first.
18
+ complete_success = command_options[:zip] ? pull_archives(files) : pull_files(files)
17
19
  run_hook(configuration.after_pull, 'after_pull')
18
20
  end
19
21
  complete_success
@@ -43,6 +45,78 @@ module WebTranslateIt
43
45
  results.all?
44
46
  end
45
47
 
48
+ # Pull through the zip file endpoint: one request returns every language
49
+ # file of the project, which beats one request per file when a project has
50
+ # many files or many locales.
51
+ def pull_archives(files)
52
+ time = Time.now
53
+ outdated = files.select { |file| file.outdated?(command_options.force) }
54
+ locales = archive_locales(outdated)
55
+ saved = save_archives(outdated, locales)
56
+ results = files.map { |file| saved.fetch(file) { file.skipped } }
57
+ report_archives(results, locales.size, Time.now - time)
58
+ end
59
+
60
+ def report_archives(results, requests, elapsed) # rubocop:todo Naming/PredicateMethod
61
+ results.each { |result| print StringUtil.array_to_columns(result.output) }
62
+ puts "Pulled #{results.size} files in #{elapsed.round(1)}s, using #{requests} archive request(s)."
63
+ results.all?(&:success)
64
+ end
65
+
66
+ # The zip file endpoint serves either the whole project or a single
67
+ # locale, so ask for the whole project when every locale is wanted and
68
+ # fall back to one request per locale otherwise.
69
+ def archive_locales(files)
70
+ return [] if files.empty?
71
+
72
+ locales = files.map(&:locale).uniq
73
+ (configuration.target_locales + [configuration.source_locale] - locales).empty? ? [nil] : locales
74
+ end
75
+
76
+ # Returns the result of writing each file, keyed by file.
77
+ def save_archives(files, locales)
78
+ return {} if locales.empty?
79
+
80
+ by_path = files.to_h { |file| [file.file_path, file] }
81
+ results, = Concurrency.concurrent_batch(locales, batch_size: 1, max_threads: command_options.threads) do |batch|
82
+ with_connection { |conn| batch.flat_map { |locale| save_archive(conn, locale, by_path) } }
83
+ end
84
+ report_missing(files, results.to_h)
85
+ end
86
+
87
+ # An archive which can't be downloaded or read — an unknown locale, a
88
+ # server error, a truncated response — fails the files it was carrying
89
+ # rather than aborting the whole pull and leaving the other threads to be
90
+ # killed mid-write.
91
+ def save_archive(connection, locale, by_path)
92
+ extract(Project.fetch_zip(connection, locale: locale), by_path)
93
+ rescue StandardError => e
94
+ by_path.each_value.select { |file| locale.nil? || file.locale == locale }
95
+ .map { |file| [file, file.failed("An error occured: #{e.message}")] }
96
+ end
97
+
98
+ def extract(archive, by_path)
99
+ Tempfile.create(['wti-pull', '.zip']) do |tempfile|
100
+ tempfile.binmode
101
+ tempfile.write(archive)
102
+ tempfile.close
103
+ Zip::File.open(tempfile.path) { |zip| zip.filter_map { |entry| save_entry(entry, by_path) } }
104
+ end
105
+ end
106
+
107
+ def save_entry(entry, by_path)
108
+ # rubyzip hands entry names back as binary strings, which never match a
109
+ # path holding an accented or non-Latin character.
110
+ file = by_path[entry.name.dup.force_encoding(Encoding::UTF_8)]
111
+ [file, file.save(entry.get_input_stream(&:read))] if file
112
+ end
113
+
114
+ # A file listed by the project but absent from the archive was deleted
115
+ # between the two requests. Report it rather than silently skipping it.
116
+ def report_missing(files, saved)
117
+ saved.merge(files.difference(saved.keys).to_h { |file| [file, file.save(nil)] })
118
+ end
119
+
46
120
  def fetch_locales # rubocop:todo Metrics/AbcSize
47
121
  locales = if command_options.locale
48
122
  warn_unknown_locales(command_options.locale.split)
@@ -95,7 +95,7 @@ module WebTranslateIt
95
95
 
96
96
  # Convenience method which returns the endpoint for fetching a list of locales for a project.
97
97
  def api_url
98
- "/api/projects/#{api_key}"
98
+ Connection::PROJECT_PATH
99
99
  end
100
100
 
101
101
  # Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
@@ -4,6 +4,9 @@ module WebTranslateIt
4
4
 
5
5
  class Connection
6
6
 
7
+ # The API resolves `current` to the project the bearer token belongs to.
8
+ PROJECT_PATH = '/api/projects/current'
9
+
7
10
  @debug = false
8
11
 
9
12
  class << self
@@ -43,6 +46,7 @@ module WebTranslateIt
43
46
  yield self if block_given?
44
47
  end
45
48
 
49
+ # Caution: Net::HTTP's debug stream prints request headers, including Authorization.
46
50
  def self.turn_debug_on
47
51
  @debug = true
48
52
  end
@@ -68,6 +72,7 @@ module WebTranslateIt
68
72
  def api_request(method_class, path, body: nil)
69
73
  request = method_class.new(path)
70
74
  HttpResponse.add_fields(request)
75
+ request['Authorization'] = "Bearer #{api_key}"
71
76
  request.body = body if body
72
77
  yield request if block_given?
73
78
  http_connection.request(request)
@@ -7,13 +7,13 @@ module WebTranslateIt
7
7
  def self.fetch_info(api_key)
8
8
  Concurrency.with_retries do
9
9
  WebTranslateIt::Connection.new(api_key) do |conn|
10
- return HttpResponse.handle_response(conn.get("/api/projects/#{api_key}"))
10
+ return HttpResponse.handle_response(conn.get(Connection::PROJECT_PATH))
11
11
  end
12
12
  end
13
13
  end
14
14
 
15
15
  def self.fetch_stats(api_key, file_id = nil)
16
- url = file_id.nil? ? "/api/projects/#{api_key}/stats" : "/api/projects/#{api_key}/stats?file=#{file_id}"
16
+ url = file_id.nil? ? "#{Connection::PROJECT_PATH}/stats" : "#{Connection::PROJECT_PATH}/stats?file=#{file_id}"
17
17
  Concurrency.with_retries do
18
18
  WebTranslateIt::Connection.new(api_key) do |conn|
19
19
  return HttpResponse.handle_response(conn.get(url))
@@ -21,16 +21,26 @@ module WebTranslateIt
21
21
  end
22
22
  end
23
23
 
24
+ # Fetch every language file of a project in a single request, as a zip
25
+ # archive. Pass a locale to narrow the archive down to that locale.
26
+ def self.fetch_zip(connection, locale: nil)
27
+ url = "#{Connection::PROJECT_PATH}/zip_file"
28
+ url += "?locale=#{URI.encode_www_form_component(locale)}" if locale
29
+ Concurrency.with_retries do
30
+ HttpResponse.handle_response(connection.get(url))
31
+ end
32
+ end
33
+
24
34
  def self.create_locale(connection, locale_code)
25
35
  Concurrency.with_retries do
26
- response = connection.post("/api/projects/#{connection.api_key}/locales") { |req| req.set_form_data({'id' => locale_code}, ';') }
36
+ response = connection.post("#{Connection::PROJECT_PATH}/locales") { |req| req.set_form_data({'id' => locale_code}, ';') }
27
37
  HttpResponse.handle_response(response)
28
38
  end
29
39
  end
30
40
 
31
41
  def self.delete_locale(connection, locale_code)
32
42
  Concurrency.with_retries do
33
- HttpResponse.handle_response(connection.delete("/api/projects/#{connection.api_key}/locales/#{locale_code}"))
43
+ HttpResponse.handle_response(connection.delete("#{Connection::PROJECT_PATH}/locales/#{locale_code}"))
34
44
  end
35
45
  end
36
46
 
@@ -43,7 +43,7 @@ module WebTranslateIt
43
43
 
44
44
  def update
45
45
  Concurrency.with_retries do
46
- HttpResponse.handle_response(connection.put("/api/projects/#{connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}", body: to_json))
46
+ HttpResponse.handle_response(connection.put("#{Connection::PROJECT_PATH}/terms/#{id}/locales/#{locale}/translations/#{id}", body: to_json))
47
47
  end
48
48
  end
49
49
 
@@ -22,7 +22,7 @@ module WebTranslateIt
22
22
  end
23
23
 
24
24
  def to_json(*_args)
25
- MultiJson.dump(to_hash)
25
+ JSON.generate(to_hash)
26
26
  end
27
27
 
28
28
  protected
@@ -30,7 +30,7 @@ module WebTranslateIt
30
30
  def assign_attributes(_params); end
31
31
 
32
32
  def translation_path
33
- "/api/projects/#{connection.api_key}/#{self.class.parent_resource_path}/#{parent_id}/locales/#{locale}/translations"
33
+ "#{Connection::PROJECT_PATH}/#{self.class.parent_resource_path}/#{parent_id}/locales/#{locale}/translations"
34
34
  end
35
35
 
36
36
  end
@@ -50,28 +50,44 @@ module WebTranslateIt
50
50
  # file.fetch # returns nothing, with a status 304 Not Modified
51
51
  # file.fetch(true) # force to re-download the file, will return the content of the file with a 200 OK
52
52
  #
53
- def fetch(connection, force = false) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
54
- display = []
55
- if fresh
56
- display.push(file_path)
57
- else
58
- display.push("*#{file_path}")
53
+ def fetch(connection, force = false)
54
+ return skipped unless outdated?(force)
55
+
56
+ make_directory
57
+ with_display(display_columns) do
58
+ response = connection.get(api_url)
59
+ File.open(file_path, 'wb') { |file| file << response.body } if response.code.to_i == 200
60
+ response
59
61
  end
60
- display.push "#{StringUtil.checksumify(local_checksum.to_s)}..#{StringUtil.checksumify(remote_checksum.to_s)}"
61
- if !File.exist?(file_path) || force || (remote_checksum != local_checksum)
62
-
63
- dir = File.dirname(file_path)
64
- FileUtils.mkpath(dir) unless File.exist?(file_path) || dir == '.'
65
- with_display(display) do
66
- response = connection.get(api_url)
67
- File.open(file_path, 'wb') { |file| file << response.body } if response.code.to_i == 200
68
- response
69
- end
62
+ end
70
63
 
71
- else
72
- display.push StringUtil.success('Skipped')
73
- Result.new(true, display)
74
- end
64
+ # Write a language file taken out of a project archive, as downloaded by
65
+ # `wti pull --zip`. `content` is nil when the archive didn't carry the file,
66
+ # which happens when it was deleted between listing the project and
67
+ # downloading the archive.
68
+ def save(content)
69
+ return failed('Missing from archive') if content.nil?
70
+
71
+ make_directory
72
+ File.open(file_path, 'wb') { |file| file << content }
73
+ Result.new(true, display_columns.push(StringUtil.success('OK')))
74
+ rescue StandardError => e
75
+ failed("An error occured: #{e.message}")
76
+ end
77
+
78
+ # The result of a file which could not be written.
79
+ def failed(message)
80
+ Result.new(false, display_columns.push(StringUtil.failure(message)))
81
+ end
82
+
83
+ # Whether the local file is missing or differs from the copy on WebTranslateIt.
84
+ def outdated?(force = false)
85
+ !File.exist?(file_path) || force || remote_checksum != local_checksum
86
+ end
87
+
88
+ # The result of leaving an already up-to-date file alone.
89
+ def skipped
90
+ Result.new(true, display_columns.push(StringUtil.success('Skipped')))
75
91
  end
76
92
 
77
93
  def fetch_remote_content(connection)
@@ -172,15 +188,15 @@ module WebTranslateIt
172
188
 
173
189
  # Convenience method which returns the URL of the API endpoint for a locale.
174
190
  def api_url
175
- "/api/projects/#{api_key}/files/#{id}/locales/#{locale}"
191
+ "#{Connection::PROJECT_PATH}/files/#{id}/locales/#{locale}"
176
192
  end
177
193
 
178
194
  def api_url_for_create
179
- "/api/projects/#{api_key}/files"
195
+ "#{Connection::PROJECT_PATH}/files"
180
196
  end
181
197
 
182
198
  def api_url_for_delete
183
- "/api/projects/#{api_key}/files/#{id}"
199
+ "#{Connection::PROJECT_PATH}/files/#{id}"
184
200
  end
185
201
 
186
202
  def local_checksum
@@ -191,6 +207,18 @@ module WebTranslateIt
191
207
 
192
208
  private
193
209
 
210
+ # The first two columns of a pull or push line: the file path, prefixed with
211
+ # a star when its translations are not up to date, and the local and remote
212
+ # checksums.
213
+ def display_columns
214
+ [fresh ? file_path : "*#{file_path}", "#{StringUtil.checksumify(local_checksum.to_s)}..#{StringUtil.checksumify(remote_checksum.to_s)}"]
215
+ end
216
+
217
+ def make_directory
218
+ dir = File.dirname(file_path)
219
+ FileUtils.mkpath(dir) unless File.exist?(file_path) || dir == '.'
220
+ end
221
+
194
222
  def with_display(display)
195
223
  Concurrency.with_retries do
196
224
  response = yield
@@ -54,7 +54,7 @@ module WebTranslateIt
54
54
  end
55
55
 
56
56
  def self.error_message(response)
57
- MultiJson.load(response.body)['error']
57
+ JSON.parse(response.body)['error']
58
58
  rescue StandardError
59
59
  response.body.to_s
60
60
  end
@@ -8,9 +8,10 @@ require 'net/http'
8
8
  require 'net/https'
9
9
  require 'openssl'
10
10
  require 'uri'
11
- require 'multi_json'
12
11
  require 'digest/sha1'
13
12
  require 'English'
13
+ require 'tempfile'
14
+ require 'zip'
14
15
 
15
16
  require 'web_translate_it/connection'
16
17
  require 'web_translate_it/util'
data/man/wti.1.ron CHANGED
@@ -45,7 +45,17 @@ These options can be used to change this behaviour:
45
45
  The Web Translate It API use HTTP caching to be efficient, and check if your file needs
46
46
  to be updated by checking its modification date against the project’s latest activity.
47
47
  Use this option to bypass this check.
48
-
48
+
49
+ * `--zip`:
50
+ Pull the language files as zip archives, one request per locale instead of one
51
+ request per file. Pulling the whole project, with `--all` and no ignored or needed
52
+ locales configured, takes a single request. Much faster on projects with many files.
53
+ Files already up to date are left alone, and only the locales needing an update
54
+ are requested.
55
+
56
+ * `--threads`:
57
+ Number of parallel downloads. Defaults to 10.
58
+
49
59
  You may additionally ask for help:
50
60
 
51
61
  * `-h`, `--help`:
data/readme.md CHANGED
@@ -61,6 +61,18 @@ Check `wti --help` for help.
61
61
 
62
62
  If you’d like to specify another path for your configuration file, you can use `wti init`. This command will ask you to enter your project API token and where to save the configuration file (by default it will create a `.wti` in your project root directory).
63
63
 
64
+ ### Keeping the API token out of `.wti`
65
+
66
+ `wti init` writes the API token into `.wti`. That's fine on your machine, but if you commit `.wti`, the token ends up in your repository's history. `wti init` warns you when `.wti` isn't in your `.gitignore`.
67
+
68
+ To commit `.wti` without the token, set the `WTI_PROJECT_API_KEY` environment variable. It always takes precedence over the `api_key` in `.wti`. If it's set when you run `wti init`, the generated file reads the token from the environment instead of storing it:
69
+
70
+ ``` yaml
71
+ api_key: <%= ENV['WTI_PROJECT_API_KEY'] %>
72
+ ```
73
+
74
+ `.wti` is read through ERB, so you can point `api_key` at any environment variable this way. In CI, store the token as a secret and expose it as `WTI_PROJECT_API_KEY`.
75
+
64
76
  Now you’re all set and you can use the `wti` commands on your project.
65
77
 
66
78
  ## Using on multiple projects
@@ -177,6 +189,10 @@ Append `--help` for each command for more information. For instance:
177
189
  <td>wti pull --force</td>
178
190
  <td>Force pull (to bypass WebTranslateIt’s HTTP caching)</td>
179
191
  </tr>
192
+ <tr>
193
+ <td>wti pull --zip</td>
194
+ <td>Download the language files as zip archives: one request per locale instead of one per file, or a single request when the whole project is pulled. Much faster on projects with many files</td>
195
+ </tr>
180
196
  <tr>
181
197
  <td>wti addlocale fr</td>
182
198
  <td>Add a new locale to the project</td>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_translate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.3
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Briere
@@ -10,19 +10,25 @@ cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
- name: multi_json
13
+ name: json
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '0'
18
+ version: '2.5'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '4'
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
22
25
  requirements:
23
26
  - - ">="
24
27
  - !ruby/object:Gem::Version
25
- version: '0'
28
+ version: '2.5'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '4'
26
32
  - !ruby/object:Gem::Dependency
27
33
  name: optimist
28
34
  requirement: !ruby/object:Gem::Requirement
@@ -37,6 +43,26 @@ dependencies:
37
43
  - - "~>"
38
44
  - !ruby/object:Gem::Version
39
45
  version: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubyzip
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '2.3'
53
+ - - "<"
54
+ - !ruby/object:Gem::Version
55
+ version: '4'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '2.3'
63
+ - - "<"
64
+ - !ruby/object:Gem::Version
65
+ version: '4'
40
66
  description: A Command Line Interface tool to push and pull language files to WebTranslateIt.com.
41
67
  email: support@webtranslateit.com
42
68
  executables:
@@ -113,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
139
  - !ruby/object:Gem::Version
114
140
  version: '0'
115
141
  requirements: []
116
- rubygems_version: 4.0.6
142
+ rubygems_version: 4.0.20
117
143
  specification_version: 4
118
144
  summary: A CLI tool to sync locale files with WebTranslateIt.com.
119
145
  test_files: []