timelog4r 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/timelog4r.rb ADDED
@@ -0,0 +1,513 @@
1
+ #!ruby -Ku
2
+ $:.unshift(File.dirname(__FILE__)) unless
3
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
4
+
5
+ require 'timelog4r/utils'
6
+ require 'timelog4r/xml_parser'
7
+
8
+ =begin rdoc
9
+ Timelog4r is TimelogAPI for ruby.
10
+
11
+ see example/*.rb
12
+
13
+ about TimelogAPI
14
+ http://timelog.jp/api.asp
15
+ =end
16
+ class Timelog4r
17
+ # OPTIONAL:: Set your Program's 'UserAgent'.
18
+ attr_accessor :user_agent
19
+ # REQUIRED:: Set your Timelog User-ID.
20
+ attr_accessor :user_id
21
+ # REQUIRED:: Set your Timelog PassWord.
22
+ attr_accessor :password
23
+
24
+ # TimelogAPI's BaseURI.
25
+ BaseAddress = 'http://api.timelog.jp/'
26
+
27
+ # Utilities.
28
+ include Utils
29
+ # Customized XML-Parser.
30
+ include XML_Parser
31
+
32
+ def initialize #:nodoc:
33
+ @user_agent = 'timelog4r'
34
+ end
35
+
36
+ =begin rdoc
37
+ Description::
38
+ get public timeline[http://timelog.jp/home/public.asp]
39
+
40
+ Params::
41
+ options {
42
+
43
+ :cnt => entry_count [Fixnum],
44
+ :stat => entry_type(:bm|:gn) [String],
45
+ :since => entry_after_since [Time]
46
+
47
+ } [Hash] default nil
48
+
49
+ Return::
50
+ {:title => 'Timelog ... ', entries => [...]}
51
+
52
+ RelatedMethods::
53
+ Utils#parse_options
54
+ Utils#create_uri
55
+ Utils#create_request
56
+ Utils#http_access
57
+ XML_Parser#parse_timeline
58
+
59
+ =end
60
+ def get_public_timeline(options = nil)
61
+ address = BaseAddress + 'public_msg.asp'
62
+ address += parse_options(options) if options
63
+ uri = create_uri(address)
64
+ request = create_request(:get, uri)
65
+ response = http_access(uri, request)
66
+ result = parse_timeline(response) if response
67
+ return result
68
+ end
69
+
70
+ =begin rdoc
71
+ Description::
72
+ get user and friends timeline[http://timelog.jp/home]
73
+
74
+ Params::
75
+ options {
76
+
77
+ :cnt => entry_count [Fixnum],
78
+ :since => entry_after_since [Time]
79
+
80
+ } [Hash] default nil
81
+
82
+ Return::
83
+ {:title => 'Timelog ... ', :entries => [...]}
84
+
85
+ RelatedMethods::
86
+ Utils#parse_options
87
+ Utils#create_uri
88
+ Utils#create_request
89
+ Utils#http_access
90
+ XML_Parser#parse_timeline
91
+
92
+ =end
93
+ def get_friends_timeline(options = nil)
94
+ address = BaseAddress + 'friends_msg.asp'
95
+ address += parse_options(options) if options
96
+ uri = create_uri(address)
97
+ request = create_request(:get, uri, true)
98
+ response = http_access(uri, request)
99
+ result = parse_timeline(response) if response
100
+ return result
101
+ end
102
+
103
+ =begin rdoc
104
+ Description::
105
+ get my timeline[http:timelog.jp/home/?ac=user_id]
106
+
107
+ Params::
108
+ options {
109
+
110
+ :cnt => entry_count [Fixnum],
111
+ :stat => entry_tipe [String],
112
+ :since => entry_after_since [Time],
113
+ :tag => find_entries_from_tag [String]
114
+
115
+ } [Hash] default nil
116
+
117
+ Return::
118
+ {:title => 'Timelog ... ', :entries => [...]}
119
+
120
+ RelatedMethods::
121
+ Utils#parse_options
122
+ Utils#create_uri
123
+ Utils#create_request
124
+ Utils#http_access
125
+ XML_Parser#parse_timeline
126
+
127
+ =end
128
+ def get_my_timeline(options = nil)
129
+ address = BaseAddress + 'my_msg.asp'
130
+ address += parse_options(options) if options
131
+ uri = create_uri(address)
132
+ request = create_request(:get, uri, true)
133
+ response = http_access(uri, request)
134
+ result = parse_timeline(response) if response
135
+ return result
136
+ end
137
+
138
+ =begin rdoc
139
+ Description::
140
+ get my profile[http://user_id.timelog.jp]
141
+
142
+ Return::
143
+ {:title => 'Timelog ... ', :user => {...} }
144
+
145
+ RelatedMethods::
146
+ Utils#parse_options
147
+ Utils#create_uri
148
+ Utils#create_request
149
+ Utils#http_access
150
+ XML_Parser#parse_profile
151
+
152
+ =end
153
+ def get_profile
154
+ address = BaseAddress + 'show.asp'
155
+ uri = create_uri(address)
156
+ request = create_request(:get, uri, true)
157
+ response = http_access(uri, request)
158
+ result = parse_profile(response) if response
159
+ return result
160
+ end
161
+
162
+ =begin rdoc
163
+ Description::
164
+ get friends list[http://timelog.jp/profile/frito.asp?ac=user_id]
165
+
166
+ Params::
167
+ options {
168
+ :cnt => friend_count [Fixnum]
169
+ } [Hash] default nil
170
+
171
+ Return::
172
+ {:title => 'Timelog ... ', :entries => [...]}
173
+
174
+ RelatedMethods::
175
+ Utils#parse_options
176
+ Utils#create_uri
177
+ Utils#create_request
178
+ Utils#http_access
179
+ XML_Parser#parse_user_list
180
+
181
+ =end
182
+ def get_friend_list(options = nil)
183
+ address = BaseAddress + 'friends.asp'
184
+ address += parse_options(options) if options
185
+ uri = create_uri(address)
186
+ request = create_request(:get, uri, true)
187
+ response = http_access(uri, request)
188
+ result = parse_user_list(response) if response
189
+ return result
190
+ end
191
+
192
+ =begin rdoc
193
+ Description::
194
+ get fan list[http://timelog.jp/profile/frifrom.asp?ac=user_id]
195
+
196
+ Params::
197
+ options {
198
+ :cnt => user_count [Fixnum]
199
+ } [Hash] default nil
200
+
201
+ Return::
202
+ {:title => 'Timelog ... ', :entries => [...]}
203
+
204
+ RelatedMethods::
205
+ Utils#parse_options
206
+ Utils#create_uri
207
+ Utils#create_request
208
+ Utils#http_access
209
+ XML_Parser#parse_user_list
210
+
211
+ =end
212
+ def get_fan_list(options = nil)
213
+ address = BaseAddress + 'fan.asp'
214
+ address += parse_options(options) if options
215
+ uri = create_uri(address)
216
+ request = create_request(:get, uri, true)
217
+ response = http_access(uri, request)
218
+ result = parse_user_list(response) if response
219
+ return result
220
+ end
221
+
222
+ =begin rdoc
223
+ Description::
224
+ get tag list[http://timelog.jp/taglist.asp]
225
+
226
+ Params::
227
+ options {
228
+ :cnt => tag_count [Fixnum]
229
+ } [Hash] default nil
230
+
231
+ Return::
232
+ {:title => 'Timelog ... ', :tags => [...]}
233
+
234
+ RelatedMethods::
235
+ Utils#parse_options
236
+ Utils#create_uri
237
+ Utils#create_request
238
+ Utils#http_access
239
+ XML_Parser#parse_tag_list
240
+
241
+ =end
242
+ def get_tag_list(options = nil)
243
+ address = BaseAddress + 'tags.asp'
244
+ address += parse_options(options) if options
245
+ uri = create_uri(address)
246
+ request = create_request(:get, uri, true)
247
+ response = http_access(uri, request)
248
+ result = parse_tag_list(response) if response
249
+ return result
250
+ end
251
+
252
+ =begin rdoc
253
+ Description::
254
+ get direct messages[http://timelog.jp/profile/message.asp]
255
+
256
+ Params::
257
+ options {
258
+ :cnt => message_count [Fixnum]
259
+ } [Hash] default nil
260
+
261
+ Return::
262
+ {:title => 'Timelog ... ', :entries => [...]}
263
+
264
+ RelatedMethods::
265
+ Utils#parse_options
266
+ Utils#create_uri
267
+ Utils#create_request
268
+ Utils#http_access
269
+ XML_Parser#parse_timeline
270
+
271
+ =end
272
+ def get_direct_messages(options = nil)
273
+ address = BaseAddress + 'direct_msg.asp'
274
+ address += parse_options(options) if options
275
+ uri = create_uri(address)
276
+ request = create_request(:get, uri, true)
277
+ response = http_access(uri, request)
278
+ result = parse_timeline(response) if response
279
+ return result
280
+ end
281
+
282
+ =begin rdoc
283
+ Description::
284
+ get reply to my memos[http://timelog.jp/profile/remsg.asp]
285
+
286
+ Params::
287
+ options {
288
+ :cnt => entry_count [Fixnum]
289
+ } [Hash] default nil
290
+
291
+ Return::
292
+ {:title => 'Timelog ... ', :entries => [...]}
293
+
294
+ RelatedMethods::
295
+ Utils#parse_options
296
+ Utils#create_uri
297
+ Utils#create_request
298
+ Utils#http_access
299
+ XML_Parser#parse_timeline
300
+
301
+ =end
302
+ def get_reply_list(options = nil)
303
+ address = BaseAddress + 'res_msg.asp'
304
+ address += parse_options(options) if options
305
+ uri = create_uri(address)
306
+ request = create_request(:get, uri, true)
307
+ response = http_access(uri, request)
308
+ result = parse_timeline(response) if response
309
+ return result
310
+ end
311
+
312
+ =begin rdoc
313
+ Description::
314
+ set tags to text.
315
+
316
+ Params::
317
+
318
+ :text memo's body [String]
319
+ :tags set tags [Array]
320
+
321
+ Return::
322
+ text [String]
323
+
324
+ =end
325
+ def set_tags(text, tags)
326
+ text += '[' + tags.join(',') + ']'
327
+ return text
328
+ end
329
+
330
+ =begin rdoc
331
+ Description::
332
+ update memo.
333
+ insert :text in 'Timelog Command' Support.
334
+
335
+ Params::
336
+
337
+ :text memo's body [Stirng]
338
+ :res_id response to memo_id [String] default nil
339
+ :tags update tags [Array] default nil
340
+
341
+ Return::
342
+
343
+ true (case of 'update success')
344
+ false (case of 'update failed')
345
+
346
+ RelatedMethods::
347
+ set_tags
348
+ Utils#parse_options
349
+ Utils#create_uri
350
+ Utils#create_request
351
+ Utils#http_access
352
+
353
+ =end
354
+ def update(text, res_id = nil, tags = nil)
355
+ text = set_tags(text, tags) if tags
356
+ params = {:text => text}
357
+ params[:remsgid] = res_id if res_id
358
+ address = BaseAddress + 'new.asp' + parse_options(params)
359
+ uri = create_uri(address)
360
+ request = create_request(:post, uri, true)
361
+ response = http_access(uri, request)
362
+ return !response.nil?
363
+ end
364
+
365
+ =begin rdoc
366
+ Description::
367
+ send a direct message to timelog-user.
368
+
369
+ Params::
370
+
371
+ :text memo's body [String]
372
+ :to send to timelog user_id [String]
373
+ :res_id response to memo_id [String] default nil
374
+ :tags update tags [Array] default nil
375
+
376
+ Return::
377
+
378
+ true (case of 'send message success')
379
+ false (case of 'send message failed')
380
+
381
+ RelatedMethods::
382
+ update
383
+
384
+ =end
385
+ def send_message(text, to, res_id = nil, tags = nil)
386
+ text = '/d @' + to + ' ' + text
387
+ update(text, res_id, tags)
388
+ end
389
+
390
+ =begin rdoc
391
+ Description::
392
+ update memo type of todo.
393
+
394
+ Params::
395
+
396
+ :text memo's body [String]
397
+ :time todo end-time [Time]
398
+ :res_id response to memo_id [String] default nil
399
+ :tags update tags [Array] default nil
400
+
401
+ Return::
402
+
403
+ true (case of 'update success')
404
+ false (case of 'update failed')
405
+
406
+ RelatedMethods::
407
+ update
408
+
409
+ =end
410
+ def set_todo(text, time, res_id = nil, tags = nil)
411
+ text = '/t ' +
412
+ time.strftime('%Y%m%d%H%M') +
413
+ ' ' + text
414
+ update(text, res_id, tags)
415
+ end
416
+
417
+ =begin rdoc
418
+ Description::
419
+ update memo type of bookmark.
420
+
421
+ Params::
422
+
423
+ :text memo's body [String]
424
+ :uri bookmarks uri [URI]
425
+ :res_id response to memo_id [String] default nil
426
+ :tags update tags [Array] default nil
427
+
428
+ Return::
429
+
430
+ true (case of 'update success')
431
+ false (case of 'update failed')
432
+
433
+ RelatedMethods::
434
+ update
435
+
436
+ =end
437
+ def update_bookmark(text, uri, res_id = nil, tags = nil)
438
+ text = '/b ' + uri.to_s + ' ' + text
439
+ update(text, res_id, tags)
440
+ end
441
+
442
+ =begin rdoc
443
+ Description::
444
+ update memo type of good.
445
+
446
+ Params::
447
+
448
+ :text memo's body [String]
449
+ :res_id response to memo_id [String] default nil
450
+ :tags update tags [Array] default nil
451
+
452
+ Return::
453
+
454
+ true (case of 'update success')
455
+ false (case of 'update failed')
456
+
457
+ RelatedMethods::
458
+ update
459
+
460
+ =end
461
+ def update_good(text, res_id = nil, tags = nil)
462
+ text = '/g ' + text
463
+ update(text, res_id, tags)
464
+ end
465
+
466
+ =begin rdoc
467
+ Description::
468
+ update memo type of news.
469
+
470
+ Params::
471
+
472
+ :text memo's body [String]
473
+ :res_id response to memo_id [String] default nil
474
+ :tags update tags [Array] default nil
475
+
476
+ Return::
477
+
478
+ true (case of 'update success')
479
+ false (case of 'update failed')
480
+
481
+ RelatedMethods::
482
+ update
483
+
484
+ =end
485
+ def update_news(text, res_id = nil, tags = nil)
486
+ text = '/n ' + text
487
+ update(text, res_id, tags)
488
+ end
489
+
490
+ =begin rdoc
491
+ Description::
492
+ update memo type of vote.
493
+
494
+ Params::
495
+
496
+ :text memo's body [String]
497
+ :res_id response to memo_id [String] default nil
498
+ :tags update tags [Array] default nil
499
+
500
+ Return::
501
+
502
+ true (case of 'update success')
503
+ false (case of 'update failed')
504
+
505
+ RelatedMethods::
506
+ update
507
+
508
+ =end
509
+ def update_vote(text, res_id = nil, tags = nil)
510
+ text = '/j ' + text
511
+ update(text, res_id, tags)
512
+ end
513
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/timelog4r.rb'}"
9
+ puts "Loading timelog4r gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ GEM_NAME = 'timelog4r' # what ppl will type to install your gem
4
+ RUBYFORGE_PROJECT = 'timelog4r'
5
+
6
+ require 'rubygems'
7
+ begin
8
+ require 'newgem'
9
+ require 'rubyforge'
10
+ rescue LoadError
11
+ puts "\n\nGenerating the website requires the newgem RubyGem"
12
+ puts "Install: gem install newgem\n\n"
13
+ exit(1)
14
+ end
15
+ require 'redcloth'
16
+ require 'syntax/convertors/html'
17
+ require 'erb'
18
+ require File.dirname(__FILE__) + "/../lib/#{GEM_NAME}/version.rb"
19
+
20
+ version = Timelog4r::VERSION::STRING
21
+ download = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
22
+
23
+ def rubyforge_project_id
24
+ RubyForge.new.autoconfig["group_ids"][RUBYFORGE_PROJECT]
25
+ end
26
+
27
+ class Fixnum
28
+ def ordinal
29
+ # teens
30
+ return 'th' if (10..19).include?(self % 100)
31
+ # others
32
+ case self % 10
33
+ when 1: return 'st'
34
+ when 2: return 'nd'
35
+ when 3: return 'rd'
36
+ else return 'th'
37
+ end
38
+ end
39
+ end
40
+
41
+ class Time
42
+ def pretty
43
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
44
+ end
45
+ end
46
+
47
+ def convert_syntax(syntax, source)
48
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
49
+ end
50
+
51
+ if ARGV.length >= 1
52
+ src, template = ARGV
53
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
54
+ else
55
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
56
+ exit!
57
+ end
58
+
59
+ template = ERB.new(File.open(template).read)
60
+
61
+ title = nil
62
+ body = nil
63
+ File.open(src) do |fsrc|
64
+ title_text = fsrc.readline
65
+ body_text_template = fsrc.read
66
+ body_text = ERB.new(body_text_template).result(binding)
67
+ syntax_items = []
68
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
69
+ ident = syntax_items.length
70
+ element, syntax, source = $1, $2, $3
71
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
72
+ "syntax-temp-#{ident}"
73
+ }
74
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
75
+ body = RedCloth.new(body_text).to_html
76
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
77
+ end
78
+ stat = File.stat(src)
79
+ created = stat.ctime
80
+ modified = stat.mtime
81
+
82
+ $stdout << template.result(binding)