Skip to content

Instantly share code, notes, and snippets.

@davedash
Last active August 28, 2015 17:57
Show Gist options
  • Save davedash/12146a9f1ab5029256aa to your computer and use it in GitHub Desktop.
Save davedash/12146a9f1ab5029256aa to your computer and use it in GitHub Desktop.
# Import a blog post.
def ingest(external_post)
map = nil
MyStatsd.client.time('find_map') do
map = AuthorExternalMap.find_by_external_id(external_post.author_id)
end
if map.present?
author = nil
MyStatsd.client.time('find_author') do
author = Author.where(id: map.author_id).include(posts: [:categories]).try(:first)
end
category = nil
MyStatsd.client.time('find_category') do
category = Category.find_or_create_by!(name: external_post.category_name)
end
post = nil
MyStatsd.client.time('find_post') do
post = find_existing_post(external_post)
end
if post.present?
MyStatsd.client.time('add_author') do
post.authors << author
end
MyStatsd.client.time('add_category') do
post.category << category
end
else
post = nil
MyStatsd.client.time('create_post') do
post = author.post.create!(name: external_post.name, category: category, content: external_post.content)
end
MyStatsd.client.time('touch_author') do
author.touch
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment