Class: GoogleTask

Inherits:
Object
  • Object
show all
Defined in:
lib/gtasks.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GoogleTask

Returns a new instance of GoogleTask.



253
254
255
256
257
258
259
260
261
# File 'lib/gtasks.rb', line 253

def initialize(options={})
  @client_id = options[:client_id] or raise ArgumentError.new
  @client_secret = options[:client_secret] or raise ArgumentError.new
  @logger = options[:logger] ||= Logger.new(nil)
  @no_refresh = options[:no_refresh] ||= false
  @callback_uri = options[:callback_uri] ||= nil

  @gs_api = GoogleTaskAPI.new(:client_id => @client_id, :client_secret => @client_secret, :logger => @logger, :no_refresh => @no_refresh, :callback_uri => @callback_uri)
end

Instance Method Details

#add(task_name, list_ident = '@default') ⇒ Object



271
272
273
# File 'lib/gtasks.rb', line 271

def add(task_name, list_ident = '@default')
  @gs_api.task_insert(list_ident, :title => task_name)
end

#all_tasks(username = '@me') ⇒ Object



324
325
326
327
328
329
330
331
# File 'lib/gtasks.rb', line 324

def all_tasks(username = '@me')
  hash = {}
  @gs_api.tasklist_list(username)["items"].map{ |item|
    hash[item["title"]] ||= []
    hash[item["title"]] += tasks(item["id"]).map{|e| e["title"]}
  }
  hash
end

#clear(list_ident = '@default') ⇒ Object



267
268
269
# File 'lib/gtasks.rb', line 267

def clear(list_ident = '@default')
  @gs_api.task_clear(list_ident)
end

#delete(task_number, list_ident = '@default') ⇒ Object



286
287
288
289
290
# File 'lib/gtasks.rb', line 286

def delete(task_number, list_ident = '@default')
  task_proc(task_number){ |task|
    @gs_api.task_delete(list_ident, task["id"])
  }
end

#delete_if(regexp, list_ident = '@default') ⇒ Object



292
293
294
295
296
297
298
# File 'lib/gtasks.rb', line 292

def delete_if(regexp, list_ident = '@default')
  regexp_if(regexp, list_ident){ |item|
    @logger.info "try to delete: #{item["title"]} (#{item["id"]})"
    @gs_api.task_delete(list_ident, item["id"])
    @logger.info "deleted: #{item["title"]}"
  }
end

#done(task_number, list_ident = '@default') ⇒ Object



306
307
308
309
310
# File 'lib/gtasks.rb', line 306

def done(task_number, list_ident = '@default')
  task_proc(task_number) { |task|
    @gs_api.task_update(list_ident, task["id"], "status" => "completed")
  }
end

#done_if(regexp, list_ident = '@default') ⇒ Object



312
313
314
315
316
317
318
# File 'lib/gtasks.rb', line 312

def done_if(regexp, list_ident = '@default')
  regexp_if(regexp, list_ident){ |item|
    @logger.info "try to done task: #{item["title"]} (#{item["id"]})"
    @gs_api.task_update(list_ident, item["id"], "status" => "completed")
    @logger.info "updated: #{item["title"]}"
  }
end

#lists(username = '@me') ⇒ Object



320
321
322
# File 'lib/gtasks.rb', line 320

def lists(username = '@me')
  @gs_api.tasklist_list(username)["items"]
end

#regexp_if(regexp, list_ident = '@default') ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'lib/gtasks.rb', line 275

def regexp_if(regexp, list_ident = '@default')
  matched = []
  @gs_api.task_list(list_ident)["items"].each{ |item|
    if regexp.match(item["title"])
      yield(item) if block_given?
      matched << item
    end
  }
  matched
end

#show(username = '@me') ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/gtasks.rb', line 333

def show(username = '@me')
  all_tasks.each{ |list_title, tasks|
    puts "*#{list_title}"
    tasks.each{ |task|
      puts "\t#{task}"
    }
  }
end

#tasks(list_ident = '@default') ⇒ Object



263
264
265
# File 'lib/gtasks.rb', line 263

def tasks(list_ident = '@default')
  @gs_api.task_list(list_ident)["items"]
end

#update(task_number, update_info = {}, list_ident = '@default') ⇒ Object



300
301
302
303
304
# File 'lib/gtasks.rb', line 300

def update(task_number, update_info = {}, list_ident = '@default')
  task_proc(task_number) { |task|
    @gs_api.task_update(list_ident, task["id"], update_info)
  }
end