PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/delicious.rb

http://github.com/jnunemaker/httparty
Ruby | 37 lines | 23 code | 6 blank | 8 comment | 0 complexity | 6bb901076dba7f8e9da7dcfdf35d9c03 MD5 | raw file
 1dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
 2require File.join(dir, 'httparty')
 3require 'pp'
 4config = YAML.load(File.read(File.join(ENV['HOME'], '.delicious')))
 5
 6class Delicious
 7  include HTTParty
 8  base_uri 'https://api.del.icio.us/v1'
 9
10  def initialize(u, p)
11    @auth = { username: u, password: p }
12  end
13
14  # query params that filter the posts are:
15  #   tag (optional). Filter by this tag.
16  #   dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
17  #   url (optional). Filter by this url.
18  #   ie: posts(query: {tag: 'ruby'})
19  def posts(options = {})
20    options.merge!({ basic_auth: @auth })
21    self.class.get('/posts/get', options)
22  end
23
24  # query params that filter the posts are:
25  #   tag (optional). Filter by this tag.
26  #   count (optional). Number of items to retrieve (Default:15, Maximum:100).
27  def recent(options = {})
28    options.merge!({ basic_auth: @auth })
29    self.class.get('/posts/recent', options)
30  end
31end
32
33delicious = Delicious.new(config['username'], config['password'])
34pp delicious.posts(query: { tag: 'ruby' })
35pp delicious.recent
36
37delicious.recent['posts']['post'].each { |post| puts post['href'] }