TextMate Ruby Snippet Retrieval


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



Copy this code and paste it in your HTML
  1. #!/usr/bin/ruby
  2. $: << ENV['TM_SUPPORT_PATH'] + '/lib'
  3.  
  4. require "cgi"
  5. require "ui"
  6. require "xmlrpc/client"
  7.  
  8. unless ENV['SNIPPLR_KEY']
  9. TextMate::UI.alert(:critical, "Snipplr error", "Please first set your 'SNIPPLR_KEY' in Preferences > Advanced > Shell Variables.")
  10. exit
  11. end
  12.  
  13. TextMate::UI.request_string(:title => "Get Snippet from Snipplr",
  14. :prompt => "Enter tags to search for:",
  15. :button1 => "Search") do |tag|
  16.  
  17. begin
  18. server = XMLRPC::Client.new2("http://snipplr.com/xml-rpc.php")
  19. snippet_list = server.call('snippet.list', ENV['SNIPPLR_KEY'], tag)
  20. rescue
  21. # fall through
  22. end
  23.  
  24. unless snippet_list and snippet_list.length > 0
  25. TextMate::UI.alert(:warning, "Snipplr result", "No snippets for '#{tag}' found.")
  26. else
  27. items = {}
  28. snippet_list.each { |r| items[r['title']] = r['id'] }
  29.  
  30. TextMate::UI.request_item(:items => items.keys.sort,
  31. :title => "Snipplr result",
  32. :prompt => "Choose",
  33. :string => "Select which snippet to paste") do |item|
  34. begin
  35. snippet = server.call('snippet.get', items[item])
  36. puts CGI.unescapeHTML(snippet['source'])
  37. rescue
  38. TextMate::UI.alert(:critical, "Snipplr error", "Could not retrieve '#{item}'.")
  39. end
  40. end
  41. end
  42. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.