Skip to content

Instantly share code, notes, and snippets.

@julien51
Created September 24, 2009 05:30
Show Gist options
  • Save julien51/192542 to your computer and use it in GitHub Desktop.
Save julien51/192542 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'atom'
require 'xmlrpc/client'
require 'uri'
# Make sure our template can use <%=h
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
##
# Subscription verification
get '/' do
if params["hub.challenge"]
params["hub.challenge"]
else
"This is awesome."
end
end
##
# Notification
post '/' do
# Parsing the Atom from the notification
feed = Atom::Feed.new(request.body.read)
uri = URI.parse(params["endpoint"])
uri.user = params["login"]
uri.password = params["password"]
server = XMLRPC::Client.new2(uri.to_s)
feed.entries.each do |e|
newPost = Hash.new
newPost['title'] = e.title.to_s
newPost['description'] = (e.content ? e.content.value.to_s : e.summary.to_s)
newPost['dateCreated'] = e.published
newPost['categories'] = e.categories.map { |c| c.term.to_s }
# Posting to the blog
server.call("metaWeblog.newPost", params["blog_id"], params["login"], params["password"], newPost, true)
end
"OK"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment