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.



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

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



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

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



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

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



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

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



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

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



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

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

#get_completed_itemsObject



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

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



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

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



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

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



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

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