Class: LokaliseRails::TaskDefinition::Importer

Inherits:
Base
  • Object
show all
Defined in:
lib/lokalise_rails/task_definition/importer.rb

Class Method Summary collapse

Methods inherited from Base

check_required_opts

Class Method Details

.download_filesObject



17
18
19
20
21
22
# File 'lib/lokalise_rails/task_definition/importer.rb', line 17

def download_files
  client = ::Lokalise.client LokaliseRails.api_token
  opts = LokaliseRails.import_opts

  client.download_files LokaliseRails.project_id, opts
end

.import!Object



7
8
9
10
11
12
13
14
15
# File 'lib/lokalise_rails/task_definition/importer.rb', line 7

def import!
  status_ok, msg = check_required_opts
  return msg unless status_ok
  return 'Task cancelled!' unless proceed_when_safe_mode?

  open_and_process_zip download_files['bundle_url']

  'Task complete!'
end

.open_and_process_zip(path) ⇒ Object



24
25
26
# File 'lib/lokalise_rails/task_definition/importer.rb', line 24

def open_and_process_zip(path)
  Zip::File.open_buffer(URI.open(path)) { |zip| process_zip zip }
end

.proceed_when_safe_mode?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/lokalise_rails/task_definition/importer.rb', line 40

def proceed_when_safe_mode?
  return true unless LokaliseRails.import_safe_mode && !Dir.empty?(LokaliseRails.locales_path.to_s)

  $stdout.puts "The target directory #{LokaliseRails.locales_path} is not empty!"
  $stdout.print 'Enter Y to continue: '
  answer = $stdin.gets
  answer.to_s.strip == 'Y'
end

.process_zip(zip) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lokalise_rails/task_definition/importer.rb', line 28

def process_zip(zip)
  zip.each do |entry|
    next unless /\.ya?ml/.match?(entry.name)

    filename = entry.name.include?('/') ? entry.name.split('/')[1] : entry.name
    data = YAML.safe_load entry.get_input_stream.read
    File.open("#{LokaliseRails.locales_path}/#{filename}", 'w+:UTF-8') do |f|
      f.write(data.to_yaml)
    end
  end
end