Class: Everdone::Todoist

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Todoist

Returns a new instance of Todoist.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/everdone/todoist.rb', line 10

def initialize(config)
    @config = config
    @projects = {}  # map of project id's to project names
    @project_id_by_name = {}  # map of project names to id's
    projects = self.call_api('https://todoist.com/API/getProjects')
    projects.each { |project|
        @projects[project['id']] = project['name']
        @project_id_by_name[project['name']] = project['id']
    }
    @labels = {}  # map of label id's to their name strings
    labels = self.call_api('https://todoist.com/API/getLabels')
    labels.each { |label|
        @labels[label[1]['id']] = label[1]['name']
    }
end

Instance Method Details

#add_item_to_project_by_name(project_name, content, priority) ⇒ Object



40
41
42
43
44
# File 'lib/everdone/todoist.rb', line 40

def add_item_to_project_by_name(project_name, content, priority)
    ret = self.call_api('https://todoist.com/API/addItem',
        params={'project_id'=>@project_id_by_name[project_name],'content'=>content, 'priority'=>priority})
    return ret['id']
end

#add_note(item_id, content) ⇒ Object



46
47
48
49
# File 'lib/everdone/todoist.rb', line 46

def add_note(item_id, content)
    ret = self.call_api('https://todoist.com/API/addNote', params={'item_id'=>item_id,'content'=>content})
    return ret
end

#call_api(url, params = nil) ⇒ Object

helper function for making RESTful calls



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/everdone/todoist.rb', line 27

def call_api(url, params=nil)
    query = { 'token' => @config.todoist_token }
    query.merge!(params) if params
    ret = HTTParty.post(url, {
      :query => query
    })

    if ret.response.code != "200"
      raise "ERROR: Error calling Todoist API #{url}\n   Response Code: #{ret.response.code}\n  Response: \n#{ret.response.body}"
    end
    return ret
end

#complete_item_by_id(id) ⇒ Object



78
79
80
81
# File 'lib/everdone/todoist.rb', line 78

def complete_item_by_id(id)
    ret = self.call_api('https://todoist.com/API/completeItems', params={'ids'=>"[#{id}]"})
    return ret
end

#delete_item_by_id(id) ⇒ Object



83
84
85
86
# File 'lib/everdone/todoist.rb', line 83

def delete_item_by_id(id)
    ret = self.call_api('https://todoist.com/API/deleteItems', params={'ids'=>"[#{id}]"})
    return ret
end

#get_completed_itemsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/everdone/todoist.rb', line 51

def get_completed_items()
    ret = [] # list of completed items converted into TodoItem objects
    interval = @config.todoist_completed_window
    items = self.call_api('https://todoist.com/API/getAllCompletedItems', params={'interval'=>interval})
    items['items'].each { |item|
        todo_item = TodoItem.new(@config, item, @projects, @labels)
        ret.push(todo_item)
    }
    return ret
end

#get_item_by_id(id) ⇒ Object



62
63
64
65
66
# File 'lib/everdone/todoist.rb', line 62

def get_item_by_id(id)
    items = self.call_api('https://todoist.com/API/getItemsById', params={'ids'=>id})  # get a single item based on id
    todo_item = TodoItem.new(@config, items[0], @projects, @labels)
    return todo_item
end

#get_items_by_project_name(project_name) ⇒ Object



68
69
70
71
# File 'lib/everdone/todoist.rb', line 68

def get_items_by_project_name(project_name)
    ret = self.call_api('https://todoist.com/API/getUncompletedItems', params={'project_id'=>@project_id_by_name[project_name]})
    return ret
end

#get_notes(item_id) ⇒ Object



73
74
75
76
# File 'lib/everdone/todoist.rb', line 73

def get_notes(item_id)
    ret = self.call_api('https://todoist.com/API/getNotes', params={'item_id'=>item_id})
    return ret
end