Skip to content

Instantly share code, notes, and snippets.

@sskylar
Last active December 18, 2015 18:10
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sskylar/5824224 to your computer and use it in GitHub Desktop.
Save sskylar/5824224 to your computer and use it in GitHub Desktop.
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".
[{
"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"
}]
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!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment