Class: Localeui::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/localeui/resources/source_file.rb

Class Method Summary collapse

Class Method Details

.allObject



94
95
96
97
98
# File 'lib/localeui/resources/source_file.rb', line 94

def self.all
  Dir.glob("#{Localeui.locales_path}/**/*")
     .select { |path| File.file?(path) }
     .map { |path| path.delete_prefix("#{Localeui.locales_path}/") }
end

.downloadObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/localeui/resources/source_file.rb', line 22

def self.download
  raise NoProjectError, 'No project initialised' if Localeui.project_id.nil?

  # TODO: think about, if we want to create an event

  # Request all source files
  files_response = files_to_download

  # Request all locales
  locales_response = locales_to_download

  # Request each source file with file content
  locales_response.body.each do |locale|
    files_response.body.each do |file|
      downloaded_file_response = download_file(file, locale)
      store_file(downloaded_file_response)
    end
  end
end

.download_file(file, locale) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/localeui/resources/source_file.rb', line 72

def self.download_file(file, locale)
  response = Localeui::Http.request(
    method: :get,
    endpoint: "projects/#{Localeui.project_id}/source_files/#{file['api_id']}?locale_id=#{locale['api_id']}"
  )
  Localeui::Utils.logger.info "File '#{response.body['name']}' has been successfully downloaded"
  response
end

.file_upload(file, event) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/localeui/resources/source_file.rb', line 81

def self.file_upload(file, event)
  file_path = [Localeui.locales_path, file].join('/')

  response = Localeui::Http.request(
    method: :post, endpoint: "projects/#{Localeui.project_id}/source_files", params: {
      filename: file, event_id: event.body['api_id'],
      content: YAML.load_file(file_path).to_json
    }
  )
  Localeui::Utils.logger.info "File '#{file}' has been successfully uploaded"
  response
end

.files_to_downloadObject



42
43
44
45
46
47
48
# File 'lib/localeui/resources/source_file.rb', line 42

def self.files_to_download
  response = Localeui::Http.request(
    method: :get, endpoint: "projects/#{Localeui.project_id}/source_files"
  )
  Localeui::Utils.logger.info "#{response.body.count} files will be downloaded"
  response
end

.locales_to_downloadObject



50
51
52
53
54
55
56
# File 'lib/localeui/resources/source_file.rb', line 50

def self.locales_to_download
  response = Localeui::Http.request(
    method: :get, endpoint: "projects/#{Localeui.project_id}/locales"
  )
  Localeui::Utils.logger.info "#{response.body.count} locale(s) available"
  response
end

.store_file(file) ⇒ Object

rubocop:disable Metrics/AbcSize



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/localeui/resources/source_file.rb', line 58

def self.store_file(file) # rubocop:disable Metrics/AbcSize
  if Rails.env.test?
    Localeui::Utils.logger.info "Skipping file storage for '#{file.body['name']}' in test environment"
    return
  end

  file_path = [Localeui.locales_path, file.body['name']].join('/')
  File.open file_path, 'w' do |f|
    f.write YAML.dump(file.body['content'])
    f.close
  end
  Localeui::Utils.logger.info "File '#{file.body['name']}' has been successfully stored under '#{file_path}'"
end

.uploadObject

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/localeui/resources/source_file.rb', line 5

def self.upload
  raise NoProjectError, 'No project initialised' if Localeui.project_id.nil?

  # Get all source files
  files = all

  # Create event
  event = Localeui::Event.create

  # Submit each file
  files.each do |file|
    file_upload(file, event)
  end

  { event: event.body['api_id'], files: files.count }
end