Class: BuntoImport::Importers::CSV

Inherits:
BuntoImport::Importer show all
Defined in:
lib/bunto-import/importers/csv.rb

Defined Under Namespace

Classes: CSVPost

Class Method Summary collapse

Methods inherited from BuntoImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object

Reads a csv with title, permalink, body, published_at, and filter. It creates a post file for each row in the csv



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bunto-import/importers/csv.rb', line 21

def self.process(options)
  file = options.fetch('file', "posts.csv")

  FileUtils.mkdir_p "_posts"
  posts = 0
  abort "Cannot find the file '#{file}'. Aborting." unless File.file?(file)

  ::CSV.foreach(file) do |row|
    next if row[0] == "title" # header
    posts += 1
    write_post(CSVPost.new(row), options)
  end
  Bunto.logger.info "Created #{posts} posts!"
end

.require_depsObject



6
7
8
9
10
11
12
# File 'lib/bunto-import/importers/csv.rb', line 6

def self.require_deps
  BuntoImport.require_with_fallback(%w[
    csv
    fileutils
    yaml
  ])
end

.specify_options(c) ⇒ Object



14
15
16
17
# File 'lib/bunto-import/importers/csv.rb', line 14

def self.specify_options(c)
  c.option 'file', '--file NAME', 'The CSV file to import (default: "posts.csv")'
  c.option 'no-front-matter', '--no-front-matter', 'Do not add the default front matter to the post body'
end

.write_frontmatter(f, post, options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bunto-import/importers/csv.rb', line 82

def self.write_frontmatter(f, post, options)
  no_frontmatter = options.fetch('no-front-matter', false)
  unless no_frontmatter
    f.puts YAML.dump({
      "layout"    => "post",
      "title"     => post.title,
      "date"      => post.published_at.to_s,
      "permalink" => post.permalink
    })
    f.puts "---"
  end
end

.write_post(post, options = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/bunto-import/importers/csv.rb', line 75

def self.write_post(post, options = {})
  File.open(File.join("_posts", post.filename), "w") do |f|
    write_frontmatter(f, post, options)
    f.puts post.body
  end
end