aj1215
6/20/2013 - 4:17 PM

Siteleaf import script. Imports a JSON dump of blog posts into a Siteleaf page. Requires the Siteleaf Gem (https://github.com/siteleaf/si

Siteleaf import script.

Imports a JSON dump of blog posts into a Siteleaf page. Requires the Siteleaf Gem (https://github.com/siteleaf/siteleaf-gem).

Run in the command line using "ruby import-siteleaf.rb".

require "siteleaf"
require "json"

# API settings
Siteleaf.api_key = '...'
Siteleaf.api_secret = '...'

# site settings
site_id = '...'
page_id = '...' # blog page to import posts into
test_mode = false # change to true to test your import

# get entries from JSON dump
contents = JSON.parse(File.read("import-siteleaf.json"))

# loop through and add entries
contents.each do |content|
  puts "Creating post..."
  
  # set up post
  post = Siteleaf::Post.new
  post.site_id = site_id
  post.parent_id = page_id
  
  # required
  post.title = content["title"]
  post.body = content["body"]
  
  # optional
  post.taxonomy = [
    {"key" => "Tags", "values" => content["tags"]}
  ]
  post.slug = content["slug"] 
  post.published_at = content["published_at"]
  
  # save
  puts test_mode ? post.inspect : post.save.inspect
end

# done!
puts "Success!"
[{
  "title":"Post 1",
  "body":"Body goes here.",
  "tags":["Tag 1", "Tag 2"],
  "slug":"post-1",
  "published_at":"2013-06-05T18:24:59-04:00"
}, {
  "title":"Post 2",
  "body":"Body goes here.",
  "tags":["Tag 1", "Tag 2"],
  "slug":"post-2",
  "published_at":"2013-06-05T18:24:59-04:00"
}]