Snipplr API ruby library


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



Copy this code and paste it in your HTML
  1. module SnipplrAPI
  2. require 'xmlrpc/client'
  3.  
  4. API_URL = "http://snipplr.com/xml-rpc.php"
  5. class Snipplr
  6. attr_accessor :api_key
  7.  
  8. def initialize(key)
  9. @api_key = key
  10. @server = XMLRPC::Client::new2(SnipplrAPI::API_URL)
  11. end
  12.  
  13. def list(tags="", sort="", limit="")
  14. cmd = "snippet.list"
  15. result = @server.call(cmd, @api_key, tags, sort, limit)
  16. end
  17.  
  18. def get(snippet_id)
  19. cmd = "snippet.get"
  20. result = @server.call(cmd, snippet_id)
  21. end
  22.  
  23. def post(title,code,tags="",language="")
  24. cmd = "snippet.post"
  25. result = @server.call(cmd, @api_key, title, code, tags, language)
  26. end
  27.  
  28. def delete(snippet_id)
  29. cmd = "snippet.delete"
  30. result = @server.call(cmd, @api_key, snippet_id)
  31. end
  32.  
  33. def checkkey(api_key)
  34. cmd = "user.checkkey"
  35. result = @server.call(cmd, api_key)
  36. end
  37.  
  38. def languages()
  39. cmd = "languages.list"
  40. result = @server.call(cmd)
  41. end
  42.  
  43. end
  44. end
  45.  
  46. # =========================================
  47. # Testing: Do not include in class file
  48. # =========================================
  49. require 'pp' #used to Pretty Print results not required to use above module
  50.  
  51. # Replace YOUR_API_KEY below with your assigned API key
  52. # can be found at snipplr.com at http://snipplr.com/settings/
  53. # at the bottom of the page.
  54.  
  55. key = "YOUR_API_KEY"
  56. snipplr = SnipplrAPI::Snipplr.new(key)
  57.  
  58. # list your snippets
  59. pp snippets.list
  60.  
  61. # list only snippets tagged with php or xml
  62. #pp snipplr.list("PHP")

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.