lokalise_rails 1.0.0 → 1.3.1

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: 87524123a10425d69c33950f4b2c16366146f5c1ab14c19dbc7185de61fb726a
4
- data.tar.gz: 39ba0b0a91f443ea162d7804c89906f4523f7c13411fbd541624bafb9197a569
3
+ metadata.gz: 2e85d26a390d153b8deabe38694c1b7313fca351232066d58918828388dcc7fc
4
+ data.tar.gz: 0ee837ad7426ed5570badcec88245de2f4d35ea27b3c821f3facb5922083b2b2
5
5
  SHA512:
6
- metadata.gz: 4e36f385c123ab80402aaa428973c2c0706b2cbceabade8bcf12689fe0955bdc927a223c27a9114c102d83af6a709b061e1c10d2e189ca832b5b56a0a6b043f8
7
- data.tar.gz: bbbe1857d99cdc7c3595f611d664c555981fbdf5f1d37c30a52cb179d641902f879b1492996dc7c1c1cb6dacdfbd7c2a3ca226f31a687879fddf59b1e8f9fe7a
6
+ metadata.gz: 4af0fd091968510911787fcdc7bd7ffdefada590fb48c4137106de5d3d34296890a868b65debbf2262fc0d82901eb77a35407e3d6a7c5f958babbb2b548b14cd
7
+ data.tar.gz: ff6948bb092ab43e837b54de2d593d8e5db319ac872390aa335cb67802d198f11a30d58993d3d5e561a1f3ad823fe52ac7f55b1bcbe3de9a2ba09dfb0d41d99f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.1 (01-Apr-21)
4
+
5
+ * A bit better exception handling
6
+ * Update dependencies
7
+
8
+ ## 1.3.0 (02-Feb-21)
9
+
10
+ * Use ruby-lokalise-api v3
11
+
12
+ ## 1.2.0 (11-Nov-20)
13
+
14
+ * New option `translations_loader`
15
+ * New option `translations_converter`
16
+ * New option `lang_iso_inferer`
17
+
18
+ ## 1.1.0 (23-Oct-20)
19
+
20
+ * New option `branch`
21
+ * New option `timeouts`
22
+ * New method `.reset_api_client!` for the task definitions
23
+
24
+ ## 1.0.1 (14-Oct-20)
25
+
26
+ * Minor bug fixes and spec updates
27
+
3
28
  ## 1.0.0 (01-Oct-20)
4
29
 
5
30
  * Added export feature
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
1
  # LokaliseRails
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/lokalise_rails.svg)](https://badge.fury.io/rb/lokalise_rails)
4
- [![Build Status](https://travis-ci.org/bodrovis/lokalise_rails.svg?branch=master)](https://travis-ci.org/bodrovis/lokalise_rails)
3
+ ![Gem](https://img.shields.io/gem/v/lokalise_rails)
4
+ [![Build Status](https://travis-ci.com/bodrovis/lokalise_rails.svg?branch=master)](https://travis-ci.com/github/bodrovis/lokalise_rails)
5
5
  [![Test Coverage](https://codecov.io/gh/bodrovis/lokalise_rails/graph/badge.svg)](https://codecov.io/gh/bodrovis/lokalise_rails)
6
6
 
7
7
  This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails and allows to exchange translation files easily. It relies on [ruby-lokalise-api](https://lokalise.github.io/ruby-lokalise-api) to send APIv2 requests.
8
8
 
9
+ *If you would like to know how this gem was built, check out the ["How to create a Ruby gem" series at Lokalise blog](https://lokalise.com/blog/create-a-ruby-gem-basics/).*
10
+
9
11
  ## Getting started
10
12
 
11
13
  ### Requirements
@@ -62,6 +64,25 @@ To export translations from your Rails app to the specified Lokalise project, ru
62
64
  rails lokalise_rails:export
63
65
  ```
64
66
 
67
+ ## Running tasks programmatically
68
+
69
+ You can also run the import and export tasks from the Rails app:
70
+
71
+ ```ruby
72
+ require "#{Rails.root}/config/lokalise_rails.rb"
73
+
74
+ # Import the files:
75
+ result = LokaliseRails::TaskDefinition::Importer.import!
76
+ ```
77
+ `result` contains a boolean value with the result of the operation
78
+
79
+ ```ruby
80
+ # Export the files:
81
+ processes = LokaliseRails::TaskDefinition::Exporter.export!
82
+ ```
83
+
84
+ `processes` contains a list of [queued background processes](https://lokalise.github.io/ruby-lokalise-api/api/queued-processes).
85
+
65
86
  ## Configuration
66
87
 
67
88
  Options are specified in the `config/lokalise_rails.rb` file.
@@ -71,7 +92,8 @@ Options are specified in the `config/lokalise_rails.rb` file.
71
92
  * `api_token` (`string`, required) - Lokalise API token with read/write permissions.
72
93
  * `project_id` (`string`, required) - Lokalise project ID. You must have import/export permissions in the specified project.
73
94
  * `locales_path` (`string`) - path to the directory with your translation files. Defaults to `"#{Rails.root}/config/locales"`.
74
- * `file_ext_regexp` (`regexp`) - regular expression applied to file extensions to determine which files should be imported and exported. Defaults to `/\.ya?ml\z/i` (YAML files).
95
+ * `branch` (`string`) - Lokalise project branch to use. Defaults to `"master"`.
96
+ * `timeouts` (`hash`) - set [request timeouts for the Lokalise API client](https://lokalise.github.io/ruby-lokalise-api/additional_info/customization#setting-timeouts). By default, requests have no timeouts: `{open_timeout: nil, timeout: nil}`. Both values are in seconds.
75
97
 
76
98
  ### Import settings
77
99
 
@@ -111,6 +133,20 @@ en_US:
111
133
  c.skip_file_export = ->(file) { f.split[1].to_s.include?('fr') }
112
134
  ```
113
135
 
136
+ ### Settings to work with formats other than YAML
137
+
138
+ If your translation files are not in YAML format, you will need to adjust the following options:
139
+
140
+ * `file_ext_regexp` (`regexp`) - regular expression applied to file extensions to determine which files should be imported and exported. Defaults to `/\.ya?ml\z/i` (YAML files).
141
+ * `translations_loader` (`lambda` or `proc`) - loads translations data and makes sure they are valid before saving them to a translation file. Defaults to `->(raw_data) { YAML.safe_load raw_data }`. In the simplest case you may just return the data back, for example `-> (raw_data) { raw_data }`.
142
+ * `translations_converter` (`lambda` or `proc`) - converts translations data to a proper format before saving them to a translation file. Defaults to `->(raw_data) { raw_data.to_yaml }`. In the simplest case you may just return the data back, for example `-> (raw_data) { raw_data }`.
143
+ * `lang_iso_inferer` (`lambda` or `proc`) - infers language ISO code based on the translation file data before uploading it to Lokalise. Defaults to `->(data) { YAML.safe_load(data)&.keys&.first }`.
144
+
145
+ ## Running tests
146
+
147
+ 1. Copypaste `.env.example` file as `.env`. Put your Lokalise API token and project ID inside. The `.env` file is excluded from version control so your data is safe. All in all, we use pre-recorded VCR cassettes, so the actual API requests won’t be sent. However, providing at least some values is required.
148
+ 2. Run `rspec .`. Observe test results and code coverage.
149
+
114
150
  ## License
115
151
 
116
- Copyright (c) [Lokalise team](http://lokalise.com), [Ilya Bodrov](http://bodrovis.tech)
152
+ Copyright (c) [Lokalise team](http://lokalise.com), [Ilya Bodrov](http://bodrovis.tech). License type is [MIT](https://github.com/bodrovis/lokalise_rails/blob/master/LICENSE).
@@ -10,6 +10,12 @@ LokaliseRails.config do |c|
10
10
  # Provide a custom path to the directory with your translation files:
11
11
  # c.locales_path = "#{Rails.root}/config/locales"
12
12
 
13
+ # Provide a Lokalise project branch to use:
14
+ # c.branch = 'master'
15
+
16
+ # Provide request timeouts for the Lokalise API client:
17
+ # c.timeouts = {open_timeout: nil, timeout: nil}
18
+
13
19
  # Import options have the following defaults:
14
20
  # c.import_opts = {
15
21
  # format: 'yaml',
@@ -29,6 +35,16 @@ LokaliseRails.config do |c|
29
35
  # Provide additional file exclusion criteria for exports (by default, any file with the proper extension will be exported)
30
36
  # c.skip_file_export = ->(file) { file.split[1].to_s.include?('fr') }
31
37
 
32
- # Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
33
- # c.file_ext_regexp = /\.ya?ml\z/i
38
+ # Set the options below if you would like to work with format other than YAML
39
+ ## Regular expression to use when choosing the files to extract from the downloaded archive and upload to Lokalise
40
+ ## c.file_ext_regexp = /\.ya?ml\z/i
41
+
42
+ ## Load translations data and make sure they are valid:
43
+ ## c.translations_loader = ->(raw_data) { YAML.safe_load raw_data }
44
+
45
+ ## Convert translations data to a proper format:
46
+ ## c.translations_converter = ->(raw_data) { raw_data.to_yaml }
47
+
48
+ ## Infer language ISO code for the translation file:
49
+ ## c.lang_iso_inferer = ->(data) { YAML.safe_load(data)&.keys&.first }
34
50
  end
@@ -8,7 +8,8 @@ module LokaliseRails
8
8
  class << self
9
9
  attr_accessor :api_token, :project_id
10
10
  attr_writer :import_opts, :import_safe_mode, :export_opts, :locales_path,
11
- :file_ext_regexp, :skip_file_export
11
+ :file_ext_regexp, :skip_file_export, :branch, :timeouts,
12
+ :translations_loader, :translations_converter, :lang_iso_inferer
12
13
 
13
14
  # Main interface to provide configuration options for rake tasks
14
15
  def config
@@ -20,6 +21,16 @@ module LokaliseRails
20
21
  @locales_path || "#{Rails.root}/config/locales"
21
22
  end
22
23
 
24
+ # Project branch to use
25
+ def branch
26
+ @branch || 'master'
27
+ end
28
+
29
+ # Set request timeouts for the Lokalise API client
30
+ def timeouts
31
+ @timeouts || {}
32
+ end
33
+
23
34
  # Regular expression used to select translation files with proper extensions
24
35
  def file_ext_regexp
25
36
  @file_ext_regexp || /\.ya?ml\z/i
@@ -51,6 +62,20 @@ module LokaliseRails
51
62
  def skip_file_export
52
63
  @skip_file_export || ->(_) { false }
53
64
  end
65
+
66
+ def translations_loader
67
+ @translations_loader || ->(raw_data) { YAML.safe_load raw_data }
68
+ end
69
+
70
+ # Converts translations data to the proper format
71
+ def translations_converter
72
+ @translations_converter || ->(raw_data) { raw_data.to_yaml }
73
+ end
74
+
75
+ # Infers lang ISO for the given translation file
76
+ def lang_iso_inferer
77
+ @lang_iso_inferer || ->(data) { YAML.safe_load(data)&.keys&.first }
78
+ end
54
79
  end
55
80
  end
56
81
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rake'
4
-
5
3
  module LokaliseRails
6
4
  class Railtie < Rails::Railtie
7
5
  rake_tasks do
@@ -13,7 +13,13 @@ module LokaliseRails
13
13
  #
14
14
  # @return [Lokalise::Client]
15
15
  def api_client
16
- @api_client ||= ::Lokalise.client LokaliseRails.api_token
16
+ @api_client ||= ::Lokalise.client LokaliseRails.api_token, LokaliseRails.timeouts
17
+ end
18
+
19
+ # Resets API client
20
+ def reset_api_client!
21
+ Lokalise.reset_client!
22
+ @api_client = nil
17
23
  end
18
24
 
19
25
  # Checks task options
@@ -44,6 +50,13 @@ module LokaliseRails
44
50
  def subdir_and_filename_for(entry)
45
51
  Pathname.new(entry).split
46
52
  end
53
+
54
+ # Returns Lokalise project ID and branch, semicolumn separated
55
+ #
56
+ # @return [String]
57
+ def project_id_with_branch
58
+ "#{LokaliseRails.project_id}:#{LokaliseRails.branch}"
59
+ end
47
60
  end
48
61
  end
49
62
  end
@@ -14,13 +14,13 @@ module LokaliseRails
14
14
 
15
15
  if errors.any?
16
16
  errors.each { |e| $stdout.puts e }
17
- return false
17
+ return errors
18
18
  end
19
19
 
20
20
  queued_processes = []
21
21
  each_file do |full_path, relative_path|
22
22
  queued_processes << api_client.upload_file(
23
- LokaliseRails.project_id, opts(full_path, relative_path)
23
+ project_id_with_branch, opts(full_path, relative_path)
24
24
  )
25
25
  rescue StandardError => e
26
26
  $stdout.puts "Error while trying to upload #{full_path}: #{e.inspect}"
@@ -55,12 +55,10 @@ module LokaliseRails
55
55
  def opts(full_p, relative_p)
56
56
  content = File.read full_p
57
57
 
58
- lang_iso = YAML.safe_load(content)&.keys&.first
59
-
60
58
  initial_opts = {
61
59
  data: Base64.strict_encode64(content.strip),
62
60
  filename: relative_p,
63
- lang_iso: lang_iso
61
+ lang_iso: LokaliseRails.lang_iso_inferer.call(content)
64
62
  }
65
63
 
66
64
  initial_opts.merge LokaliseRails.export_opts
@@ -37,7 +37,7 @@ module LokaliseRails
37
37
  def download_files
38
38
  opts = LokaliseRails.import_opts
39
39
 
40
- api_client.download_files LokaliseRails.project_id, opts
40
+ api_client.download_files project_id_with_branch, opts
41
41
  rescue StandardError => e
42
42
  $stdout.puts "There was an error when trying to download files: #{e.inspect}"
43
43
  end
@@ -46,9 +46,11 @@ module LokaliseRails
46
46
  #
47
47
  # @param path [String]
48
48
  def open_and_process_zip(path)
49
- Zip::File.open_buffer(URI.open(path)) do |zip|
49
+ Zip::File.open_buffer(open_file_or_remote(path)) do |zip|
50
50
  fetch_zip_entries(zip) { |entry| process!(entry) }
51
51
  end
52
+ rescue StandardError => e
53
+ $stdout.puts "There was an error when trying to process the downloaded files: #{e.inspect}"
52
54
  end
53
55
 
54
56
  # Iterates over ZIP entries. Each entry may be a file or folder.
@@ -64,13 +66,13 @@ module LokaliseRails
64
66
 
65
67
  # Processes ZIP entry by reading its contents and creating the corresponding translation file
66
68
  def process!(zip_entry)
67
- data = YAML.safe_load zip_entry.get_input_stream.read
69
+ data = data_from zip_entry
68
70
  subdir, filename = subdir_and_filename_for zip_entry.name
69
71
  full_path = "#{LokaliseRails.locales_path}/#{subdir}"
70
72
  FileUtils.mkdir_p full_path
71
73
 
72
74
  File.open(File.join(full_path, filename), 'w+:UTF-8') do |f|
73
- f.write data.to_yaml
75
+ f.write LokaliseRails.translations_converter.call(data)
74
76
  end
75
77
  rescue StandardError => e
76
78
  $stdout.puts "Error when trying to process #{zip_entry&.name}: #{e.inspect}"
@@ -87,6 +89,24 @@ module LokaliseRails
87
89
  answer = $stdin.gets
88
90
  answer.to_s.strip == 'Y'
89
91
  end
92
+
93
+ # Opens a local file or a remote URL using the provided patf
94
+ #
95
+ # @return [String]
96
+ def open_file_or_remote(path)
97
+ parsed_path = URI.parse(path)
98
+ if parsed_path&.scheme&.include?('http')
99
+ parsed_path.open
100
+ else
101
+ File.open path
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ def data_from(zip_entry)
108
+ LokaliseRails.translations_loader.call zip_entry.get_input_stream.read
109
+ end
90
110
  end
91
111
  end
92
112
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LokaliseRails
4
- VERSION = '1.0.0'
4
+ VERSION = '1.3.1'
5
5
  end
@@ -23,22 +23,21 @@ Gem::Specification.new do |spec|
23
23
  spec.extra_rdoc_files = ['README.md']
24
24
  spec.require_paths = ['lib']
25
25
 
26
- spec.add_dependency 'ruby-lokalise-api', '~> 3.1'
26
+ spec.add_dependency 'ruby-lokalise-api', '~> 4.0'
27
27
  spec.add_dependency 'rubyzip', '~> 2.3'
28
28
 
29
- spec.add_development_dependency 'codecov', '~> 0.1'
29
+ spec.add_development_dependency 'codecov', '~> 0.2'
30
30
  spec.add_development_dependency 'dotenv', '~> 2.5'
31
31
  if ENV['TEST_RAILS_VERSION'].nil?
32
- spec.add_development_dependency 'rails', '~> 6.0.3'
32
+ spec.add_development_dependency 'rails', '~> 6.1.0'
33
33
  else
34
34
  spec.add_development_dependency 'rails', ENV['TEST_RAILS_VERSION'].to_s
35
35
  end
36
36
  spec.add_development_dependency 'rake', '~> 13.0'
37
37
  spec.add_development_dependency 'rspec', '~> 3.6'
38
- spec.add_development_dependency 'rspec-rails', '~> 4.0'
39
- spec.add_development_dependency 'rubocop', '~> 0.60'
38
+ spec.add_development_dependency 'rubocop', '~> 1.0'
40
39
  spec.add_development_dependency 'rubocop-performance', '~> 1.5'
41
- spec.add_development_dependency 'rubocop-rspec', '~> 1.37'
40
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.2.0'
42
41
  spec.add_development_dependency 'simplecov', '~> 0.16'
43
42
  spec.add_development_dependency 'vcr', '~> 6.0'
44
43
  end
@@ -17,9 +17,7 @@ Rails.application.configure do
17
17
 
18
18
  # Configure public file server for tests with Cache-Control for performance.
19
19
  config.public_file_server.enabled = true
20
- config.public_file_server.headers = {
21
- 'Cache-Control' => "public, max-age=#{1.hour.to_i}"
22
- }
20
+ config.public_file_server.headers = {'Cache-Control' => 'public, max-age=3600'}
23
21
 
24
22
  # Show full error reports and disable caching.
25
23
  config.consider_all_requests_local = true
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe LokaliseRails::TaskDefinition::Base do
4
+ specify '.reset_client!' do
5
+ expect(described_class.api_client).to be_an_instance_of(Lokalise::Client)
6
+ described_class.reset_api_client!
7
+ current_client = described_class.instance_variable_get '@api_client'
8
+ expect(current_client).to be_nil
9
+ end
10
+
11
+ specify '.project_id_with_branch!' do
12
+ result = described_class.send :project_id_with_branch
13
+ expect(result).to be_an_instance_of(String)
14
+ expect(result).to include(':master')
15
+ end
16
+
17
+ describe '.subdir_and_filename_for' do
18
+ it 'works properly for longer paths' do
19
+ path = 'my_path/is/here/file.yml'
20
+ result = described_class.send(:subdir_and_filename_for, path)
21
+ expect(result.length).to eq(2)
22
+ expect(result[0]).to be_an_instance_of(Pathname)
23
+ expect(result[0].to_s).to eq('my_path/is/here')
24
+ expect(result[1].to_s).to eq('file.yml')
25
+ end
26
+
27
+ it 'works properly for shorter paths' do
28
+ path = 'file.yml'
29
+ result = described_class.send(:subdir_and_filename_for, path)
30
+ expect(result.length).to eq(2)
31
+ expect(result[1]).to be_an_instance_of(Pathname)
32
+ expect(result[0].to_s).to eq('.')
33
+ expect(result[1].to_s).to eq('file.yml')
34
+ end
35
+ end
36
+
37
+ describe '.opt_errors' do
38
+ it 'returns an error when the API key is not set' do
39
+ allow(LokaliseRails).to receive(:api_token).and_return(nil)
40
+ errors = described_class.opt_errors
41
+
42
+ expect(LokaliseRails).to have_received(:api_token)
43
+ expect(errors.length).to eq(1)
44
+ expect(errors.first).to include('API token is not set')
45
+ end
46
+
47
+ it 'returns an error when the project_id is not set' do
48
+ allow_project_id nil do
49
+ errors = described_class.opt_errors
50
+
51
+ expect(errors.length).to eq(1)
52
+ expect(errors.first).to include('Project ID is not set')
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '.proper_ext?' do
58
+ it 'works properly with path represented as a string' do
59
+ path = 'my_path/here/file.yml'
60
+ expect(described_class.send(:proper_ext?, path)).to be true
61
+ end
62
+
63
+ it 'works properly with path represented as a pathname' do
64
+ path = Pathname.new 'my_path/here/file.json'
65
+ expect(described_class.send(:proper_ext?, path)).to be false
66
+ end
67
+ end
68
+
69
+ describe '.api_client' do
70
+ before(:all) { described_class.reset_api_client! }
71
+
72
+ after(:all) { described_class.reset_api_client! }
73
+
74
+ it 'is possible to set timeouts' do
75
+ allow(LokaliseRails).to receive(:timeouts).and_return({
76
+ open_timeout: 100,
77
+ timeout: 500
78
+ })
79
+
80
+ expect(described_class.api_client).to be_an_instance_of(Lokalise::Client)
81
+ expect(described_class.api_client.open_timeout).to eq(100)
82
+ expect(described_class.api_client.timeout).to eq(500)
83
+ end
84
+ end
85
+ end
@@ -18,6 +18,8 @@ describe LokaliseRails::TaskDefinition::Exporter do
18
18
 
19
19
  describe '.export!' do
20
20
  it 'sends a proper API request' do
21
+ allow_project_id
22
+
21
23
  process = VCR.use_cassette('upload_files') do
22
24
  described_class.export!
23
25
  end.first
@@ -26,16 +28,30 @@ describe LokaliseRails::TaskDefinition::Exporter do
26
28
  expect(process.status).to eq('queued')
27
29
  end
28
30
 
31
+ it 'sends a proper API request when a different branch is provided' do
32
+ allow_project_id
33
+ allow(LokaliseRails).to receive(:branch).and_return('develop')
34
+
35
+ process = VCR.use_cassette('upload_files_branch') do
36
+ described_class.export!
37
+ end.first
38
+
39
+ expect(LokaliseRails).to have_received(:branch)
40
+ expect(process.project_id).to eq(LokaliseRails.project_id)
41
+ expect(process.status).to eq('queued')
42
+ end
43
+
29
44
  it 'halts when the API key is not set' do
30
- expect(LokaliseRails).to receive(:api_token).and_return(nil)
31
- expect(described_class).not_to receive(:each_file)
45
+ allow(LokaliseRails).to receive(:api_token).and_return(nil)
46
+
32
47
  expect(-> { described_class.export! }).to output(/API token is not set/).to_stdout
48
+ expect(LokaliseRails).to have_received(:api_token)
33
49
  end
34
50
 
35
51
  it 'halts when the project_id is not set' do
36
- expect(LokaliseRails).to receive(:project_id).and_return(nil)
37
- expect(described_class).not_to receive(:each_file)
38
- expect(-> { described_class.export! }).to output(/Project ID is not set/).to_stdout
52
+ allow_project_id nil do
53
+ expect(-> { described_class.export! }).to output(/Project ID is not set/).to_stdout
54
+ end
39
55
  end
40
56
  end
41
57
 
@@ -60,13 +76,14 @@ describe LokaliseRails::TaskDefinition::Exporter do
60
76
  end
61
77
 
62
78
  it 'allows to redefine options' do
63
- expect(LokaliseRails).to receive(:export_opts).and_return({
64
- detect_icu_plurals: true,
65
- convert_placeholders: true
66
- })
79
+ allow(LokaliseRails).to receive(:export_opts).and_return({
80
+ detect_icu_plurals: true,
81
+ convert_placeholders: true
82
+ })
67
83
 
68
84
  resulting_opts = described_class.opts(path, relative_name)
69
85
 
86
+ expect(LokaliseRails).to have_received(:export_opts)
70
87
  expect(resulting_opts[:data]).to eq(base64content)
71
88
  expect(resulting_opts[:filename]).to eq(relative_name)
72
89
  expect(resulting_opts[:lang_iso]).to eq('en')
@@ -91,6 +108,8 @@ describe LokaliseRails::TaskDefinition::Exporter do
91
108
 
92
109
  describe '.export!' do
93
110
  it 'rescues from export errors' do
111
+ allow_project_id
112
+
94
113
  processes = VCR.use_cassette('upload_files_error') do
95
114
  described_class.export!
96
115
  end
@@ -129,7 +148,7 @@ describe LokaliseRails::TaskDefinition::Exporter do
129
148
  end
130
149
 
131
150
  it 'does not yield files that have to be skipped' do
132
- expect(LokaliseRails).to receive(:skip_file_export).twice.and_return(
151
+ allow(LokaliseRails).to receive(:skip_file_export).twice.and_return(
133
152
  ->(f) { f.split[1].to_s.include?('ru') }
134
153
  )
135
154
  expect { |b| described_class.each_file(&b) }.to yield_successive_args(
@@ -138,6 +157,8 @@ describe LokaliseRails::TaskDefinition::Exporter do
138
157
  Pathname.new(relative_name)
139
158
  ]
140
159
  )
160
+
161
+ expect(LokaliseRails).to have_received(:skip_file_export).twice
141
162
  end
142
163
  end
143
164
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe LokaliseRails::TaskDefinition::Importer do
4
+ describe '.open_and_process_zip' do
5
+ let(:faulty_trans) { "#{Rails.root}/public/faulty_trans.zip" }
6
+
7
+ it 'rescues from errors during file processing' do
8
+ expect(-> { described_class.open_and_process_zip(faulty_trans) }).
9
+ to output(/Psych::DisallowedClass/).to_stdout
10
+ end
11
+
12
+ it 'rescues from errors during file opening' do
13
+ expect(-> { described_class.open_and_process_zip('http://fake.url/wrong/path.zip') }).
14
+ to output(/SocketError/).to_stdout
15
+ end
16
+ end
17
+
18
+ describe '.download_files' do
19
+ it 'returns a proper download URL' do
20
+ allow_project_id '189934715f57a162257d74.88352370' do
21
+ response = VCR.use_cassette('download_files') do
22
+ described_class.download_files
23
+ end
24
+
25
+ expect(response['project_id']).to eq('189934715f57a162257d74.88352370')
26
+ expect(response['bundle_url']).to include('s3-eu-west-1.amazonaws.com')
27
+ end
28
+ end
29
+
30
+ it 'rescues from errors during file download' do
31
+ allow_project_id 'invalid'
32
+ VCR.use_cassette('download_files_error') do
33
+ expect(-> { described_class.download_files }).
34
+ to output(/Lokalise::Error::BadRequest/).to_stdout
35
+ end
36
+ end
37
+ end
38
+
39
+ describe '.import!' do
40
+ it 'halts when the API key is not set' do
41
+ allow(LokaliseRails).to receive(:api_token).and_return(nil)
42
+ expect(-> { described_class.import! }).to output(/API token is not set/).to_stdout
43
+ expect(LokaliseRails).to have_received(:api_token)
44
+ expect(count_translations).to eq(0)
45
+ end
46
+
47
+ it 'halts when the project_id is not set' do
48
+ allow_project_id nil do
49
+ expect(-> { described_class.import! }).to output(/Project ID is not set/).to_stdout
50
+ expect(count_translations).to eq(0)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -15,17 +15,17 @@ describe LokaliseRails do
15
15
  let(:fake_class) { class_double('LokaliseRails') }
16
16
 
17
17
  it 'is possible to set project_id' do
18
- expect(fake_class).to receive(:project_id=).with('123.abc')
18
+ allow(fake_class).to receive(:project_id=).with('123.abc')
19
19
  fake_class.project_id = '123.abc'
20
20
  end
21
21
 
22
22
  it 'is possible to set file_ext_regexp' do
23
- expect(fake_class).to receive(:file_ext_regexp=).with(Regexp.new('.*'))
23
+ allow(fake_class).to receive(:file_ext_regexp=).with(Regexp.new('.*'))
24
24
  fake_class.file_ext_regexp = Regexp.new('.*')
25
25
  end
26
26
 
27
27
  it 'is possible to set import_opts' do
28
- expect(fake_class).to receive(:import_opts=).with(duck_type(:each))
28
+ allow(fake_class).to receive(:import_opts=).with(duck_type(:each))
29
29
  fake_class.import_opts = {
30
30
  format: 'json',
31
31
  indentation: '8sp'
@@ -33,32 +33,63 @@ describe LokaliseRails do
33
33
  end
34
34
 
35
35
  it 'is possible to set export_opts' do
36
- expect(fake_class).to receive(:export_opts=).with(duck_type(:each))
36
+ allow(fake_class).to receive(:export_opts=).with(duck_type(:each))
37
37
  fake_class.export_opts = {
38
38
  convert_placeholders: true,
39
39
  detect_icu_plurals: true
40
40
  }
41
41
  end
42
42
 
43
+ it 'is possible to set branch' do
44
+ allow(fake_class).to receive(:branch=).with('custom')
45
+ fake_class.branch = 'custom'
46
+ end
47
+
48
+ it 'is possible to set timeouts' do
49
+ allow(fake_class).to receive(:timeouts=).with(duck_type(:each))
50
+ fake_class.timeouts = {
51
+ open_timeout: 100,
52
+ timeout: 500
53
+ }
54
+ end
55
+
43
56
  it 'is possible to set import_safe_mode' do
44
- expect(fake_class).to receive(:import_safe_mode=).with(true)
57
+ allow(fake_class).to receive(:import_safe_mode=).with(true)
45
58
  fake_class.import_safe_mode = true
46
59
  end
47
60
 
48
61
  it 'is possible to set api_token' do
49
- expect(fake_class).to receive(:api_token=).with('abc')
62
+ allow(fake_class).to receive(:api_token=).with('abc')
50
63
  fake_class.api_token = 'abc'
51
64
  end
52
65
 
53
66
  it 'is possible to override locales_path' do
54
- expect(fake_class).to receive(:locales_path=).with('/demo/path')
67
+ allow(fake_class).to receive(:locales_path=).with('/demo/path')
55
68
  fake_class.locales_path = '/demo/path'
56
69
  end
57
70
 
58
71
  it 'is possible to set skip_file_export' do
59
72
  cond = ->(f) { f.nil? }
60
- expect(fake_class).to receive(:skip_file_export=).with(cond)
73
+ allow(fake_class).to receive(:skip_file_export=).with(cond)
61
74
  fake_class.skip_file_export = cond
62
75
  end
76
+
77
+ it 'is possible to set translations_loader' do
78
+ runner = ->(f) { f.to_json }
79
+ allow(fake_class).to receive(:translations_loader=).with(runner)
80
+ fake_class.translations_loader = runner
81
+ end
82
+
83
+ it 'is possible to set translations_converter' do
84
+ runner = ->(f) { f.to_json }
85
+ allow(fake_class).to receive(:translations_converter=).with(runner)
86
+ fake_class.translations_converter = runner
87
+ end
88
+
89
+ it 'is possible to set lang_iso_inferer' do
90
+ runner = ->(f) { f.to_json }
91
+ allow(fake_class).to receive(:lang_iso_inferer=).with(runner)
92
+ fake_class.lang_iso_inferer = runner
93
+ end
63
94
  end
64
95
  end
@@ -18,7 +18,7 @@ RSpec.describe LokaliseRails do
18
18
  end
19
19
 
20
20
  it 'import rake task is callable' do
21
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
21
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
22
22
  :download_files
23
23
  ).and_return(
24
24
  {
@@ -30,14 +30,14 @@ RSpec.describe LokaliseRails do
30
30
  expect(import_executor).to output(/complete!/).to_stdout
31
31
 
32
32
  expect(count_translations).to eq(4)
33
-
33
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
34
34
  expect_file_exist loc_path, 'en/nested/main_en.yml'
35
35
  expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
36
36
  expect_file_exist loc_path, 'ru/main_ru.yml'
37
37
  end
38
38
 
39
39
  it 'import rake task downloads ZIP archive properly' do
40
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
40
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
41
41
  :download_files
42
42
  ).and_return(
43
43
  {
@@ -48,8 +48,8 @@ RSpec.describe LokaliseRails do
48
48
 
49
49
  expect(import_executor).to output(/complete!/).to_stdout
50
50
 
51
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
51
52
  expect(count_translations).to eq(4)
52
-
53
53
  expect_file_exist loc_path, 'en/nested/main_en.yml'
54
54
  expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
55
55
  expect_file_exist loc_path, 'ru/main_ru.yml'
@@ -73,7 +73,7 @@ RSpec.describe LokaliseRails do
73
73
  end
74
74
 
75
75
  it 'import proceeds when the user agrees' do
76
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
76
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
77
77
  :download_files
78
78
  ).and_return(
79
79
  {
@@ -81,21 +81,27 @@ RSpec.describe LokaliseRails do
81
81
  'bundle_url' => local_trans
82
82
  }
83
83
  )
84
- expect($stdin).to receive(:gets).and_return('Y')
84
+
85
+ allow($stdin).to receive(:gets).and_return('Y')
85
86
  expect(import_executor).to output(/is not empty/).to_stdout
86
87
 
87
88
  expect(count_translations).to eq(5)
88
-
89
+ expect($stdin).to have_received(:gets)
90
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
89
91
  expect_file_exist loc_path, 'en/nested/main_en.yml'
90
92
  expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
91
93
  expect_file_exist loc_path, 'ru/main_ru.yml'
92
94
  end
93
95
 
94
96
  it 'import halts when a user chooses not to proceed' do
95
- expect(LokaliseRails::TaskDefinition::Importer).not_to receive(:download_files)
96
- expect($stdin).to receive(:gets).and_return('N')
97
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
98
+ :download_files
99
+ ).at_most(0).times
100
+ allow($stdin).to receive(:gets).and_return('N')
97
101
  expect(import_executor).to output(/is not empty/).to_stdout
98
102
 
103
+ expect(LokaliseRails::TaskDefinition::Importer).not_to have_received(:download_files)
104
+ expect($stdin).to have_received(:gets)
99
105
  expect(count_translations).to eq(1)
100
106
  end
101
107
  end
data/spec/spec_helper.rb CHANGED
@@ -20,16 +20,13 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
20
20
 
21
21
  # Configure Rails Environment
22
22
  ENV['RAILS_ENV'] = 'test'
23
-
24
23
  require_relative '../spec/dummy/config/environment'
25
- # ActiveRecord::Migrator.migrations_paths = [File.expand_path('../spec/dummy/db/migrate', __dir__)]
26
24
  ENV['RAILS_ROOT'] ||= "#{File.dirname(__FILE__)}../../../spec/dummy"
27
25
 
28
- require 'rspec/rails'
29
-
30
26
  RSpec.configure do |config|
31
27
  config.include FileManager
32
28
  config.include RakeUtils
29
+ config.include SpecAddons
33
30
  end
34
31
 
35
32
  # rubocop:disable Style/MixinUsage
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpecAddons
4
+ def allow_project_id(value = ENV['LOKALISE_PROJECT_ID'])
5
+ allow(LokaliseRails).to receive(:project_id).and_return(value)
6
+ return unless block_given?
7
+
8
+ yield
9
+ expect(LokaliseRails).to have_received(:project_id)
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokalise_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-01 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-lokalise-api
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.1'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubyzip
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.1'
47
+ version: '0.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.1'
54
+ version: '0.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: dotenv
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 6.0.3
75
+ version: 6.1.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 6.0.3
82
+ version: 6.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,34 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.6'
111
- - !ruby/object:Gem::Dependency
112
- name: rspec-rails
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '4.0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '4.0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rubocop
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
115
  - - "~>"
130
116
  - !ruby/object:Gem::Version
131
- version: '0.60'
117
+ version: '1.0'
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
122
  - - "~>"
137
123
  - !ruby/object:Gem::Version
138
- version: '0.60'
124
+ version: '1.0'
139
125
  - !ruby/object:Gem::Dependency
140
126
  name: rubocop-performance
141
127
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +142,14 @@ dependencies:
156
142
  requirements:
157
143
  - - "~>"
158
144
  - !ruby/object:Gem::Version
159
- version: '1.37'
145
+ version: 2.2.0
160
146
  type: :development
161
147
  prerelease: false
162
148
  version_requirements: !ruby/object:Gem::Requirement
163
149
  requirements:
164
150
  - - "~>"
165
151
  - !ruby/object:Gem::Version
166
- version: '1.37'
152
+ version: 2.2.0
167
153
  - !ruby/object:Gem::Dependency
168
154
  name: simplecov
169
155
  requirement: !ruby/object:Gem::Requirement
@@ -242,20 +228,22 @@ files:
242
228
  - spec/dummy/config/routes.rb
243
229
  - spec/dummy/db/seeds.rb
244
230
  - spec/lib/generators/lokalise_rails/install_generator_spec.rb
245
- - spec/lib/lokalise_rails/exporter_spec.rb
246
- - spec/lib/lokalise_rails/importer_spec.rb
231
+ - spec/lib/lokalise_rails/task_definition/base_spec.rb
232
+ - spec/lib/lokalise_rails/task_definition/exporter_spec.rb
233
+ - spec/lib/lokalise_rails/task_definition/importer_spec.rb
247
234
  - spec/lib/lokalise_rails_spec.rb
248
235
  - spec/lib/tasks/export_task_spec.rb
249
236
  - spec/lib/tasks/import_task_spec.rb
250
237
  - spec/spec_helper.rb
251
238
  - spec/support/file_manager.rb
252
239
  - spec/support/rake_utils.rb
240
+ - spec/support/spec_addons.rb
253
241
  - spec/support/vcr.rb
254
242
  homepage: https://github.com/bodrovis/lokalise_rails
255
243
  licenses:
256
244
  - MIT
257
245
  metadata: {}
258
- post_install_message:
246
+ post_install_message:
259
247
  rdoc_options: []
260
248
  require_paths:
261
249
  - lib
@@ -270,8 +258,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
270
258
  - !ruby/object:Gem::Version
271
259
  version: '0'
272
260
  requirements: []
273
- rubygems_version: 3.1.4
274
- signing_key:
261
+ rubygems_version: 3.2.11
262
+ signing_key:
275
263
  specification_version: 4
276
264
  summary: Lokalise integration for Ruby on Rails
277
265
  test_files:
@@ -298,12 +286,14 @@ test_files:
298
286
  - spec/dummy/config/routes.rb
299
287
  - spec/dummy/db/seeds.rb
300
288
  - spec/lib/generators/lokalise_rails/install_generator_spec.rb
301
- - spec/lib/lokalise_rails/exporter_spec.rb
302
- - spec/lib/lokalise_rails/importer_spec.rb
289
+ - spec/lib/lokalise_rails/task_definition/base_spec.rb
290
+ - spec/lib/lokalise_rails/task_definition/exporter_spec.rb
291
+ - spec/lib/lokalise_rails/task_definition/importer_spec.rb
303
292
  - spec/lib/lokalise_rails_spec.rb
304
293
  - spec/lib/tasks/export_task_spec.rb
305
294
  - spec/lib/tasks/import_task_spec.rb
306
295
  - spec/spec_helper.rb
307
296
  - spec/support/file_manager.rb
308
297
  - spec/support/rake_utils.rb
298
+ - spec/support/spec_addons.rb
309
299
  - spec/support/vcr.rb
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe LokaliseRails::TaskDefinition::Importer do
4
- describe '.open_and_process_zip' do
5
- let(:faulty_trans) { "#{Rails.root}/public/faulty_trans.zip" }
6
-
7
- it 'rescues from errors during file processing' do
8
- expect(-> { described_class.open_and_process_zip(faulty_trans) }).
9
- to output(/Psych::DisallowedClass/).to_stdout
10
- end
11
- end
12
-
13
- describe '.download_files' do
14
- it 'returns a proper download URL' do
15
- expect(LokaliseRails).to receive(:project_id).and_return('189934715f57a162257d74.88352370')
16
- response = VCR.use_cassette('download_files') do
17
- described_class.download_files
18
- end
19
-
20
- expect(response['project_id']).to eq('189934715f57a162257d74.88352370')
21
- expect(response['bundle_url']).to include('s3-eu-west-1.amazonaws.com')
22
- end
23
-
24
- it 'rescues from errors during file download' do
25
- allow(LokaliseRails).to receive(:api_token).and_return('incorrect')
26
-
27
- VCR.use_cassette('download_files_error') do
28
- expect(-> { described_class.download_files }).
29
- to output(/Lokalise::Error::BadRequest/).to_stdout
30
- end
31
- end
32
- end
33
-
34
- describe '.import!' do
35
- it 'halts when the API key is not set' do
36
- expect(LokaliseRails).to receive(:api_token).and_return(nil)
37
- expect(-> {described_class.import!}).to output(/API token is not set/).to_stdout
38
- expect(count_translations).to eq(0)
39
- end
40
-
41
- it 'halts when the project_id is not set' do
42
- expect(LokaliseRails).to receive(:project_id).and_return(nil)
43
- expect(-> {described_class.import!}).to output(/Project ID is not set/).to_stdout
44
- expect(count_translations).to eq(0)
45
- end
46
- end
47
- end