8 /my-site\.name/ => %r{^http://my-site\.name/(.+)\.xhtml$}
11 Jello::Mould.new do |paste, board|
13 if paste =~ %r{^http://.*}
15 unless paste =~ %r{^http://tr.im}
16 # We're going to add the main part of the domain to the end of the URI
17 # as a bullshit parameter, to give visitors some indication of what
18 # their destination is. If you're in a character-limited location, such
19 # as twitter or a text message, feel free to simply delete this section
20 # of the URL by hand after pasting. (⌥⌫ is helpful!)
22 # We also check if the URI matches a key of the Paths constant, and
23 # process the URI based on the value matched to that key if it matches.
24 # Keys can be stringish or regexish, in the latter case, it will run it
25 # matches. Values can be stringish or regexish, in the latter case,
26 # the last matching group will be used as the parameter.
28 matcher = Paths.select {|matcher,baser| uri.to_s =~ (matcher.is_a?(Regexp) ? matcher : /#{matcher}/) } .first
30 base = uri.to_s.match( matcher[1] )
33 unless base and (base = base[1])
34 base = uri.host.match( /(?:[\w\d\-\.]+\.)?([\w\d\-]+)\.[\w]{2,4}/ )[1]
37 base = URI::unescape(base).gsub(/\s/, '_')
38 uri = CGI::escape uri.to_s
40 shortener = URI.parse 'http://tr.im/api/trim_url.json'
42 # Feel free to copy this Mould to your ~/.jello directory and hardcode
43 # in your username and password, if you don't feel like having your
44 # username and password in your shell history.
46 params[:username] = ENV['TRIM_USERNAME'] if ENV['TRIM_USERNAME']
47 params[:password] = ENV['TRIM_PASSWORD'] if ENV['TRIM_PASSWORD']
50 shortener.query = params.to_a.map {|a| a.join '=' }.join('&')
53 puts " --@ [#{shortener}]" if $DEBUG
54 reply = open(shortener).read
55 short = JSON.parse reply
56 rescue OpenURI::HTTPError => e
57 short = {'url' => paste}
59 shortened = [short['url'], base].join('?')
60 shortened.length < paste.length ? shortened : paste