Class: WebTranslateIt::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/web_translate_it/configuration.rb

Overview

Handles the configuration of your project, both via the the configuration file and via the API. Implementation example, assuming you have a valid .wti file:

configuration = WebTranslateIt::Configuration.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = Rails.root, path_to_config_file = '.wti') ⇒ Configuration

Load configuration file from the path.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/web_translate_it/configuration.rb', line 16

def initialize(root_path = Rails.root, path_to_config_file = '.wti') # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  self.path_to_config_file = path_to_config_file
  self.path           = root_path
  self.logger         = logger
  if File.exist?(File.expand_path(path_to_config_file, path))
    self.api_key        = ENV.fetch('WTI_PROJECT_API_KEY') { configuration['api_key'] }
    self.before_pull    = configuration['before_pull']
    self.after_pull     = configuration['after_pull']
    self.before_push    = configuration['before_push']
    self.after_push     = configuration['after_push']
    self.ignore_files   = configuration['ignore_files']
    project_info = JSON.parse WebTranslateIt::Project.fetch_info(api_key)
    self.ignore_locales = locales_to_ignore(configuration)
    self.needed_locales = locales_needed(configuration)
    self.files = files_from_project(project_info['project'])
    self.source_locale = source_locale_from_project(project_info['project'])
    self.target_locales = target_locales_from_project(project_info['project'])
    self.project_name = project_info['project']['name']
  else
    puts StringUtil.failure("\nNo configuration file found in #{File.expand_path(path_to_config_file, path)}")
    exit(1)
  end
end

Instance Attribute Details

#after_pullObject

Returns the value of attribute after_pull.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def after_pull
  @after_pull
end

#after_pushObject

Returns the value of attribute after_push.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def after_push
  @after_push
end

#api_keyObject

Returns the value of attribute api_key.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def api_key
  @api_key
end

#before_pullObject

Returns the value of attribute before_pull.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def before_pull
  @before_pull
end

#before_pushObject

Returns the value of attribute before_push.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def before_push
  @before_push
end

#filesObject

Returns the value of attribute files.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def files
  @files
end

#ignore_filesObject

Returns the value of attribute ignore_files.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def ignore_files
  @ignore_files
end

#ignore_localesObject

Returns the value of attribute ignore_locales.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def ignore_locales
  @ignore_locales
end

#loggerObject

Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.



102
103
104
# File 'lib/web_translate_it/configuration.rb', line 102

def logger
  @logger
end

#needed_localesObject

Returns the value of attribute needed_locales.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def needed_locales
  @needed_locales
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def path
  @path
end

#path_to_config_fileObject

Returns the value of attribute path_to_config_file.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def path_to_config_file
  @path_to_config_file
end

#project_nameObject

Returns the value of attribute project_name.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def project_name
  @project_name
end

#source_localeObject

Returns the value of attribute source_locale.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def source_locale
  @source_locale
end

#target_localesObject

Returns the value of attribute target_locales.



13
14
15
# File 'lib/web_translate_it/configuration.rb', line 13

def target_locales
  @target_locales
end

Instance Method Details

#api_urlObject

Convenience method which returns the endpoint for fetching a list of locales for a project.



97
98
99
# File 'lib/web_translate_it/configuration.rb', line 97

def api_url
  "/api/projects/#{api_key}"
end

#configurationObject



110
111
112
# File 'lib/web_translate_it/configuration.rb', line 110

def configuration
  @configuration ||= YAML.safe_load(parse_erb_in_configuration)
end

#files_from_project(project) ⇒ Object

Set the project files from the Project API. Implementation example:

configuration = WebTranslateIt::Configuration.new
files = configuration.files # returns an array of TranslationFile


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/web_translate_it/configuration.rb', line 67

def files_from_project(project) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  array_files = []
  project['project_files'].each do |project_file|
    if project_file['name'].nil? || (project_file['name'].strip == '')
      puts "File #{project_file['id']} not set up"
    elsif ignore_files&.any? { |glob| File.fnmatch(glob, project_file['name']) }
      puts "Ignoring #{project_file['name']}"
    else
      array_files.push TranslationFile.new(project_file['id'], project_file['name'], project_file['locale_code'], api_key, project_file['updated_at'], project_file['hash_file'], project_file['master_project_file_id'], project_file['fresh'])
    end
  end
  array_files
end

#locales_needed(configuration) ⇒ Object

Returns an array of locales to specifically pull from the configuration file, if set



87
88
89
# File 'lib/web_translate_it/configuration.rb', line 87

def locales_needed(configuration)
  Array(configuration['needed_locales']).map(&:to_s)
end

#locales_to_ignore(configuration) ⇒ Object

Returns an array of locales to ignore from the configuration file, if set.



82
83
84
# File 'lib/web_translate_it/configuration.rb', line 82

def locales_to_ignore(configuration)
  Array(configuration['ignore_locales']).map(&:to_s)
end

#reloadObject

Reload project data



42
43
44
45
46
47
48
49
50
# File 'lib/web_translate_it/configuration.rb', line 42

def reload # rubocop:todo Metrics/AbcSize
  project_info = JSON.parse WebTranslateIt::Project.fetch_info(api_key)
  self.ignore_locales = locales_to_ignore(configuration)
  self.needed_locales = locales_needed(configuration)
  self.files = files_from_project(project_info['project'])
  self.source_locale = source_locale_from_project(project_info['project'])
  self.target_locales = target_locales_from_project(project_info['project'])
  self.project_name = project_info['project']['name']
end

#source_locale_from_project(project) ⇒ Object

Returns the source locale from the Project API.



53
54
55
# File 'lib/web_translate_it/configuration.rb', line 53

def source_locale_from_project(project)
  project['source_locale']['code']
end

#target_locales_from_project(project) ⇒ Object

Returns the target locales from the Project API.



58
59
60
# File 'lib/web_translate_it/configuration.rb', line 58

def target_locales_from_project(project)
  project['target_locales'].map { |locale| locale['code'] }
end