Class: WebTranslateIt::Configuration
- Inherits:
-
Object
- Object
- WebTranslateIt::Configuration
- 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
-
#after_pull ⇒ Object
Returns the value of attribute after_pull.
-
#after_push ⇒ Object
Returns the value of attribute after_push.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#before_pull ⇒ Object
Returns the value of attribute before_pull.
-
#before_push ⇒ Object
Returns the value of attribute before_push.
-
#files ⇒ Object
Returns the value of attribute files.
-
#ignore_locales ⇒ Object
Returns the value of attribute ignore_locales.
-
#logger ⇒ Object
Returns a logger.
-
#needed_locales ⇒ Object
Returns the value of attribute needed_locales.
-
#path ⇒ Object
Returns the value of attribute path.
-
#path_to_config_file ⇒ Object
Returns the value of attribute path_to_config_file.
-
#project_name ⇒ Object
Returns the value of attribute project_name.
-
#source_locale ⇒ Object
Returns the value of attribute source_locale.
-
#target_locales ⇒ Object
Returns the value of attribute target_locales.
Instance Method Summary collapse
-
#api_url ⇒ Object
Convenience method which returns the endpoint for fetching a list of locales for a project.
- #configuration ⇒ Object
-
#initialize(root_path = Rails.root, path_to_config_file = ".wti") ⇒ Configuration
constructor
Load configuration file from the path.
-
#reload ⇒ Object
Reload project data.
-
#set_files(project) ⇒ Object
Set the project files from the Project API.
-
#set_locales(project) ⇒ Object
Set the project locales from the Project API.
-
#set_locales_needed(configuration) ⇒ Object
Set locales to specifically pull from the configuration file, if set.
-
#set_locales_to_ignore(configuration) ⇒ Object
Set locales to ignore from the configuration file, if set.
Constructor Details
#initialize(root_path = Rails.root, path_to_config_file = ".wti") ⇒ Configuration
Load configuration file from the path.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/web_translate_it/configuration.rb', line 18 def initialize(root_path = Rails.root, path_to_config_file = ".wti") self.path_to_config_file = path_to_config_file self.path = root_path self.logger = logger if File.exists?(File.(path_to_config_file, self.path)) self.api_key = ENV["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'] project_info = if RUBY_VERSION >= 3.1 YAML.safe_load WebTranslateIt::Project.fetch_info(api_key), permitted_classes: [Time] else YAML.load WebTranslateIt::Project.fetch_info(api_key) end set_locales_to_ignore(configuration) set_locales_needed(configuration) set_files(project_info['project']) set_locales(project_info['project']) WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors'] self.project_name = project_info['project']['name'] else puts StringUtil.failure("\nNo configuration file found in #{File.(path_to_config_file, self.path)}") exit(1) end end |
Instance Attribute Details
#after_pull ⇒ Object
Returns the value of attribute after_pull.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def after_pull @after_pull end |
#after_push ⇒ Object
Returns the value of attribute after_push.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def after_push @after_push end |
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def api_key @api_key end |
#before_pull ⇒ Object
Returns the value of attribute before_pull.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def before_pull @before_pull end |
#before_push ⇒ Object
Returns the value of attribute before_push.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def before_push @before_push end |
#files ⇒ Object
Returns the value of attribute files.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def files @files end |
#ignore_locales ⇒ Object
Returns the value of attribute ignore_locales.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def ignore_locales @ignore_locales end |
#logger ⇒ Object
Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
99 100 101 |
# File 'lib/web_translate_it/configuration.rb', line 99 def logger @logger end |
#needed_locales ⇒ Object
Returns the value of attribute needed_locales.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def needed_locales @needed_locales end |
#path ⇒ Object
Returns the value of attribute path.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def path @path end |
#path_to_config_file ⇒ Object
Returns the value of attribute path_to_config_file.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def path_to_config_file @path_to_config_file end |
#project_name ⇒ Object
Returns the value of attribute project_name.
15 16 17 |
# File 'lib/web_translate_it/configuration.rb', line 15 def project_name @project_name end |
#source_locale ⇒ Object
Returns the value of attribute source_locale.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def source_locale @source_locale end |
#target_locales ⇒ Object
Returns the value of attribute target_locales.
14 15 16 |
# File 'lib/web_translate_it/configuration.rb', line 14 def target_locales @target_locales end |
Instance Method Details
#api_url ⇒ Object
Convenience method which returns the endpoint for fetching a list of locales for a project.
94 95 96 |
# File 'lib/web_translate_it/configuration.rb', line 94 def api_url "/api/projects/#{api_key}.yaml" end |
#configuration ⇒ Object
107 108 109 |
# File 'lib/web_translate_it/configuration.rb', line 107 def configuration @configuration ||= YAML.load(parse_erb_in_configuration) end |
#reload ⇒ Object
Reload project data
47 48 49 50 51 52 53 54 55 |
# File 'lib/web_translate_it/configuration.rb', line 47 def reload project_info = YAML.load WebTranslateIt::Project.fetch_info(self.api_key) set_locales_to_ignore(configuration) set_locales_needed(configuration) set_files(project_info['project']) set_locales(project_info['project']) WebTranslateIt::Connection.turn_silent_on if configuration['silence_errors'] self.project_name = project_info['project']['name'] end |
#set_files(project) ⇒ Object
Set the project files from the Project API. Implementation example:
configuration = WebTranslateIt::Configuration.new
files = configuration.files # returns an array of TranslationFile
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/web_translate_it/configuration.rb', line 72 def set_files(project) self.files = [] project['project_files'].each do |project_file| if project_file['name'].nil? or project_file['name'].strip == '' puts "File #{project_file['id']} not set up" else self.files.push TranslationFile.new(project_file['id'], project_file['name'], project_file['locale_code'], self.api_key, project_file['updated_at'], project_file['hash_file'], project_file['master_project_file_id'], project_file['fresh']) end end end |
#set_locales(project) ⇒ Object
Set the project locales from the Project API. Implementation example:
configuration = WebTranslateIt::Configuration.new
locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]
62 63 64 65 |
# File 'lib/web_translate_it/configuration.rb', line 62 def set_locales(project) self.source_locale = project['source_locale']['code'] self.target_locales = project['target_locales'].map{|locale| locale['code']} end |
#set_locales_needed(configuration) ⇒ Object
Set locales to specifically pull from the configuration file, if set
89 90 91 |
# File 'lib/web_translate_it/configuration.rb', line 89 def set_locales_needed(configuration) self.needed_locales = Array(configuration['needed_locales']).map{ |locale| locale.to_s } end |
#set_locales_to_ignore(configuration) ⇒ Object
Set locales to ignore from the configuration file, if set.
84 85 86 |
# File 'lib/web_translate_it/configuration.rb', line 84 def set_locales_to_ignore(configuration) self.ignore_locales = Array(configuration['ignore_locales']).map{ |locale| locale.to_s } end |