delicious-ext 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ = Del.icio.us Extended API
2
+
3
+ This Module is an extension to official del.icio.us API. The main purpose here is to provide methods like get popular, fresh, hot and recent links using scraping.
4
+
5
+ === Author:
6
+ Thiago Bueno Silva (mailto:tbueno@tbueno.com)
7
+
8
+ === License:
9
+ Distributes under the same terms as Ruby
10
+
11
+
12
+ == USAGE:
13
+
14
+
15
+ d = Delicious::Collector.new
16
+ links = d.popular 'ruby'
17
+ links.each do |link|
18
+ puts '------------------------------------------'
19
+ puts "Text: #{link.text}"
20
+ puts "URL: #{link.url}"
21
+ puts "People: #{link.people}"
22
+ puts "Posted By: #{link.posted_by.name}" if link.posted_by.name
23
+ puts "Tags: #{link.tags * ','}"
24
+ end
25
+
26
+
27
+ == METHODS
28
+
29
+ === Popular
30
+
31
+ Get popular links found at "delicious.com/popular".
32
+
33
+ links = d.popular
34
+ links.each do |link|
35
+ puts '------------------------------------------'
36
+ puts "Text: #{link.text}"
37
+ puts "URL: #{link.url}"
38
+ puts "People: #{link.people}"
39
+ puts "Posted By: #{link.posted_by.name}" if link.posted_by.name
40
+ puts "Tags: #{link.tags * ','}"
41
+ end
42
+
43
+ # This links can also be searched by tags
44
+
45
+ d.popular 'ruby'
46
+
47
+ === Recent
48
+
49
+ Links newly added to delicious.
50
+ recent_links = d.recent
51
+
52
+ === Fresh
53
+
54
+ "The freshest bookmarks that are flying like hotcakes on Delicious and beyond."
55
+
56
+ fresh_links = d.fresh
57
+
58
+ === Hot List
59
+
60
+ Popular links right now.
61
+
62
+ hot_links = d.hot_list
63
+
64
+ === Search
65
+
66
+ Search links based on a term.
67
+
68
+ links = d.search 'rails'
69
+
70
+
71
+
72
+
data/lib/delicious.rb ADDED
@@ -0,0 +1,151 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'open-uri'
4
+
5
+
6
+ # This Module is an extension to official del.icio.us API.
7
+ # The main purpose here is to provide methods like
8
+ # get popular, hot and recent links using scraping.
9
+
10
+ # Author:: Thiago Bueno Silva (mailto:tbueno@tbueno.com)
11
+ # License:: Distributes under the same terms as Ruby
12
+ module Delicious
13
+
14
+ #Possible types of lists
15
+ FRESH = 0
16
+ POPULAR = 1
17
+ RECENT = 2
18
+ SEARCH = 3
19
+ BASE_URL = 'http://del.icio.us'
20
+
21
+ POPULAR_URL = BASE_URL+'/popular'
22
+ HOT_LIST_URL = BASE_URL+'/?view=hotlist'
23
+ RECENT_URL = BASE_URL+'/recent'
24
+ SEARCH_URL = BASE_URL+'/search?context=all&p='
25
+
26
+ FRESH_QUERY = POPULAR_QUERY = "//ul[@id='bookmarklist']/li/div"
27
+ SEARCH_QUERY = "//div/[@id='bd']/div[@id='yui-main']/div[@id='content']/ul/li/div"
28
+
29
+ class Collector
30
+ @list_type = 0
31
+
32
+
33
+ def fresh
34
+ @list_type = FRESH
35
+ get_links BASE_URL, FRESH_QUERY
36
+ end
37
+
38
+ def hot_list
39
+ @list_type = FRESH
40
+ get_links HOT_LIST_URL, FRESH_QUERY
41
+ end
42
+
43
+ def popular(tag='')
44
+ @list_type = POPULAR
45
+ get_links POPULAR_URL+'/'+tag, POPULAR_QUERY
46
+ end
47
+
48
+ def recent(min=2)
49
+ @list_type = RECENT
50
+ get_links RECENT_URL+'?min='+min.to_s, POPULAR_QUERY
51
+ end
52
+
53
+ def search(term)
54
+ @list_type = SEARCH
55
+ get_links SEARCH_URL+term+"&lc=1", SEARCH_QUERY
56
+ end
57
+
58
+ private
59
+ def get_links(base_url, query)
60
+ links = []
61
+
62
+ doc = Hpricot(open(base_url))
63
+ (doc/query).each do |result|
64
+ text = (result/"div[@class='data']/h4/a[@href").first.inner_text
65
+ url = (result/"div[@class='data']/h4/a[@href").first['href']
66
+ posted_by = get_posted_by(result)
67
+ people = get_people(result).to_i
68
+ tags = get_tags(result)
69
+ links << Delicious::Link.new( text, url, posted_by, people, tags)
70
+
71
+ end
72
+ links
73
+ end
74
+
75
+ def get_posted_by(result)
76
+ name = " "
77
+ if @list_type == POPULAR or @list_type == SEARCH
78
+ name = (result/"div[@class='meta']/a/span").first.inner_text.split.last
79
+ elsif @list_type == RECENT
80
+ name = (result/"div[@class='meta']/a[@class='user").first['href'].gsub("/", "")
81
+ end
82
+ Delicious::Person.new(name)
83
+ end
84
+
85
+ def get_people(result)
86
+ (result/"div[@class='data']/div/div/a/span").inner_text
87
+ end
88
+
89
+ def get_tags(result)
90
+ tags = []
91
+ query = (result/"div[@class='tagdisplay']/ul/li/a/span").each do |span|
92
+ tags << span.inner_text
93
+ end
94
+ tags
95
+ end
96
+ end
97
+ ####### Collector Class end #############
98
+
99
+ class Link
100
+
101
+ attr_reader :text, :url, :posted_by, :people, :tags
102
+
103
+ def initialize(text, url, posted_by, people, tags)
104
+ @text = text
105
+ @url = url
106
+ @posted_by = posted_by
107
+ @people = people
108
+ @tags = tags
109
+ end
110
+
111
+ #Comparator method. The links should be ordered by the number of people that saved them.
112
+ def <=> (link)
113
+ if @people > link.people
114
+ return 1
115
+ elsif @people < link.people
116
+ return -1
117
+ end
118
+ 0
119
+ end
120
+
121
+ def equal?(link)
122
+ link.url == @url
123
+ end
124
+
125
+ end
126
+ ####### Link Class end #############
127
+
128
+ class Person
129
+
130
+ attr_reader :name, :url
131
+
132
+ def initialize(name)
133
+ @name = name
134
+ @url = BASE_URL+'/'+name
135
+ end
136
+ end
137
+ ########### Person Class end ############
138
+
139
+ class Tag
140
+ attr_reader :name, :url
141
+
142
+ def initialize(name)
143
+ @name = name
144
+ @url = POPULAR_URL +'/'+name
145
+ end
146
+ end
147
+
148
+
149
+ end
150
+
151
+
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + "/../lib/delicious"
4
+
5
+ class DeliciousTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @delicious = Delicious::Collector.new
9
+ end
10
+
11
+ def test_popular
12
+ assert(@delicious.popular.size > 0, "All query should return at least one occurence")
13
+ end
14
+ end
data/test/link_test.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + "/../lib/delicious"
4
+
5
+ class LinkTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @link = Delicious::Link.new('text', 'url', 'author', 200, ['test','test2'])
9
+ end
10
+ def test_equals
11
+ link2 = Delicious::Link.new('', 'url', '', 0, [])
12
+ assert(@link.equal?(link2), "According to url, this links have to be equals")
13
+ end
14
+
15
+ def test_equals_should_notice_different
16
+ link2 = Delicious::Link.new('', 'url2', '', '', [])
17
+ assert(!@link.equal?(link2), "According to url, this links have to be different")
18
+ end
19
+
20
+ def test_comparator
21
+ link2 = Delicious::Link.new('', 'url2', '', 201, [])
22
+ link3 = Delicious::Link.new('', 'url3', '', 203, [])
23
+ links = [link2, @link, link3].sort
24
+
25
+ assert(@link == links[0], "#{@link} should be the firs element in this test")
26
+
27
+ assert(link3 == links[2], "#{link3} should be the firs element in this test")
28
+ end
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delicious-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Thiago Bueno Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-01 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mime-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.15"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: diff-lcs
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.2
34
+ version:
35
+ description: An Extension to Del.icio.us API using Ruby.
36
+ email: tbueno@tbueno.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.rdoc
45
+ - lib/delicious.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/tbueno/delicious-ext
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: delicious-ext
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: An Extension to Del.icio.us API using Ruby.
74
+ test_files:
75
+ - test/delicious_test.rb
76
+ - test/link_test.rb