Class: Railspress::PostImportProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/models/railspress/post_import_processor.rb

Constant Summary collapse

MARKDOWN_EXTENSIONS =
%w[.md .markdown].freeze
TEXT_EXTENSIONS =
%w[.txt].freeze
ZIP_EXTENSIONS =
%w[.zip].freeze
IMAGE_EXTENSIONS =
%w[.jpg .jpeg .png .gif .webp].freeze
FRONTMATTER_REGEX =
/\A---\s*\n(.*?\n?)^---\s*\n/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(import:, file_path:) ⇒ PostImportProcessor

Returns a new instance of PostImportProcessor.



17
18
19
20
21
22
# File 'app/models/railspress/post_import_processor.rb', line 17

def initialize(import:, file_path:)
  @import = import
  @file_path = file_path
  @errors = []
  @extract_dir = nil
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'app/models/railspress/post_import_processor.rb', line 15

def errors
  @errors
end

#extract_dirObject (readonly)

Returns the value of attribute extract_dir.



15
16
17
# File 'app/models/railspress/post_import_processor.rb', line 15

def extract_dir
  @extract_dir
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



15
16
17
# File 'app/models/railspress/post_import_processor.rb', line 15

def file_path
  @file_path
end

#importObject (readonly)

Returns the value of attribute import.



15
16
17
# File 'app/models/railspress/post_import_processor.rb', line 15

def import
  @import
end

Instance Method Details

#process!Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/railspress/post_import_processor.rb', line 24

def process!
  import.mark_processing!

  process_file(file_path)

  if import.error_count > 0
    import.mark_failed! if import.success_count == 0
    import.mark_completed! if import.success_count > 0
  else
    import.mark_completed!
  end
end

#process_file(path, base_dir: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/railspress/post_import_processor.rb', line 37

def process_file(path, base_dir: nil)
  extension = File.extname(path).downcase

  if MARKDOWN_EXTENSIONS.include?(extension)
    process_markdown(path, base_dir: base_dir)
  elsif TEXT_EXTENSIONS.include?(extension)
    process_text(path, base_dir: base_dir)
  elsif ZIP_EXTENSIONS.include?(extension)
    process_zip(path)
  else
    import.add_error("Unsupported file type: #{File.basename(path)}")
  end
end