Get one of your snippets from Snipplr


/ Published in: Ruby
Save to your folder(s)

This command allows you to browse your snippet collection on Snipplr and retrieve them into e. Requires the installation of the wxCocoaDialog exe in the /Support/bin subdirectory.

Replace YOUR_API_KEY_HERE with the API key -- you can find it on your Settings page (http://snipplr.com/settings/).

Update 1/31/2007: I found escaping the $ was necessary to prevent the snippet engine from eating the variable.
Update 2/01/2007: Use the standard library function for escaping the snippet (instead of my own gsub() regex.
Update 2/02/2007: Add error checking code; handle empty accounts a bit more gracefully (and informatively).


Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby -w
  2. require "xmlrpc/client"
  3. require "cgi"
  4. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
  5. require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb"
  6.  
  7. COCOA_DIALOG = ENV['TM_SUPPORT_PATH'] + '/bin/CocoaDialog'
  8. API_KEY = "YOUR_API_KEY_HERE"
  9. server = XMLRPC::Client.new( "snipplr.com", "/xml-rpc.php")
  10.  
  11. if 0 == server.call("user.checkkey", API_KEY)
  12. puts "#{API_KEY} was not accepted as a valid API Key"
  13. TextMate.exit_show_tool_tip
  14. end
  15.  
  16. begin
  17. result = server.call("snippet.list", API_KEY)
  18. result_list = ""
  19. result.each { |row| result_list += "'#{row['id']} : #{row['title']}' " }
  20.  
  21. res=%x("#{COCOA_DIALOG}" dropdown \
  22. --title "Retrieve Snippet" \
  23. --string-output \
  24. --text "You have #{result.length} Snipplr entries stored under this API Key." \
  25. --items #{result_list} \
  26. --button1 'Retreive' --button2 'Cancel')
  27. button, item = res.split
  28. case button
  29. when 'Retreive'
  30. $snippet = server.call("snippet.get", item)
  31. when 'Cancel'
  32. puts "Snippet retreival cancelled."
  33. TextMate.exit_show_tool_tip
  34. end
  35. puts e_sn(CGI::unescapeHTML($snippet['source']))
  36.  
  37. rescue XMLRPC::FaultException => err
  38. if err.faultString =~ /No snippets found/
  39. print "You don't have any snippets yet!"
  40. else
  41. print "Error: " + err.faultCode.to_s + ", " + err.faultString
  42. end
  43. TextMate.exit_show_tool_tip
  44. rescue
  45. print "Error: #{$!}"
  46. TextMate.exit_show_tool_tip
  47. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.