Class: TerrImporter::Application::Importer

Inherits:
Object
  • Object
show all
Includes:
ImporterHelper
Defined in:
lib/terrimporter/importer.rb

Overview

todo split importer into css_importer, image_importer, module_importer, js_importer

Constant Summary

Constants included from ImporterHelper

ImporterHelper::CSS_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImporterHelper

#file_contains_valid_css?, #replace_line!

Constructor Details

#initialize(options = {}) ⇒ Importer

Returns a new instance of Importer.



8
9
10
11
12
13
# File 'lib/terrimporter/importer.rb', line 8

def initialize(options = {})
  self.options = options
  loader = ConfigurationLoader.new(options[:config_file])
  self.config = loader.load_configuration
  initialize_downloader
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#import_cssObject



44
45
46
47
48
49
50
51
52
53
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
# File 'lib/terrimporter/importer.rb', line 44

def import_css
  LOG.info("Importing stylesheets")

  unclean_suffix = "_unclean"
  stylesheets = config.stylesheets

  stylesheets.each do |css|
    file_path = File.join(config.stylesheets_destination, css)
    options = {}
    options[:suffix] = $1 if css =~ /(ie.*).css$/ #add ie option if in array
    source_url = export_path(:css, options)
    unclean_file_path = file_path + unclean_suffix;
    constructed_file_path = (config.replace_style_strings? ? unclean_file_path : file_path)
    @downloader.download(source_url, constructed_file_path)
    STAT.add(:css)
    if file_contains_valid_css?(constructed_file_path)
      if config.replace_style_strings?
        LOG.info "CSS line replacements"
        File.open(file_path, 'w') do |d|
          File.open(constructed_file_path, 'r') do |s|
            lines = s.readlines
            lines.each do |line|
              d.print replace_stylesheet_lines!(line)
            end
          end
        end
      else
        LOG.debug "Skipping css line replacements"
      end

      if File.exists?(unclean_file_path)
        LOG.debug "Deleting unclean css files"
        FileUtils.remove unclean_file_path
      end
    else
      FileUtils.remove(file_path)
      LOG.debug "Deleting empty"
    end
  end
end

#import_imagesObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/terrimporter/importer.rb', line 121

def import_images

  if config.has_images?
    LOG.info "Import images"
    config.images.each do |image|
      image_source_path = File.join(config.images_server_path, image['server_path'])
      @downloader.batch_download(image_source_path, image['destination_path'], image['file_types'], :image)
    end
  else
    LOG.debug "Skipping image import"
  end
end

#import_jsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/terrimporter/importer.rb', line 85

def import_js
  LOG.info("Importing javascripts")
  file_path = File.join(config.javascripts_destination, "base.js")
  js_source_url = export_path(:js)
  LOG.debug "Import base.js from #{js_source_url} to #{file_path}"
  @downloader.download(js_source_url, file_path)
  STAT.add(:js)
  if config.has_dynamic_javascripts?
    if config.libraries_server_path.nil?
      LOG.info "Define 'libraries_server_path' in configuration file"
    else
      libraries_file_path = config.libraries_destination_path
      LOG.info "Import libraries from #{config.libraries_server_path} to #{libraries_file_path}"
      js_libraries = config.dynamic_libraries
      js_libraries.each do |lib|
        @downloader.download(File.join(config.libraries_server_path, lib), File.join(libraries_file_path, lib))
        STAT.add(:js)
      end
    end
  end

  if config.has_dynamic_plugins?
    if config.plugins_server_path.nil?
      LOG.info "Define 'plugins_server_path' in configuration file"
    else
    plugins_file_path = config.plugins_destination_path
    LOG.info "Import plugins from #{config.plugins_server_path} to #{plugins_file_path}"
    js_plugins = config.dynamic_plugins
    js_plugins.each do |lib|
      @downloader.download(File.join(config.plugins_server_path, lib), File.join(plugins_file_path, lib))
      STAT.add(:js)
    end
      end
  end
end

#import_modulesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/terrimporter/importer.rb', line 134

def import_modules

  if config.has_modules?
    LOG.info "Module import"
    config.modules.each do |mod|
      name = mod['name']
      skin = mod['skin']
      module_source_url = module_path(name, mod['module_template'], skin, mod['template_only'])
      filename = name.clone
      filename << "_#{skin}" unless skin.to_s.strip.length == 0
      @downloader.download(module_source_url, File.join(mod['destination_path'], filename + '.html'))
      STAT.add(:module)
    end
  else
    LOG.debug "Skipping module import"
  end
end

#initialize_downloaderObject



15
16
17
# File 'lib/terrimporter/importer.rb', line 15

def initialize_downloader
  @downloader = Downloader.new(config.application_url)
end

#module_path(name, module_template, skin = nil, template = nil) ⇒ Object

Raises:



152
153
154
155
156
157
158
159
# File 'lib/terrimporter/importer.rb', line 152

def module_path(name, module_template, skin = nil, template = nil)
  skin = '' if skin.nil?
  raise ConfigurationError, "Name cannot be empty for template" if name.nil?
  raise ConfigurationError, "Module template missing in configuration for template #{name}" if module_template.nil?
  export_path = config.application_url.clone
  export_path << "/terrific/module/details/#{name}/#{module_template}/#{skin}/format/module#{"content" if template}"
  export_path
end

#runObject



19
20
21
22
23
24
25
# File 'lib/terrimporter/importer.rb', line 19

def run
  if options[:all] != nil and options[:all] == true
    run_all_imports
  else
    run_specific_imports
  end
end

#run_all_importsObject



27
28
29
30
31
32
33
# File 'lib/terrimporter/importer.rb', line 27

def run_all_imports
  LOG.info "Import everything"
  import_js
  import_css
  import_images
  import_modules
end

#run_specific_importsObject



35
36
37
38
39
40
41
42
# File 'lib/terrimporter/importer.rb', line 35

def run_specific_imports
  options.each do |option, value|
    if option.to_s =~ /^import_/ and value == true
      LOG.info "Import of #{option.to_s.split('_').last} started"
      self.send option.to_s
    end
  end
end