rtmilk 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ #
2
+ # rtm.auth : Auth API
3
+ #
4
+ module RTM
5
+ module Auth
6
+
7
+ class GetFrob < RTM::API
8
+ # return frob
9
+ def parse_result(result)
10
+ super
11
+ result['frob'].first
12
+ end
13
+
14
+ def initialize
15
+ super 'rtm.auth.getFrob'
16
+ end
17
+ end # GetFrob
18
+
19
+ class GetToken < RTM::API
20
+ # return {:token, :perms, :user => {id, name, fullname}}
21
+ def parse_result(result)
22
+ super
23
+ Auth.parse_result_auth(result)
24
+ end
25
+
26
+ def initialize(frob)
27
+ super 'rtm.auth.getToken'
28
+ @param = {:frob => frob}
29
+ end
30
+ end # GetToken
31
+
32
+ class CheckToken < RTM::API
33
+ # return {:token, :perms, :user => {id, name, fullname}}
34
+ def parse_result(result)
35
+ super
36
+ Auth.parse_result_auth(result)
37
+ end
38
+
39
+ def initialize(token)
40
+ super 'rtm.auth.checkToken'
41
+ @param = {:auth_token => token}
42
+ end
43
+ end # CheckToken
44
+
45
+ private
46
+ # return {:token, :perms, :user => {id, name, fullname}}
47
+ def Auth.parse_result_auth(result)
48
+ auth = result['auth'].first
49
+ user = auth['user'].first
50
+ parsed = {
51
+ :token => auth['token'].first,
52
+ :perms => auth['perms'].first,
53
+ :user => {
54
+ :id => user['id'],
55
+ :name => user['username'],
56
+ :fullname => user['fullname'] }
57
+ }
58
+ end
59
+
60
+ end # Auth
61
+ end # RTM
62
+ # vim:fdm=indent
@@ -0,0 +1,177 @@
1
+ #
2
+ # rtm.lists : Lists API
3
+ #
4
+
5
+ module RTM
6
+ #
7
+ # rtm.lists : Lists API
8
+ #
9
+ module Lists
10
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.add.rtm
11
+ class Add < RTM::API
12
+ # return [name, smart, filter?, id, archived, deleted, position, locked]*
13
+ def parse_result(result)
14
+ super
15
+ result['list'].first
16
+ end
17
+
18
+ def initialize(token, name, filter=nil)
19
+ super 'rtm.lists.add', token, RTM::Timeline.new(token).to_s
20
+ @param['name'] = name
21
+ if filter
22
+ @param['filter'] = filter
23
+ end
24
+ end
25
+ end # Add
26
+
27
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.archive.rtm
28
+ class Archive < RTM::API
29
+ end # Archive
30
+
31
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.delete.rtm
32
+ class Delete < RTM::API
33
+ # return [name, smart, filter?, id, archived, deleted, position, locked]*
34
+ def parse_result(result)
35
+ super
36
+ result['list'].first
37
+ end
38
+
39
+ def initialize(token, id)
40
+ super 'rtm.lists.delete', token, RTM::Timeline.new(token).to_s
41
+ @param['list_id'] = id
42
+ end
43
+ end # Delete
44
+
45
+ class GetList < RTM::API
46
+ # return [name, smart, filter?, id, archived, deleted, position, locked]*
47
+ def parse_result(result)
48
+ super
49
+ result['lists'].first['list']
50
+ end
51
+
52
+ def initialize(token)
53
+ super 'rtm.lists.getList', token
54
+ end
55
+ end # GetList
56
+
57
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.setDefaultList.rtm
58
+ class SetDefaultList < RTM::API
59
+ end # SetDefaultList
60
+
61
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.setName.rtm
62
+ class SetName < RTM::API
63
+ # return [name, smart, filter?, id, archived, deleted, position, locked]*
64
+ def parse_result(result)
65
+ super
66
+ result['list'].first
67
+ end
68
+
69
+ def initialize(token, id, name)
70
+ super 'rtm.lists.setName', token, RTM::Timeline.new(token).to_s
71
+ @param['list_id'] = id
72
+ @param['name'] = name
73
+ end
74
+ end # SetName
75
+
76
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.unarchive.rtm
77
+ class UnArchive < RTM::API
78
+ end # UnArchive
79
+ end # Lists
80
+
81
+ # more friendly class.
82
+ class List
83
+ #attr_reader :name, :smart, :id, :archived, :deleted, :position, :locked
84
+
85
+ def initialize(arg)
86
+ if arg.class == String # name
87
+ @hash = RTM::Lists::Add.new(RTM::API.token, arg).invoke
88
+ elsif arg.class == Hash # already constructed
89
+ @hash = arg
90
+ else
91
+ raise RTM::Error, "invalid argument"
92
+ end
93
+ end
94
+
95
+ def name; @hash['name']; end
96
+ def id; @hash['id']; end
97
+ def position; @hash['position']; end
98
+ def smart?; @hash['smart'] == '1'; end
99
+ def archived?; @hash['archived'] == '1'; end
100
+ def locked?; @hash['locked'] == '1'; end
101
+ def deleted?; @hash['deleted'] == '1'; end
102
+
103
+ def name=(new_name)
104
+ RTM::API::SetName(RTM::API.token, @id, new_name)
105
+ end
106
+
107
+ # archive this list.
108
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.archive.rtm
109
+ def archive
110
+ end
111
+
112
+ # unarchive this list.
113
+ def unarchive
114
+ end
115
+
116
+ # delete this list.
117
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.delete.rtm
118
+ def delete
119
+ deleted = RTM::Lists::Delete.new(RTM::API.token, @hash['id']).invoke
120
+ end
121
+
122
+ # set this list as default.
123
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.setDefaultList.rtm
124
+ def set_default
125
+ end
126
+
127
+ # -----------------------------------------------------------------
128
+ # class methods
129
+ #
130
+
131
+ # find a List by name.
132
+ def List.find(name, alive_only=true) # by name
133
+ lists = RTM::Lists::GetList.new(RTM::API.token).invoke
134
+ lists.find do |list|
135
+ if alive_only
136
+ list['name'] == name && list['archived'] == '0' && list['deleted'] == '0'
137
+ else
138
+ list['name'] == name
139
+ end
140
+ end
141
+ end
142
+
143
+ # find all Lists.
144
+ def List.find_all(name, alive_only=true) # by name
145
+ lists = RTM::Lists::GetList.new(RTM::API.token).invoke
146
+ lists.find_all do |list|
147
+ if alive_only
148
+ list['name'] == name && list['archived'] == '0' && list['deleted'] == '0'
149
+ else
150
+ list['name'] == name
151
+ end
152
+ end.collect do |x|
153
+ List.new(x)
154
+ end
155
+ end
156
+
157
+ # find all alive Lists.
158
+ def List.alive_all
159
+ lists = RTM::Lists::GetList.new(RTM::API.token).invoke
160
+ lists.find_all do |list|
161
+ list['archived'] == '0' && list['deleted'] == '0'
162
+ end.collect do |x|
163
+ List.new(x)
164
+ end
165
+ end
166
+
167
+ # http://www.rememberthemilk.com/services/api/methods/rtm.lists.getList.rtm
168
+ def List.get
169
+ end
170
+
171
+ def List.inbox
172
+ List.find('Inbox')
173
+ end
174
+ end # List
175
+
176
+ end # RTM
177
+ # vim:fdm=indent
@@ -0,0 +1,141 @@
1
+ #
2
+ # rtm.tasks.notes : Notes API
3
+ #
4
+ require 'test/unit/assertions'
5
+
6
+ module RTM
7
+ module Tasks
8
+ module Notes
9
+
10
+ class Add < RTM::API
11
+ def parse_result(result)
12
+ super
13
+ [result['note'].first, result['transaction'].first]
14
+ end
15
+
16
+ def initialize(token, timeline, list, taskseries, task, title, text)
17
+ super 'rtm.tasks.notes.add', token
18
+ @param[:timeline] = timeline
19
+ @param[:list_id] = list
20
+ @param[:taskseries_id] = taskseries
21
+ @param[:task_id] = task
22
+ @param[:note_title] = title
23
+ @param[:note_text] = text
24
+ end
25
+ end # Add
26
+
27
+ class Delete < RTM::API
28
+ def parse_result(result)
29
+ super
30
+ end
31
+
32
+ def initialize(token, timeline, note)
33
+ super 'rtm.tasks.notes.delete', token
34
+ @param[:timeline] = timeline
35
+ @param[:note_id] = note
36
+ end
37
+ end # Delete
38
+
39
+ class Edit < RTM::API
40
+ def parse_result(result)
41
+ super
42
+ [result['note'].first, result['transaction'].first]
43
+ end
44
+
45
+ def initialize(token, timeline, note, title, text)
46
+ super 'rtm.tasks.notes.edit', token
47
+ @param[:timeline] = timeline
48
+ @param[:note_id] = note
49
+ @param[:note_title] = title
50
+ @param[:note_text] = text
51
+ end
52
+ end # Add
53
+
54
+ end # Notes
55
+ end # Tasks
56
+
57
+ # = a Note.
58
+ #
59
+ # belongs to Task (TaskSeries).
60
+ #
61
+ # == usage:
62
+ # * n = RTM::Note.new(:task=>_task_, :title=>'yeah', :body=>'you')
63
+ # * n.title = 'newtitle'
64
+ # * n.body = 'newbody'
65
+ # * n.delete
66
+ #
67
+ class Note
68
+ include Test::Unit::Assertions
69
+
70
+ def id; @hash['id']; end
71
+ def title; @hash['title']; end
72
+ def modified; @hash['modified']; end
73
+ def body; @hash['content']; end
74
+ def created; @hash['created']; end
75
+
76
+ def initialize(arg)
77
+ assert_equal(Hash, arg.class)
78
+
79
+ if arg.has_key? :hash # already constructed
80
+ @hash = arg[:hash]
81
+ else # from scratch
82
+ assert(arg.has_key?(:task))
83
+ assert_equal(RTM::Task, arg[:task].class)
84
+ assert(arg.has_key?(:title))
85
+ assert(arg.has_key?(:body))
86
+
87
+ @hash, transaction = RTM::Tasks::Notes::Add.new(
88
+ RTM::API.token,
89
+ RTM::Timeline.new(RTM::API.token).to_s,
90
+ arg[:task].list, arg[:task].id,
91
+ arg[:task].chunks.first.id, # XXX: Note belongs to TaskSeries, why need Task id ?
92
+ arg[:title],
93
+ arg[:body]).invoke
94
+ end
95
+ end
96
+
97
+ # alter title field.
98
+ def title=(text)
99
+ ret, transaction = RTM::Tasks::Notes::Edit.new(
100
+ RTM::API.token,
101
+ RTM::Timeline.new(RTM::API.token).to_s,
102
+ id,
103
+ text,
104
+ body).invoke
105
+ assert(ret['id'], id)
106
+ @hash['title'] = text
107
+ end
108
+
109
+ # alter body field.
110
+ def body=(text)
111
+ ret, transaction = RTM::Tasks::Notes::Edit.new(
112
+ RTM::API.token,
113
+ RTM::Timeline.new(RTM::API.token).to_s,
114
+ id,
115
+ title,
116
+ text).invoke
117
+ assert(ret['id'], id)
118
+ @hash['content'] = text
119
+ end
120
+
121
+ # delete this note.
122
+ # after deleted, should not touch this object.
123
+ def delete
124
+ RTM::Tasks::Notes::Delete.new(
125
+ RTM::API.token,
126
+ RTM::Timeline.new(RTM::API.token).to_s,
127
+ id).invoke
128
+ end
129
+
130
+ def Note.update(arg)
131
+ ret, transaction = RTM::Tasks::Notes::Edit.new(
132
+ RTM::API.token,
133
+ RTM::Timeline.new(RTM::API.token).to_s,
134
+ arg[:id],
135
+ arg[:title],
136
+ arg[:body]).invoke
137
+ end
138
+ end # Note
139
+
140
+ end # RTM
141
+ # vim:fdm=indent
@@ -0,0 +1,196 @@
1
+ #
2
+ # rtm.tasks : Tasks API
3
+ #
4
+ require 'test/unit/assertions'
5
+ module RTM
6
+ module Tasks
7
+
8
+ # get all TaskSeries.
9
+ class GetList < RTM::API
10
+ # return [list_id, taskseries*]*
11
+ def parse_result(result)
12
+ super
13
+ result['tasks'].first['list']
14
+ end
15
+
16
+ def initialize(token, list_id=nil, filter=nil, last_sync=nil)
17
+ super 'rtm.tasks.getList', token
18
+ @param[:list_id] = list_id if list_id
19
+ @param[:filter] = filter if filter
20
+ @param[:last_sync] = last_sync if last_sync
21
+ end
22
+ end # GetList
23
+
24
+ class Add < RTM::API
25
+ def parse_result(result)
26
+ super
27
+ [result['list'].first, result['transaction']]
28
+ end
29
+
30
+ def initialize(token, timeline, list, name)
31
+ super 'rtm.tasks.add', token
32
+ @token = token
33
+ @param[:timeline] = timeline
34
+ @param[:list_id] = list
35
+ @param[:name] = name
36
+ end
37
+ end # Add
38
+
39
+ class Delete < RTM::API
40
+ def parse_result(result)
41
+ super
42
+ [result['list'].first, result['transaction']]
43
+ end
44
+
45
+ def initialize(token, timeline, list, taskseries, task)
46
+ super 'rtm.tasks.delete', token
47
+ @param[:timeline] = timeline
48
+ @param[:list_id] = list
49
+ @param[:taskseries_id] = taskseries
50
+ @param[:task_id] = task
51
+ end
52
+ end # Delete
53
+
54
+ end # Tasks
55
+
56
+
57
+ #
58
+ # Task class.
59
+ # Unfortunately, RTM API defines this object as TaskSeries.
60
+ # That seemes to be unnatural.
61
+ class Task
62
+ attr_reader :list, :chunks, :notes
63
+
64
+ include Test::Unit::Assertions
65
+
66
+ def name; @taskseries['name']; end
67
+ def id; @taskseries['id']; end
68
+ def modified; @taskseries['modified']; end
69
+ def tags; @taskseries['tags']; end
70
+ def participants; @taskseries['participants']; end
71
+ def url; @taskseries['url']; end
72
+ def created; @taskseries['created']; end
73
+ def source; @taskseries['source']; end
74
+
75
+ # find a Task by name.
76
+ def Task.find(arg)
77
+ all_tasks(arg[:list]).find do |task|
78
+ task.name =~ /#{arg[:name]}/
79
+ end
80
+ end
81
+
82
+ # find all tasks by list, and name
83
+ def Task.find_all(arg)
84
+ tasks = all_tasks(arg[:list])
85
+ if arg.has_key? :name
86
+ tasks.find_all do |task|
87
+ task['name'] =~ /#{arg[:name]}/
88
+ end
89
+ else
90
+ tasks
91
+ end
92
+ end
93
+
94
+ private
95
+ def Task.all_tasks(list = nil)
96
+ result = RTM::Tasks::GetList.new(RTM::API.token, list).invoke
97
+ ret = []
98
+
99
+ result.each do |x|
100
+ if x.has_key? 'taskseries'
101
+ x['taskseries'].each do |y|
102
+ ret.push Task.new(:taskseries => y, :list => x['id'])
103
+ end
104
+ end
105
+ end
106
+ ret
107
+ end
108
+
109
+ public
110
+ # create a Task.
111
+ def initialize(arg)
112
+ assert_equal(Hash, arg.class)
113
+
114
+ @list = if arg.has_key? :list
115
+ arg[:list]
116
+ else
117
+ RTM::List.inbox['id']
118
+ end
119
+
120
+ if arg.has_key? :name # create from scratch
121
+ result,transaction = RTM::Tasks::Add.new(
122
+ RTM::API.token,
123
+ RTM::Timeline.new(RTM::API.token).to_s,
124
+ @list, arg[:name]).invoke
125
+ @taskseries = result['taskseries'].first
126
+ assert(@list, result['id'])
127
+ create_chunks
128
+ @notes = []
129
+ else
130
+ assert(arg.has_key?(:taskseries))
131
+ @taskseries = arg[:taskseries]
132
+ create_chunks
133
+ create_notes
134
+ end
135
+ end
136
+
137
+ # delete a Task and its all chunks.
138
+ def delete
139
+ chunks.collect { |chunk| chunk.delete(id, @list) }
140
+ end
141
+
142
+ def addNote(arg)
143
+ assert_equal(Hash, arg.class)
144
+
145
+ n = RTM::Note.new(
146
+ :task => self,
147
+ :title => arg[:title],
148
+ :body => arg[:body])
149
+ @notes.push n
150
+ end
151
+
152
+ private
153
+ def create_chunks
154
+ @chunks = []
155
+ @taskseries['task'].each do |t|
156
+ @chunks.push Chunk.new(t)
157
+ end
158
+ end
159
+
160
+ def create_notes
161
+ @notes = []
162
+ assert_equal(Array, @taskseries['notes'].class)
163
+ assert_equal(Hash, @taskseries['notes'].first.class)
164
+ if @taskseries['notes'].first.has_key? 'note'
165
+ @taskseries['notes'].first['note'].each do |n|
166
+ @notes.push Note.new(:hash => n)
167
+ end
168
+ end
169
+ end
170
+ end # Task
171
+
172
+ # correspond to each Task.
173
+ class Chunk
174
+ def completed; @hash['completed']; end
175
+ def added; @hash['added']; end
176
+ def postponed; @hash['postponed']; end # TODO
177
+ def priority; @hash['priority']; end
178
+ def id; @hash['id']; end
179
+ def deleted; @hash['deleted']; end
180
+ def has_due_time; @hash['has_due_time'] == '1' ; end
181
+ def estimate; @hash['estimate']; end
182
+ def due; @hash['due']; end
183
+
184
+ def initialize(hash)
185
+ @hash = hash
186
+ end
187
+
188
+ def delete(series, list)
189
+ token = RTM::API.token
190
+ timeline = RTM::Timeline.new(RTM::API.token).to_s
191
+ RTM::Tasks::Delete.new(token, timeline, list, series, id).invoke # TODO
192
+ end
193
+ end # Chunk
194
+
195
+ end # RTM
196
+ # vim:fdm=indent