Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
#!/usr/bin/ruby -w # This is a Pavatar script written in ruby by Jeena Paradies # I hereby place this script into the public domain. # # This is version 0.2 and it could include some bugs I haven't found yet. # Because the program is licensed free of charge, there is no warranty # for the program, but it works for me, try it if you want. # # If you have some questions contact me: pavatar@jeenaparadies.net # To run it you need a server with Ruby support and have to call it # via HTTP with the attribute url like: # http://example.org/cgi-bin/pavatar.rb?url=http://jeenaparadies.net # it will output the the Pavatar as a image or the default Pavatar # defined here: default_pavatar = "http://example.org/default-pavatar.png" # If you want to use it in a HTML page use: # <img src="http://example.org/cgi-bin/pavatar.rb?url=http://commentators-homepage.com" alt="Pavatar" /> # include some libraries to handle HTTP requests require 'cgi' require 'uri' require 'net/http' class Pavatar # content types defined by the spec http://pavatar.com/spec#technical-definition @@content_types = ['image/png', 'image/jpeg', 'image/gif'] # get pavatar url at initialization def initialize url unless url =~ /^http:\/\// url = 'http://' + url end @url = url @purl = URI.parse(@url) # check for the first real pavatar url and save it into a class variable if (@pavatar = xHeader).nil? and (@pavatar = link).nil? and (@pavatar = path).nil? @pavatar = favicon end end # Autodiscovery methods # get url from header if there is one def xHeader begin res = FetchHelper.head(@url) res['X-Pavatar'] unless res['X-Pavatar'].nil? rescue end end # get url from <link> if there is one def link begin res = FetchHelper.get(@url) pavatar = /<link rel="pavatar" href="([^"]+)" ?\/?>/.match(res.body) pavatar[1] unless pavatar.nil? rescue end end # look in users path if there is a pavatar def path begin # to discover the given directory is a little bit tricky in ruby path = @purl.path.to_s unless path[-1..-1] == '/' path = File.dirname(path) unless path == '.' or path == '/' path << '/' end end if path == '.' path = '/' end url = 'http://' + @purl.host.to_s + path + 'pavatar.png' # if would be ok to look at the header here but Apache has a problem with # head requests so we use a get request to workaround it res = FetchHelper.get(url) # some people redirect to a 404 page and send status 200 so we need to check the content type raise "Not supported content-type" if res['content-type'].nil? or not @@content_types.include?(res['content-type']) url rescue end end # look in users home directory if there is a pavatar def favicon begin url = 'http://' + @purl.host.to_s + '/pavatar.png' res = FetchHelper.get(url) raise "Not supported content-type" if res['content-type'].nil? or not @@content_types.include?(res['content-type']) url rescue end end # url getter def get_url @pavatar end # get a whole html string to show a pavatar def get_html (attr = '') attr = ' ' + attr unless attr.length > 0 '<img src="' + get_url + '"' + attr + ' />' end end # helps us handle redirections module FetchHelper def FetchHelper.head(url_str, limit = 10) # stop if there is a loop raise ArgumentError, 'HTTP redirect too deep' if limit == 0 url = URI.parse(url_str) path = File.dirname(url.path.to_s) response = nil Net::HTTP.start(url.host, url.port) { |http| response = http.head( path == '.' ? '/' : url.path.to_s ) } case response when Net::HTTPSuccess then response when Net::HTTPRedirection then FetchHelper.head(response['location'], limit - 1) else response.error! end end def FetchHelper.get(url_str, limit = 10) raise ArgumentError, 'HTTP redirect too deep' if limit == 0 response = Net::HTTP.get_response(URI.parse(url_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then FetchHelper.get(response['location'], limit - 1) else response.error! end end end # cach the GET variable cgi = CGI.new begin pavatar_url = Pavatar.new(cgi['url']) pavatar = FetchHelper.get(pavatar_url.get_url) rescue pavatar = FetchHelper.get(default_pavatar) print "Content-type: #{pavatar['content-type']}\r\n\r\n" print pavatar.body exit end # print our HTML header print "Content-type: #{pavatar['content-type']}\r\n\r\n" print pavatar.body # end of file
This paste will be private.
From the Design Piracy series on my blog: