rtodoist 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rtodoist.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+ require 'net/http'
4
+ require 'cgi'
5
+ require 'pp'
6
+
7
+ class Todoist
8
+
9
+ def initialize(token)
10
+ @token = token
11
+
12
+ if (check_connection(2, 'http://todoist.com') == false)
13
+ raise 'http://todoist.com not accessible. Program will now exit.'
14
+ end
15
+ end
16
+
17
+ def pretty_print_json(data)
18
+ if (data.nil? == false)
19
+ data.each { |result|
20
+ result.each { |key, value|
21
+ puts "#{key} = #{value}"
22
+ }
23
+ puts "--------"
24
+ }
25
+ else
26
+ puts "No data returned - cannot print"
27
+ end
28
+ end
29
+
30
+ def get_all_projects()
31
+ base_url = "http://todoist.com/API/getProjects"
32
+ url = %<#{base_url}?token=#{@token}>
33
+ return get_json_data(url)
34
+ end
35
+
36
+ def get_project(project_id)
37
+ base_url = "http://todoist.com/API/getProject"
38
+ url = %<#{base_url}?project_id=#{project_id}&token=#{@token}>
39
+ return get_json_data(url)
40
+ end
41
+
42
+ def get_labels()
43
+ base_url = "http://todoist.com/API/getLabels"
44
+ url = %<#{base_url}?token=#{@token}>
45
+ return get_json_data(url)
46
+ end
47
+
48
+ def get_uncompleted_items(project_id)
49
+ base_url = "http://todoist.com/API/getUncompletedItems"
50
+ url = %<#{base_url}?project_id=#{project_id}&token=#{@token}>
51
+ return get_json_data(url)
52
+ end
53
+
54
+ def get_completed_items(project_id)
55
+ base_url = "http://todoist.com/API/getCompletedItems"
56
+ url = %<#{base_url}?project_id=#{project_id}&token=#{@token}>
57
+ return get_json_data(url)
58
+ end
59
+
60
+ def query(query)
61
+ base_url = "http://todoist.com/API/query"
62
+ url = %<#{base_url}?queries=#{query}&token=#{@token}>
63
+ return get_json_data(url)
64
+ end
65
+
66
+ def add_item(project_id, content, due_date, priority)
67
+ base_url = "http://todoist.com/API/addItem"
68
+ # URL encode the content to deal with spaces and special characters
69
+ content = CGI.escape(content)
70
+ url = %<#{base_url}?content=#{content}&project_id=#{project_id}&priority=#{priority}token=#{@token}>
71
+ return get_json_data(url)
72
+ end
73
+
74
+ def update_item(item_id, content, due_date, priority)
75
+ base_url = "http://todoist.com/API/updateItem"
76
+ # URL encode the content to deal with spaces and special characters
77
+ content = CGI.escape(content)
78
+ url = %<#{base_url}?content=#{content}&id=#{id}&priority=#{priority}token=#{@token}>
79
+ return get_json_data(url)
80
+ end
81
+
82
+
83
+
84
+ ###################################################################################
85
+ # All methods below are private, and therefore accessible only to the class itself
86
+ ###################################################################################
87
+ private
88
+
89
+ def check_connection(max_tries, url)
90
+ tries = 0
91
+ begin
92
+ tries += 1
93
+ open(url) {
94
+ return true
95
+ }
96
+ rescue
97
+ if (tries > max_tries)
98
+ puts "Connection error: #{url} not accessible"
99
+ return false
100
+ else
101
+ retry
102
+ end
103
+ end
104
+ end
105
+
106
+ def get_json_data(url)
107
+ resp = Net::HTTP.get_response(URI.parse(url))
108
+ data = resp.body
109
+
110
+ if (data[0,1] != "[")
111
+ return nil
112
+ end
113
+
114
+ # we convert the returned JSON data to native Ruby
115
+ # data structure - a hash
116
+ result = JSON.parse(data)
117
+
118
+ return result
119
+ end
120
+ end
121
+
122
+
123
+
124
+ #td = Todoist.new('1c932299bec6eda2bfb24a3a031296e9cfe2d310')
125
+
126
+ #result = td.query('[%22viewall%22]')
127
+
128
+ #pp(result[0]['data'][2])
129
+
130
+ #json_result = JSON.parse(result[0])
File without changes
data/rtodoist.gemspec ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'rtodoist'
5
+ s.version = '0.0.1'
6
+ s.summary = "RTodoist allows easy access to the Todoist todo list manager (http://todoist.com"
7
+ s.files = Dir.glob("**/**/**")
8
+ s.test_files = Dir.glob("test/*_test.rb")
9
+ s.author = "Robin Wilson"
10
+ s.email = "r.t.wilson@rmplc.co.uk"
11
+ s.has_rdoc = false
12
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: rtodoist
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2008-06-07 00:00:00 +01:00
8
+ summary: RTodoist allows easy access to the Todoist todo list manager (http://todoist.com
9
+ require_paths:
10
+ - lib
11
+ email: r.t.wilson@rmplc.co.uk
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Robin Wilson
31
+ files:
32
+ - doc
33
+ - lib
34
+ - lib/rtodoist.rb
35
+ - pkg
36
+ - rtodoist-0.0.1.gem
37
+ - rtodoist.gemspec
38
+ - test
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies: []
52
+