Class: Milkmaid

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

Defined Under Namespace

Classes: ListNotFound, TaskNotFound

Instance Method Summary collapse

Constructor Details

#initializeMilkmaid

Returns a new instance of Milkmaid.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/milkmaid.rb', line 4

def initialize
  @rtm = RTM::RTM.new(RTM::Endpoint.new('31308536ffed80061df846c3a4564a27', 'c1476318e3483441'))
  @auth = @rtm.auth
  begin
    @config_file = File.join(ENV['HOME'], '.milkmaid')
    @config = YAML.load_file(@config_file)
    @auth.frob = @config[:frob]
    @rtm.token = @config[:token]
    @timeline = @rtm.timelines.create['timeline'] unless @config[:token].nil?
  rescue Errno::ENOENT
    @config = {}
  end
end

Instance Method Details

#add_task(name) ⇒ Object



87
88
89
# File 'lib/milkmaid.rb', line 87

def add_task(name)
  @rtm.tasks.add :name=>name, :parse=>'1', :timeline=>@timeline
end

#auth_finishObject



98
99
100
101
# File 'lib/milkmaid.rb', line 98

def auth_finish
  @config[:token] = @auth.get_token
  save_config
end

#auth_startObject



91
92
93
94
95
96
# File 'lib/milkmaid.rb', line 91

def auth_start
  url = @auth.url
  @config[:frob] = @auth.frob
  save_config
  url
end

#cleanObject



103
104
105
106
# File 'lib/milkmaid.rb', line 103

def clean
  @config.delete_if { |k| k != :token }
  save_config
end

#complete_task(tasknum) ⇒ Object



72
73
74
75
# File 'lib/milkmaid.rb', line 72

def complete_task(tasknum)
  check_task_ids tasknum
  call_rtm_api :complete, tasknum
end

#delete_task(tasknum) ⇒ Object



82
83
84
85
# File 'lib/milkmaid.rb', line 82

def delete_task(tasknum)
  check_task_ids tasknum
  call_rtm_api :delete, tasknum
end

#incomplete_tasks(list = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/milkmaid.rb', line 26

def incomplete_tasks(list=nil)
  params = {}
  unless list.nil?
    if @config[:lists]
      list = @config[:lists][list-1]
      params[:list_id] = list unless list.nil?
    end
    raise ListNotFound if params.empty?
  end
  entries = []
  list_id = nil
  @rtm.tasks.get_list(params)['tasks']['list'].
    as_array.each do |items|
    list_id = items['id']
    if !items['taskseries'].nil?
      items['taskseries'].as_array.each do |taskseries|
        taskseries['list_id'] = list_id
        entries << taskseries if Milkmaid::last_task(taskseries)['completed'].empty?
      end
    end
  end
  entries.sort! do |a, b|
    taska = Milkmaid::last_task a
    taskb = Milkmaid::last_task b
    result = taska['priority'] <=> taskb['priority']
    if result == 0
      if taska['due'].empty?
        1
      elsif taskb['due'].empty?
        -1
      else
        Time.parse(taska['due']) <=> Time.parse(taskb['due'])
      end
    else
      result
    end
  end
  @config[:tasks] = entries.map do |taskseries|
    {:list_id => taskseries['list_id'], 
    :taskseries_id => taskseries['id'],
    :task_id => Milkmaid::last_task(taskseries)['id']}
  end
  save_config
  entries
end

#listsObject



18
19
20
21
22
23
24
# File 'lib/milkmaid.rb', line 18

def lists
  entries = @rtm.lists.get_list['lists']['list'].as_array
  list_ids = entries.map { |list| list['id'] }
  @config[:lists] = list_ids
  save_config
  entries
end

#postpone_task(tasknum) ⇒ Object



77
78
79
80
# File 'lib/milkmaid.rb', line 77

def postpone_task(tasknum)
  check_task_ids tasknum
  call_rtm_api :postpone, tasknum
end