Welcome. If you’d like to try adding a post. Point your editor at http://sin.metaatem.net/xml with any username.
You can even upload files. However, you can only upload small files until Rack, or Sinatra or SOMEONE fixes TempFile uploads.
Repository: kastner/sin Branch: master Commit: 2237bb264f48 Files: 2 Total size: 6.3 KB Directory structure: gitextract_8zhzt0p5/ ├── README └── sinatra-blog.rb ================================================ FILE CONTENTS ================================================ ================================================ FILE: README ================================================ Sin - The lazy Blogging engine in Sinatra Based on a conversation with Dan Benjamin, I decided to see how hard it would be to get XMLRPC (MetaWeblog API) working on a minimal site. As of right now (2008-02-17), it has: * posting by anyone through MetaWeblog api (point blog editor at http://site/xml) * uploads with the MetaWeblog api (only small-ish files, bug with Rack or something) * hAtom * A single file for the whole thing ================================================ FILE: sinatra-blog.rb ================================================ # Erik Kastner 2008-02-16 small blog engine with XMLRPC, hAtom and S3 upload (through xlmrpc) support require 'rubygems' require 'sinatra' require 'xmlrpc/marshal' require 'active_record' require 'aws/s3' BUCKET = "sinatra-data.metaatem.net" ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "sin.db") begin ActiveRecord::Schema.define do create_table :posts do |t| t.string :title t.string :author t.text :description t.timestamps end end rescue ActiveRecord::StatementInvalid end class Post < ActiveRecord::Base def permalink; "/posts/#{to_param}"; end def full_permalink; "http://sin.metaatem.net#{permalink}"; end def to_metaweblog { :dateCreated => created_at, :userid => 1, :postid => id, :description => description, :title => title, :link => "#{full_permalink}", :permaLink => "#{full_permalink}", :categories => ["General"], :date_created_gmt => created_at.getgm, } end end # set utf-8 for outgoing before_attend do header "Content-Type" => "text/html; charset=utf-8" end layout do <<-HTML
Welcome. If you’d like to try adding a post. Point your editor at http://sin.metaatem.net/xml with any username.
You can even upload files. However, you can only upload small files until Rack, or Sinatra or SOMEONE fixes TempFile uploads.
#{p.description}
" erb res end end # metaweblog api handler post '/xml' do xml = @request.env["rack.request.form_vars"] if xml.empty? hash = @request.env["rack.request.query_hash"] xml = (hash.keys + hash.values).join end raise "Nothing supplied" if xml.empty? call = XMLRPC::Marshal.load_call(xml) # convert metaWeblog.getPost to get_post method = call[0].gsub(/metaWeblog\.(.*)/, '\1').gsub(/([A-Z])/, '_\1').downcase header 'Content-Type' => 'text/xml' send(method, call) end def get_post(xmlrpc_call) begin post = Post.find(xmlrpc_call[1][0]) rescue ActiveRecord::RecordNotFound post = Post.find(xmlrpc_call[1][0].gsub(/^.*posts\/(\d+)[^\d].*$/, '\1')) end XMLRPC::Marshal.dump_response(post.to_metaweblog) end def get_recent_posts(xmlrpc_call) posts = Post.find(:all, :limit => 10, :order => "created_at DESC") XMLRPC::Marshal.dump_response(posts.map{|p| p.to_metaweblog}) end def new_post(xmlrpc_call) data = xmlrpc_call[1] # blog_id = data[0]; user = data[1]; pass = data[2] post_data = data[3] post = Post.create(:author => data[1], :title => post_data["title"], :description => post_data["description"]) XMLRPC::Marshal.dump_response(post.to_metaweblog) end def edit_post(xmlrpc_call) data = xmlrpc_call[1] post = Post.find(data[0]) # user = data[1]; pass = data[2] post_data = data[3] post.update_attributes!(:title => post_data["title"], :description => post_data["description"]) XMLRPC::Marshal.dump_response(post.to_metaweblog) end def get_categories(xmlrpc_call) #res = [{ :categoryId => 1,:parentId => 0,:description => "General",:categoryName => "General",:htmlUrl => "http://test.com/categories/1",:rssUrl => "http://test.com/categories/1/feed"}] XMLRPC::Marshal.dump_response(res) end def new_media_object(xmlrpc_call) post_data = xmlrpc_call[1][3] name = post_data["name"].gsub(/\//,'') AWS::S3::Base.establish_connection!( :access_key_id => ENV["AMAZON_ACCESS_KEY_ID"], :secret_access_key => ENV["AMAZON_SECRET_ACCESS_KEY"] ) AWS::S3::S3Object.store(name, post_data["bits"], BUCKET, :access => :public_read) XMLRPC::Marshal.dump_response({ :file => name, :url => "http://s3.amazonaws.com/#{BUCKET}/#{name}" }) end