带HTTParty Gem的美味API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带HTTParty Gem的美味API相关的知识,希望对你有一定的参考价值。

  1. # From: http://railstips.org/2008/7/29/it-s-an-httparty-and-everyone-is-invited
  2. # Author: John Nunemaker
  3.  
  4. # Delicious API with HTTParty Gem
  5.  
  6. require 'rubygems'
  7. require 'httparty'
  8.  
  9. class Delicious
  10. include HTTParty
  11. base_uri 'https://api.del.icio.us/v1'
  12.  
  13. def initialize(auth)
  14. @auth = auth
  15. end
  16.  
  17. # query params that filter the posts are:
  18. # tag (optional). Filter by this tag.
  19. # dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
  20. # url (optional). Filter by this url.
  21. # ie: posts(:query => {:tag => 'ruby'})
  22. def posts(options={})
  23. options.merge!({:basic_auth => @auth})
  24. # get posts and convert to structs so we can do .key instead of ['key'] with results
  25. self.class.get('/posts/get', options)
  26. end
  27.  
  28. # query params that filter the posts are:
  29. # tag (optional). Filter by this tag.
  30. # count (optional). Number of items to retrieve (Default:15, Maximum:100).
  31. def recent(options={})
  32. options.merge!({:basic_auth => @auth})
  33. self.class.get('/posts/recent', options)
  34. end
  35. end
  36.  
  37. # CHANGE USERNAME & PASS !!!
  38. delicious = Delicious.new( :username => 'username', :password => 'password' )
  39. puts delicious.posts(:query => {:tag => 'ruby'}).inspect
  40. puts
  41. puts delicious.recent.inspect

以上是关于带HTTParty Gem的美味API的主要内容,如果未能解决你的问题,请参考以下文章

HTTParty 和通过令牌授权

[BZOJ 4129]Haruna’s Breakfast(树上带修改莫队)

Ruby——httparty使用代理

将 curl 命令转换为 httparty

如何关闭 HTTParty 帖子正文中的 URI 编码?

HTTParty无法发送长JSON [重复]