Skip to content

Instantly share code, notes, and snippets.

@abhiyerra
Created May 24, 2009 03:35
Show Gist options
  • Save abhiyerra/116944 to your computer and use it in GitHub Desktop.
Save abhiyerra/116944 to your computer and use it in GitHub Desktop.
require "rubygems"
require "jdbc/mysql"
require "java"
require 'RedCloth'
require 'net/http'
require 'uri'
def post_entries
begin
# Prep the connection
Java::com.mysql.jdbc.Driver
userurl = "jdbc:mysql://host/my_database"
connSelect = java.sql.DriverManager.get_connection(userurl, "username", "password")
stmtSelect = connSelect.create_statement
# Define the query
selectquery = "SELECT * FROM posts;"
# Execute the query
rsS = stmtSelect.execute_query(selectquery)
# For each row returned do some stuff
while (rsS.next) do
content = RedCloth.new(rsS.getObject("content")).to_html
title = rsS.getObject("created_at")
title = title.to_s.gsub(/:\d\d.\d$/, '')
post_to_site title, content
end
end
# Close off the connection
stmtSelect.close
connSelect.close
end
def post_to_site title, content
site_id = '165805'
#3: Detailed control
url = URI.parse('http://posterous.com/api/newpost')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'email@address.com', 'p@$$w0rd'
req.set_form_data({'title' => title,
'body' => content,
'site_id' => site_id,
'private' => '1'})
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
puts res.error!
end
end
post_entries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment