public
Rubygem
Description: Makes http fun! Also, makes consuming restful web services dead easy.
Clone URL: git://github.com/jnunemaker/httparty.git
Click here to lend your support to: httparty and make a donation at www.pledgie.com !
httparty / examples / delicious.rb
100644 38 lines (31 sloc) 1.211 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
 
class Delicious
  include HTTParty
  base_uri 'https://api.del.icio.us/v1'
  
  def initialize(u, p)
    @auth = {:username => u, :password => p}
  end
  
  # query params that filter the posts are:
  # tag (optional). Filter by this tag.
  # dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
  # url (optional). Filter by this url.
  # ie: posts(:query => {:tag => 'ruby'})
  def posts(options={})
    options.merge!({:basic_auth => @auth})
    # get posts and convert to structs so we can do .key instead of ['key'] with results
    self.class.get('/posts/get', options)
  end
  
  # query params that filter the posts are:
  # tag (optional). Filter by this tag.
  # count (optional). Number of items to retrieve (Default:15, Maximum:100).
  def recent(options={})
    options.merge!({:basic_auth => @auth})
    self.class.get('/posts/recent', options)
  end
end
 
delicious = Delicious.new(config['username'], config['password'])
pp delicious.posts(:query => {:tag => 'ruby'})
pp delicious.recent