laco-www-delicious 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/CHANGELOG.rdoc +61 -0
  2. data/Gemfile +7 -0
  3. data/LICENSE.rdoc +25 -0
  4. data/Manifest +47 -0
  5. data/README.rdoc +201 -0
  6. data/Rakefile +57 -0
  7. data/laco-www-delicious.gemspec +41 -0
  8. data/lib/www/delicious/bundle.rb +73 -0
  9. data/lib/www/delicious/element.rb +73 -0
  10. data/lib/www/delicious/errors.rb +46 -0
  11. data/lib/www/delicious/post.rb +123 -0
  12. data/lib/www/delicious/tag.rb +101 -0
  13. data/lib/www/delicious/version.rb +33 -0
  14. data/lib/www/delicious.rb +947 -0
  15. data/setup.rb +1585 -0
  16. data/test/bundle_test.rb +63 -0
  17. data/test/delicious_test.rb +370 -0
  18. data/test/fixtures/net_response_invalid_account.yml +25 -0
  19. data/test/fixtures/net_response_success.yml +23 -0
  20. data/test/online_test.rb +147 -0
  21. data/test/post_test.rb +68 -0
  22. data/test/tag_test.rb +69 -0
  23. data/test/test_all.rb +19 -0
  24. data/test/test_helper.rb +43 -0
  25. data/test/testcases/element/bundle.xml +1 -0
  26. data/test/testcases/element/invalid_root.xml +2 -0
  27. data/test/testcases/element/post.xml +2 -0
  28. data/test/testcases/element/post_unshared.xml +2 -0
  29. data/test/testcases/element/tag.xml +1 -0
  30. data/test/testcases/response/bundles_all.xml +5 -0
  31. data/test/testcases/response/bundles_all_empty.xml +2 -0
  32. data/test/testcases/response/bundles_delete.xml +2 -0
  33. data/test/testcases/response/bundles_set.xml +2 -0
  34. data/test/testcases/response/bundles_set_error.xml +2 -0
  35. data/test/testcases/response/posts_add.xml +2 -0
  36. data/test/testcases/response/posts_all.xml +12 -0
  37. data/test/testcases/response/posts_dates.xml +14 -0
  38. data/test/testcases/response/posts_dates_with_tag.xml +14 -0
  39. data/test/testcases/response/posts_delete.xml +2 -0
  40. data/test/testcases/response/posts_get.xml +7 -0
  41. data/test/testcases/response/posts_get_with_tag.xml +6 -0
  42. data/test/testcases/response/posts_recent.xml +19 -0
  43. data/test/testcases/response/posts_recent_with_tag.xml +19 -0
  44. data/test/testcases/response/tags_get.xml +5 -0
  45. data/test/testcases/response/tags_get_empty.xml +2 -0
  46. data/test/testcases/response/tags_rename.xml +2 -0
  47. data/test/testcases/response/update.delicious1.xml +2 -0
  48. data/test/testcases/response/update.xml +3 -0
  49. metadata +161 -0
@@ -0,0 +1,63 @@
1
+ #
2
+ # = WWW::Delicious
3
+ #
4
+ # Ruby client for del.icio.us API.
5
+ #
6
+ #
7
+ # Category:: WWW
8
+ # Package:: WWW::Delicious
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ require 'test_helper'
18
+ require 'www/delicious/bundle'
19
+
20
+
21
+ class BundleTest < Test::Unit::TestCase
22
+
23
+ def test_bundle
24
+ expected = { :name => 'MyTag', :tags => %w(foo bar) }
25
+ assert_attributes(instance(expected), expected)
26
+ end
27
+
28
+ def test_tag_from_rexml
29
+ dom = REXML::Document.new(File.read(TESTCASES_PATH + '/element/bundle.xml'))
30
+ expected = { :name => 'music', :tags => %w(ipod mp3 music) }
31
+
32
+ element = WWW::Delicious::Bundle.from_rexml(dom.root)
33
+ assert_attributes(element, expected)
34
+ end
35
+
36
+
37
+ def test_bundle_name_strips_whitespaces
38
+ [' foo ', 'foo ', ' foo ', ' foo'].each do |v|
39
+ assert_equal('foo', instance(:name => v).name) # => 'foo'
40
+ end
41
+ end
42
+
43
+ def test_to_s_returns_name_as_string
44
+ assert_equal('foobar', instance(:name => 'foobar', :tags => %w(foo bar)).to_s)
45
+ end
46
+
47
+ def test_to_s_returns_empty_string_with_name_nil
48
+ assert_equal('', instance(:name => nil).to_s)
49
+ end
50
+
51
+
52
+ # def test_valid
53
+ # end
54
+
55
+
56
+ protected
57
+
58
+ # returns a stub instance
59
+ def instance(values = {}, &block)
60
+ WWW::Delicious::Bundle.new(values)
61
+ end
62
+
63
+ end
@@ -0,0 +1,370 @@
1
+ #
2
+ # = WWW::Delicious
3
+ #
4
+ # Ruby client for del.icio.us API.
5
+ #
6
+ #
7
+ # Category:: WWW
8
+ # Package:: WWW::Delicious
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ require 'test_helper'
18
+
19
+
20
+ class DeliciousTest < Test::Unit::TestCase
21
+
22
+ TEST_USERNAME = 'username'
23
+ TEST_PASSWORD = 'password'
24
+
25
+ def setup
26
+ @delicious = instance
27
+ end
28
+
29
+ def test_initialize_should_raise_without_account
30
+ assert_raise(ArgumentError) { WWW::Delicious.new() }
31
+ assert_raise(ArgumentError) { WWW::Delicious.new(TEST_USERNAME) }
32
+ end
33
+
34
+ def test_initialize_should_set_account_credentials
35
+ assert_equal(TEST_USERNAME, @delicious.username)
36
+ assert_equal(TEST_PASSWORD, @delicious.password)
37
+ end
38
+
39
+ def test_initialize_should_allow_option_user_agent
40
+ useragent = 'MyClass/1.0 (Foo/Bar +http://foo.com/)'
41
+ delicious = instance(:user_agent => useragent)
42
+ assert_equal(useragent, delicious.user_agent)
43
+ end
44
+
45
+ def test_initialize_should_default_option_user_agent_unless_option
46
+ useragent = instance.user_agent
47
+ assert_match("Ruby/#{RUBY_VERSION}", useragent)
48
+ assert_match("#{WWW::Delicious::NAME}/#{WWW::Delicious::VERSION}", useragent)
49
+ end
50
+
51
+ def test_initialize_should_allow_option_base_uri
52
+ base_uri = 'https://ma.gnolia.com/api/mirrord'
53
+ delicious = instance(:base_uri => base_uri)
54
+ assert_equal(URI.parse(base_uri), delicious.base_uri)
55
+ end
56
+
57
+ def test_initialize_should_default_option_base_uri_unless_option
58
+ base_uri = instance.base_uri
59
+ assert_equal(URI.parse('https://api.del.icio.us'), base_uri)
60
+ end
61
+
62
+
63
+ # =========================================================================
64
+ # HTTP Request common checks
65
+ # =========================================================================
66
+
67
+ def test_request_raises_without_http_client
68
+ @delicious.http_client = nil
69
+ assert_raise(WWW::Delicious::Error) { @delicious.update }
70
+ end
71
+
72
+ def test_request_waits_necessary_time_between_requests
73
+ @delicious.expects(:make_request).times(4).returns(load_fixture('/net_response_success.yml'))
74
+ @delicious.valid_account? # 1st request
75
+ 3.times do |time|
76
+ lr = @delicious.instance_variable_get(:@last_request)
77
+ @delicious.valid_account? # N request
78
+ nr = @delicious.instance_variable_get(:@last_request)
79
+ time_diff = (nr - lr)
80
+ assert !(time_diff < WWW::Delicious::SECONDS_BEFORE_NEW_REQUEST),
81
+ "Request ##{time} run after `#{time_diff}' seconds " +
82
+ "but it should wait at least `#{WWW::Delicious::SECONDS_BEFORE_NEW_REQUEST}' seconds"
83
+ end
84
+ end
85
+
86
+
87
+ def test_valid_account
88
+ @delicious.expects(:make_request).once.returns(load_fixture('/net_response_success.yml'))
89
+ assert(@delicious.valid_account?)
90
+ end
91
+
92
+ def test_invalid_account
93
+ @delicious.expects(:make_request).once.returns(load_fixture('/net_response_invalid_account.yml'))
94
+ assert(!@delicious.valid_account?)
95
+ end
96
+
97
+
98
+ # =========================================================================
99
+ # Update
100
+ # =========================================================================
101
+
102
+ def test_update
103
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
104
+ assert_equal(@delicious.update, Time.parse("2008-08-02T11:55:35Z"))
105
+ end
106
+
107
+ def test_update_raises_without_update_root_node
108
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_all.xml'))
109
+ error = assert_raise(WWW::Delicious::ResponseError) do
110
+ @delicious.update
111
+ end
112
+ assert_match(/`update`/, error.message)
113
+ end
114
+
115
+ def test_update_delicious1
116
+ @delicious.expects(:request).once.returns(mock_response('/response/update.delicious1.xml'))
117
+ assert_equal(@delicious.update, Time.parse("2008-03-12T08:41:20Z"))
118
+ end
119
+
120
+
121
+ # =========================================================================
122
+ # Bundles
123
+ # =========================================================================
124
+
125
+ def test_bundles_all
126
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_all.xml'))
127
+ expected = [ ['music', %w(ipod mp3 music)], ['pc', %w(computer software hardware)] ]
128
+
129
+ results = @delicious.bundles_all
130
+ assert_instance_of(Array, results)
131
+ assert_equal(2, results.length)
132
+
133
+ results.each_with_index do |bundle, index|
134
+ assert_instance_of(WWW::Delicious::Bundle, bundle)
135
+ name, tags = expected[index]
136
+ assert_equal(name, bundle.name)
137
+ assert_equal(tags, bundle.tags)
138
+ end
139
+ end
140
+
141
+ def test_bundles_all_empty
142
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_all_empty.xml'))
143
+ results = @delicious.bundles_all
144
+ assert_instance_of(Array, results)
145
+ assert_equal(0, results.length)
146
+ end
147
+
148
+ def test_bundles_all_raises_without_bundles_root_node
149
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
150
+ error = assert_raise(WWW::Delicious::ResponseError) do
151
+ @delicious.bundles_all
152
+ end
153
+ assert_match(/`bundles`/, error.message)
154
+ end
155
+
156
+
157
+ def test_bundles_set
158
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_set.xml'))
159
+ assert(@delicious.bundles_set('name', %w(foo bar)))
160
+ end
161
+
162
+ def test_bundles_set_raises_with_response_error
163
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_set_error.xml'))
164
+ error = assert_raise(WWW::Delicious::Error) do
165
+ @delicious.bundles_set('name', %w(foo bar))
166
+ end
167
+ assert_match(/you must supply a bundle name/, error.message)
168
+ end
169
+
170
+ def test_bundles_set_raises_without_result_root_node
171
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
172
+ error = assert_raise(WWW::Delicious::ResponseError) do
173
+ @delicious.bundles_set('name', %w(foo bar))
174
+ end
175
+ assert_match(/`result`/, error.message)
176
+ end
177
+
178
+
179
+ def test_bundles_delete
180
+ @delicious.expects(:request).once.returns(mock_response('/response/bundles_delete.xml'))
181
+ assert(@delicious.bundles_delete('name'))
182
+ end
183
+
184
+ def test_bundles_delete_raises_without_result_root_node
185
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
186
+ error = assert_raise(WWW::Delicious::ResponseError) do
187
+ @delicious.bundles_delete('name')
188
+ end
189
+ assert_match(/`result`/, error.message)
190
+ end
191
+
192
+
193
+ # =========================================================================
194
+ # Tags
195
+ # =========================================================================
196
+
197
+ def test_tags_get
198
+ @delicious.expects(:request).once.returns(mock_response('/response/tags_get.xml'))
199
+ expected = [ ['activedesktop', 1], ['business', 14] ]
200
+
201
+ results = @delicious.tags_get
202
+ assert_instance_of(Array, results)
203
+ assert_equal(2, results.length)
204
+
205
+ results.each_with_index do |tag, index|
206
+ assert_instance_of(WWW::Delicious::Tag, tag)
207
+ name, count = expected[index]
208
+ assert_equal(name, tag.name)
209
+ assert_equal(count, tag.count)
210
+ end
211
+ end
212
+
213
+ def test_tags_get_empty
214
+ @delicious.expects(:request).once.returns(mock_response('/response/tags_get_empty.xml'))
215
+ results = @delicious.tags_get
216
+ assert_instance_of(Array, results)
217
+ assert_equal(0, results.length)
218
+ end
219
+
220
+ def test_tags_get_raises_without_bundles_root_node
221
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
222
+ error = assert_raise(WWW::Delicious::ResponseError) do
223
+ @delicious.tags_get
224
+ end
225
+ assert_match(/`tags`/, error.message)
226
+ end
227
+
228
+
229
+ def test_tags_rename
230
+ @delicious.expects(:request).once.returns(mock_response('/response/tags_rename.xml'))
231
+ assert(@delicious.tags_rename('old', 'new'))
232
+ end
233
+
234
+ def test_tags_rename_raises_without_result_root_node
235
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
236
+ error = assert_raise(WWW::Delicious::ResponseError) do
237
+ @delicious.tags_rename('foo', 'bar')
238
+ end
239
+ assert_match(/`result`/, error.message)
240
+ end
241
+
242
+
243
+ # =========================================================================
244
+ # Posts
245
+ # =========================================================================
246
+
247
+ def test_posts_get
248
+ @delicious.expects(:request).once.returns(mock_response('/response/posts_get.xml'))
249
+ results = @delicious.posts_get
250
+ assert_instance_of(Array, results)
251
+ assert_equal(3, results.length)
252
+ assert_equal('New to Git? - GitHub', results.first.title)
253
+ assert_equal('.c( whytheluckystiff )o. -- The Fully Upturned Bin', results.last.title)
254
+ end
255
+
256
+ def test_posts_get_raises_without_posts_root_node
257
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
258
+ error = assert_raise(WWW::Delicious::ResponseError) do
259
+ @delicious.posts_get
260
+ end
261
+ assert_match(/`posts`/, error.message)
262
+ end
263
+
264
+
265
+ def test_posts_recent
266
+ @delicious.expects(:request).once.returns(mock_response('/response/posts_recent.xml'))
267
+ results = @delicious.posts_recent
268
+ assert_instance_of(Array, results)
269
+ assert_equal(15, results.length)
270
+ assert_equal('New to Git? - GitHub', results.first.title)
271
+ assert_equal('RichText | Lightview for modal dialogs on Rails', results.last.title)
272
+ end
273
+
274
+ def test_posts_recent_raises_without_posts_root_node
275
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
276
+ error = assert_raise(WWW::Delicious::ResponseError) do
277
+ @delicious.posts_recent
278
+ end
279
+ assert_match(/`posts`/, error.message)
280
+ end
281
+
282
+
283
+ def test_posts_all
284
+ @delicious.expects(:request).once.returns(mock_response('/response/posts_all.xml'))
285
+ results = @delicious.posts_all
286
+ assert_instance_of(Array, results)
287
+ assert_equal(8, results.length)
288
+ assert_equal('New to Git? - GitHub', results.first.title)
289
+ assert_equal('ASP 101 - Object Oriented ASP: Using Classes in Classic ASP', results.last.title)
290
+ end
291
+
292
+ def test_posts_all_raises_without_posts_root_node
293
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
294
+ error = assert_raise(WWW::Delicious::ResponseError) do
295
+ @delicious.posts_all
296
+ end
297
+ assert_match(/`posts`/, error.message)
298
+ end
299
+
300
+
301
+ def test_posts_dates
302
+ @delicious.expects(:request).once.returns(mock_response('/response/posts_dates.xml'))
303
+ results = @delicious.posts_dates
304
+ assert_instance_of(Hash, results)
305
+ assert_equal(10, results.length)
306
+ end
307
+
308
+ def test_posts_dates_raises_without_dates_root_node
309
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
310
+ error = assert_raise(WWW::Delicious::ResponseError) do
311
+ @delicious.posts_dates
312
+ end
313
+ assert_match(/`dates`/, error.message)
314
+ end
315
+
316
+
317
+ def test_posts_add
318
+ params = {:url => 'http://localhost', :title => 'Just a test'}
319
+ @delicious.expects(:request).times(2).returns(mock_response('/response/posts_add.xml'))
320
+ assert(@delicious.posts_add(WWW::Delicious::Post.new(params)))
321
+ assert(@delicious.posts_add(params))
322
+ end
323
+
324
+
325
+ def test_posts_delete
326
+ @delicious.expects(:request).once.returns(mock_response('/response/posts_delete.xml'))
327
+ assert(@delicious.posts_delete('test'))
328
+ end
329
+
330
+ def test_posts_delete_raises_without_result_root_node
331
+ @delicious.expects(:request).once.returns(mock_response('/response/update.xml'))
332
+ error = assert_raise(WWW::Delicious::ResponseError) do
333
+ @delicious.posts_delete('test')
334
+ end
335
+ assert_match(/`result`/, error.message)
336
+ end
337
+
338
+
339
+
340
+ protected
341
+
342
+ # returns a stub instance
343
+ def instance(options = {}, &block)
344
+ WWW::Delicious.new(TEST_USERNAME, TEST_PASSWORD, options, &block)
345
+ end
346
+
347
+ def load_testcase(file)
348
+ File.read(TESTCASES_PATH + file)
349
+ end
350
+
351
+ def load_fixture(file)
352
+ YAML.load(File.read(FIXTURES_PATH + file))
353
+ end
354
+
355
+ def mock_response(file_or_content)
356
+ content = case
357
+ when file_or_content =~ /\.xml$/
358
+ load_testcase(file_or_content)
359
+ when file_or_content =~ /\.yml$/
360
+ load_fixture(file_or_content)
361
+ else
362
+ file_or_content.to_s
363
+ end
364
+
365
+ response = mock()
366
+ response.expects(:body).at_least(1).returns(content)
367
+ response
368
+ end
369
+
370
+ end
@@ -0,0 +1,25 @@
1
+ --- !ruby/object:Net::HTTPUnauthorized
2
+ body: |
3
+ <?xml version="1.0" standalone="yes"?>
4
+ <result code="access denied" />
5
+ <!-- fe02.api.del.ac4.yahoo.net uncompressed/chunked Tue Jul 29 13:53:52 PDT 2008 -->
6
+
7
+ body_exist: true
8
+ code: "401"
9
+ header:
10
+ connection:
11
+ - close
12
+ p3p:
13
+ - policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
14
+ content-type:
15
+ - text/xml; charset=utf-8
16
+ date:
17
+ - Tue, 29 Jul 2008 20:53:52 GMT
18
+ www-authenticate:
19
+ - Basic realm="del.icio.us API"
20
+ transfer-encoding:
21
+ - chunked
22
+ http_version: "1.1"
23
+ message: Unauthorized
24
+ read: true
25
+ socket:
@@ -0,0 +1,23 @@
1
+ --- !ruby/object:Net::HTTPOK
2
+ body: |
3
+ <?xml version="1.0" encoding="UTF-8"?>
4
+ <update time="2008-07-29T19:29:39Z" inboxnew="0"/>
5
+ <!-- fe01.api.del.ac4.yahoo.net uncompressed/chunked Tue Jul 29 13:51:05 PDT 2008 -->
6
+
7
+ body_exist: true
8
+ code: "200"
9
+ header:
10
+ connection:
11
+ - close
12
+ p3p:
13
+ - policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
14
+ content-type:
15
+ - text/xml; charset=utf-8
16
+ date:
17
+ - Tue, 29 Jul 2008 20:51:05 GMT
18
+ transfer-encoding:
19
+ - chunked
20
+ http_version: "1.1"
21
+ message: OK
22
+ read: true
23
+ socket:
@@ -0,0 +1,147 @@
1
+ #
2
+ # = WWW::Delicious
3
+ #
4
+ # Ruby client for del.icio.us API.
5
+ #
6
+ #
7
+ # Category:: WWW
8
+ # Package:: WWW::Delicious
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ require 'test_helper'
18
+
19
+
20
+ RUN_ONLINE_TESTS = ($0 == __FILE__) unless defined?(RUN_ONLINE_TESTS)
21
+
22
+ puts "Online test #{__FILE__} skipped.\n" +
23
+ "Use `ONLINE=1` to run online tests.\n" unless RUN_ONLINE_TESTS
24
+
25
+
26
+ class OnlineTest < Test::Unit::TestCase
27
+
28
+ def setup
29
+ init_account
30
+ end
31
+
32
+
33
+ def test_update
34
+ response = @delicious.update
35
+ assert_instance_of(Time, response)
36
+ end
37
+
38
+
39
+ def test_bundles_all
40
+ response = @delicious.bundles_all
41
+ assert_kind_of(Array, response)
42
+
43
+ response.each do |bundle|
44
+ assert_instance_of(WWW::Delicious::Bundle, bundle)
45
+ assert_instance_of(Array, bundle.tags)
46
+ assert_not_nil(bundle.name)
47
+ end
48
+ end
49
+
50
+ def test_bundles_set
51
+ bundle = WWW::Delicious::Bundle.new(:name => 'test_bundle', :tags => %w(ruby python).sort)
52
+ assert_nothing_raised() { @delicious.bundles_set(bundle) }
53
+ end
54
+
55
+ def test_bundles_delete
56
+ bundle = WWW::Delicious::Bundle.new(:name => 'test_bundle')
57
+ assert_nothing_raised() { @delicious.bundles_delete(bundle) }
58
+ end
59
+
60
+
61
+ def test_tags_get
62
+ response = @delicious.tags_get
63
+ assert_kind_of(Array, response)
64
+
65
+ response.each do |tag|
66
+ assert_instance_of(WWW::Delicious::Tag, tag)
67
+ assert_not_nil(tag.name)
68
+ assert_not_nil(tag.count)
69
+ end
70
+ end
71
+
72
+ def test_tags_rename
73
+ ftag = WWW::Delicious::Tag.new(:name => 'old_tag')
74
+ otag = WWW::Delicious::Tag.new(:name => 'new_tag')
75
+ assert_nothing_raised() { @delicious.tags_rename(ftag, otag) }
76
+ end
77
+
78
+
79
+ def test_post_add
80
+ # TODO
81
+ end
82
+
83
+ def test_post_all
84
+ response = @delicious.posts_get
85
+ assert_kind_of(Array, response)
86
+
87
+ response.each do |post|
88
+ assert_kind_of(WWW::Delicious::Post, post)
89
+ end
90
+ end
91
+
92
+ def test_post_dates
93
+ response = @delicious.posts_dates
94
+ assert_kind_of(Hash, response)
95
+
96
+ response.each do |item, value|
97
+ assert_match(/[\d]{4}-[\d]{2}-[\d]{2}/, item)
98
+ assert_match(/[\d]+/, value.to_s)
99
+ end
100
+ end
101
+
102
+ def test_post_delete
103
+ # TODO
104
+ end
105
+
106
+ def test_posts_get
107
+ response = @delicious.posts_get
108
+ assert_kind_of(Array, response)
109
+
110
+ response.each do |post|
111
+ assert_kind_of(WWW::Delicious::Post, post)
112
+ end
113
+ end
114
+
115
+ def test_posts_recent
116
+ response = @delicious.posts_recent
117
+ assert_kind_of(Array, response)
118
+
119
+ response.each do |post|
120
+ assert_kind_of(WWW::Delicious::Post, post)
121
+ end
122
+ end
123
+
124
+
125
+ protected
126
+
127
+ def init_account(options = {}, &block)
128
+ @delicious_username ||= ENV['DELICIOUS_USERNAME'] || self.class.prompt('Delicious Username') { |value| abort('Value cannot be blank') if value.blank?; value }
129
+ @delicious_password ||= ENV['DELICIOUS_PASSWORD'] || self.class.prompt('Delicious Password') { |value| abort('Value cannot be blank') if value.blank?; value }
130
+ @delicious = WWW::Delicious.new(@delicious_username, @delicious_password, options, &block)
131
+ end
132
+
133
+ # Convenient method for collecting user input
134
+ def self.prompt(text, options = {}, &block)
135
+ default = options[:default] || ''
136
+ value = nil
137
+ while value.blank?
138
+ print "#{text} [#{default}]: "
139
+ value = STDIN.gets.chomp!
140
+ value = default if value.blank?
141
+ value = yield value if block_given?
142
+ value
143
+ end
144
+ value
145
+ end
146
+
147
+ end if RUN_ONLINE_TESTS
data/test/post_test.rb ADDED
@@ -0,0 +1,68 @@
1
+ #
2
+ # = WWW::Delicious
3
+ #
4
+ # Ruby client for del.icio.us API.
5
+ #
6
+ #
7
+ # Category:: WWW
8
+ # Package:: WWW::Delicious
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # License:: MIT License
11
+ #
12
+ #--
13
+ #
14
+ #++
15
+
16
+
17
+ require 'test_helper'
18
+ require 'www/delicious/post'
19
+
20
+
21
+ class PostTest < Test::Unit::TestCase
22
+
23
+ def test_post
24
+ expected = { :title => 'JavaScript DOM reference', :url => URI.parse('http://www.howtocreate.co.uk/tutorials/texterise.php?dom=1') }
25
+ assert_attributes(instance(expected), expected)
26
+ end
27
+
28
+ def test_post_shared_defaults_to_true
29
+ assert(instance(:title => 'Foo').shared)
30
+ end
31
+
32
+ def test_post_shared_is_false
33
+ assert(!instance(:title => 'Foo', :shared => false).shared)
34
+ end
35
+
36
+ def test_post_from_rexml
37
+ dom = REXML::Document.new(File.read(TESTCASES_PATH + '/element/post.xml'))
38
+ element = WWW::Delicious::Post.from_rexml(dom.root)
39
+
40
+ assert_equal(URI.parse('http://www.howtocreate.co.uk/tutorials/texterise.php?dom=1'), element.url)
41
+ assert_equal("JavaScript DOM reference", element.title)
42
+ assert_equal("dom reference", element.notes)
43
+ assert_equal("c0238dc0c44f07daedd9a1fd9bbdeebd", element.uid)
44
+ assert_equal(55, element.others)
45
+ assert_equal(%w(dom javascript webdev), element.tags)
46
+ assert_equal(Time.parse("2005-11-28T05:26:09Z"), element.time)
47
+ end
48
+
49
+ def test_post_from_rexml_not_shared
50
+ dom = REXML::Document.new(File.read(TESTCASES_PATH + '/element/post_unshared.xml'))
51
+ element = WWW::Delicious::Post.from_rexml(dom.root)
52
+
53
+ assert(!element.shared)
54
+ end
55
+
56
+
57
+ # def test_valid
58
+ # end
59
+
60
+
61
+ protected
62
+
63
+ # returns a stub instance
64
+ def instance(values = {}, &block)
65
+ WWW::Delicious::Post.new(values)
66
+ end
67
+
68
+ end