Class: TerrImporter::Application::ConfigurationLoader

Inherits:
Object
  • Object
show all
Includes:
ConfigurationHelper
Defined in:
lib/terrimporter/configuration_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationHelper

#backup_config_file, #base_config_path, #config_default_name, #config_example_path, #config_search_paths, #config_working_directory_exists?, #config_working_directory_path, #create_config_file, #remove_config_file, #schema_default_name, #schema_file_path

Constructor Details

#initialize(config_file = nil) ⇒ ConfigurationLoader

Returns a new instance of ConfigurationLoader.



8
9
10
# File 'lib/terrimporter/configuration_loader.rb', line 8

def initialize(config_file = nil)
  self.config_file = config_file unless config_file.nil?
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



6
7
8
# File 'lib/terrimporter/configuration_loader.rb', line 6

def config_file
  @config_file
end

Instance Method Details

#determine_config_file_pathObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/terrimporter/configuration_loader.rb', line 22

def determine_config_file_path
  return self.config_file unless self.config_file.nil?

  config_search_paths.each do |path|
    file_path = File.join(path, config_default_name)
    #default config supplied with terrimporter NOT valid
    return file_path if File.exists?(file_path) and not file_path.include?(File.join('terrimporter', 'config'))
  end

  raise ConfigurationError, %Q{Configuration file #{config_default_name} not found in search paths. Search paths are:
  #{config_search_paths.join "\n"} \n If this is a new project, run with the option --init}
end

#determine_configuration_values_from_html(raw_html) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/terrimporter/configuration_loader.rb', line 54

def determine_configuration_values_from_html(raw_html)
  #todo this regex does it wrong, it extracts all references and can't decide between js and css. also the version is wrongly extracted if the base path isn't correct
  results = raw_html.scan(/(\/terrific\/base\/(.*?)\/public\/.*base.(css|js).php)\?.*application=(.*?)(&|&)/)
  results.uniq!

  css_result =results.select{|v| v[2] == "css"}.first
  js_result =results.select{|v| v[2] == "js"}.first


  raise ConfigurationError, "Unable to extract javascript information from application url, content is: #{raw_html}" if js_result.nil? or js_result.size < 5

  css_export_path = css_result[0]
  js_export_path = js_result[0]
  terrific_version = css_result[1]     #todo: if it looks like this tags/0.4.0 -> extract number, remove everything before and inclusive /
  application = css_result[3]

  case nil
    when css_export_path
      raise ConfigurationError, "Unable to determine css export path"
    when js_export_path
      raise ConfigurationError, "Unable to determine js export path"
    when terrific_version
      raise ConfigurationError, "Unable to determine terrific version"
    when application
      raise ConfigurationError, "Unable to determine application path"
  end

  LOG.info "Determined the following configuration values:\n" +
               "terrific version: #{terrific_version} \n" +
               "application path: #{application}"

  [terrific_version, application, css_export_path, js_export_path]
end

#load_configurationObject

todo also complete missing values here, after this step everything should work…



13
14
15
16
17
18
19
20
# File 'lib/terrimporter/configuration_loader.rb', line 13

def load_configuration
  config_file_path = determine_config_file_path
  LOG.debug "Configuration file located, load from #{config_file_path}"
  c = Configuration.new
  c.merge!(validate_and_load_config(config_file_path))
  c['version'], c['export_settings']['application'], c['css_export_path'], c['js_export_path'] = determine_configuration_values_from_html(Downloader.new(c['application_url']).download(''))
  c
end

#load_validatorObject



48
49
50
51
52
# File 'lib/terrimporter/configuration_loader.rb', line 48

def load_validator
  LOG.debug "Loading configuration file validator from #{schema_file_path}"
  schema = Kwalify::Yaml.load_file(schema_file_path)
  Kwalify::Validator.new(schema)
end

#validate_and_load_config(file) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/terrimporter/configuration_loader.rb', line 35

def validate_and_load_config(file)
  LOG.debug "Validate configuration file"
  parser = Kwalify::Yaml::Parser.new(load_validator)
  document = parser.parse_file(file)
  errors = parser.errors()

  if errors && !errors.empty?
    error_message = errors.inject("") { |result, e| result << "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n" }
    raise ConfigurationError, error_message
  end
  document
end