class Contraption::Post
Attributes
body[R]
metadata[R]
Public Class Methods
build(data="")
click to toggle source
# File lib/contraption/post.rb, line 8 def build data="" data = data.to_s splitted = data.split(/\n\n/, 2) Post.new Header.from(splitted.first), translate_markdown(splitted.last) rescue ArgumentError => e Post.new Header.from(""), "" end
new(metadata="", body="")
click to toggle source
# File lib/contraption/post.rb, line 44 def initialize metadata="", body="" @body = body @metadata = metadata end
translate_markdown(content)
click to toggle source
# File lib/contraption/post.rb, line 17 def translate_markdown content Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(content) end
Public Instance Methods
publish(link_handlers=[])
click to toggle source
# File lib/contraption/post.rb, line 22 def publish link_handlers=[] links = list_links links.map! do |link| h = link_handlers.select{|l| l.protocol == link.first.to_sym} if h.length > 1 raise "Multiple link handlers for #{link.first} protocol" elsif h.length == 0 nil else h = h.first [link, h.handle(link)] end end.compact! update_links links Post.new( metadata.update({publication_date: Time.now}), body) end
Private Instance Methods
list_links()
click to toggle source
# File lib/contraption/post.rb, line 53 def list_links links = [] body.scan(/="([^"]*)"/) do |link| links << link.first.split('://', 2) end links end
method_missing(m, *a, &b)
click to toggle source
# File lib/contraption/post.rb, line 49 def method_missing m, *a, &b metadata.public_send(m, *a, &b) end
update_links(new_links)
click to toggle source
# File lib/contraption/post.rb, line 61 def update_links new_links new_links.each do |link| orig = link.first.join('://') new = link.last.join('://') @body.gsub!("=\"#{orig}\"", "=\"#{new}\"") end end