Class: Localeapp::CLI::Install::DefaultInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/localeapp/cli/install.rb

Direct Known Subclasses

HerokuInstaller, StandaloneInstaller

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, key_checker: Localeapp::KeyChecker.new) ⇒ DefaultInstaller

Returns a new instance of DefaultInstaller.



23
24
25
26
# File 'lib/localeapp/cli/install.rb', line 23

def initialize(output, key_checker: Localeapp::KeyChecker.new)
  @output       = output
  @key_checker  = key_checker
end

Instance Attribute Details

#config_file_pathObject

Returns the value of attribute config_file_path.



21
22
23
# File 'lib/localeapp/cli/install.rb', line 21

def config_file_path
  @config_file_path
end

#data_directoryObject

Returns the value of attribute data_directory.



21
22
23
# File 'lib/localeapp/cli/install.rb', line 21

def data_directory
  @data_directory
end

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/localeapp/cli/install.rb', line 21

def key
  @key
end

#project_dataObject

Returns the value of attribute project_data.



21
22
23
# File 'lib/localeapp/cli/install.rb', line 21

def project_data
  @project_data
end

Instance Method Details

#check_data_directory_existsObject



100
101
102
103
104
# File 'lib/localeapp/cli/install.rb', line 100

def check_data_directory_exists
  unless File.directory?(data_directory)
    @output.puts "WARNING: please create the #{data_directory} directory. Your translation data will be stored there."
  end
end

#check_key(key) ⇒ Object



106
107
108
# File 'lib/localeapp/cli/install.rb', line 106

def check_key(key)
  key_checker.check key
end

#execute(key = nil, **options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/localeapp/cli/install.rb', line 28

def execute(key = nil, **options)
  self.key = key
  print_header
  if validate_key
    print_default_locale
    set_config_paths
    @output.puts "Writing configuration file to #{config_file_path}"
    write_config_file
    if options[:write_env_file]
      write_env_file_apikey options[:write_env_file], key
    end
    check_data_directory_exists
    true
  else
    false
  end
end


69
70
71
72
73
74
75
76
# File 'lib/localeapp/cli/install.rb', line 69

def print_default_locale
  localeapp_default_code = project_data['default_locale']['code']
  @output.puts <<-eoh
Default Locale: #{localeapp_default_code} (#{project_data['default_locale']['name']})
Please ensure I18n.default_locale is #{localeapp_default_code} or change it in
config/application.rb
  eoh
end


46
47
48
49
# File 'lib/localeapp/cli/install.rb', line 46

def print_header
  @output.puts "Localeapp Install"
  @output.puts ""
end

#set_config_pathsObject



78
79
80
81
# File 'lib/localeapp/cli/install.rb', line 78

def set_config_paths
  @config_file_path = "config/initializers/localeapp.rb"
  @data_directory   = "config/locales"
end

#validate_keyObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/localeapp/cli/install.rb', line 51

def validate_key
  @output.puts "Checking API key: #{key}"
  if key.nil?
    @output.puts "ERROR: You must supply an API key"
    return false
  end

  valid_key, @project_data = check_key(key)
  if valid_key
    @output.puts "Success!"
    @output.puts "Project: #{project_data['name']}"
    true
  else
    @output.puts "ERROR: Project not found"
    false
  end
end

#write_config_fileObject



83
84
85
86
# File 'lib/localeapp/cli/install.rb', line 83

def write_config_file
  create_config_dir
  write_rails_config
end

#write_rails_configObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/localeapp/cli/install.rb', line 88

def write_rails_config
  File.open(config_file_path, 'w+') do |file|
    file.write <<-CONTENT
require 'localeapp/rails'

Localeapp.configure do |config|
  config.api_key = ENV['LOCALEAPP_API_KEY']
end
CONTENT
  end
end