toodledo 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 1.3.4 / 2010-10-08
2
+
3
+ * More patches for reestablishing connection, based off file modification time as well as :tokenexpires
4
+
5
+ == 1.3.3 / 2010-10-05
6
+
7
+ * Patches for reestablishing connection when the token is too old.
8
+
1
9
  == 1.3.2 / 2010-02-16
2
10
 
3
11
  * Added file caching for token (will write to $HOME/.toodledo/tokens/<user_id> file)
@@ -7,4 +7,11 @@ module Toodledo
7
7
  class ServerError < StandardError
8
8
 
9
9
  end
10
+
11
+ # Thrown when the key is invalid (usually because we're using an old key or
12
+ # the expiration timed out for some reason)
13
+ class InvalidKeyError < ServerError
14
+
15
+ end
16
+
10
17
  end
@@ -26,10 +26,10 @@ module Toodledo
26
26
  'Connection' => 'keep-alive',
27
27
  'Keep-Alive' => '300'
28
28
  }
29
-
30
- # The key should be good for four hours.
31
- EXPIRATION_TIME_IN_SECS = 60 * 60 * 4
32
29
 
30
+ # Make it a little less than 4 hours, so the file should always be fresher.
31
+ FILE_EXPIRATION_TIME_IN_SECS = (60 * 60 * 4) - 300
32
+
33
33
  DATE_FORMAT = '%Y-%m-%d'
34
34
 
35
35
  DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
@@ -57,6 +57,7 @@ module Toodledo
57
57
  @folders_by_id = nil
58
58
  @goals_by_id = nil
59
59
  @contexts_by_id = nil
60
+ @expiration_time = nil
60
61
 
61
62
  @logger = logger
62
63
  end
@@ -65,7 +66,7 @@ module Toodledo
65
66
  def md5(input_string)
66
67
  return Digest::MD5.hexdigest(input_string)
67
68
  end
68
-
69
+
69
70
  # Connects to the server, asking for a new key that's good for an hour.
70
71
  # Optionally takes a base URL as a parameter. Defaults to DEFAULT_API_URL.
71
72
  def connect(base_url = DEFAULT_API_URL, proxy = nil)
@@ -94,27 +95,43 @@ module Toodledo
94
95
  key = md5(md5(@password).to_s + session_token + @user_id);
95
96
 
96
97
  @key = key
97
- @start_time = Time.now
98
+ @start_time = Time.now
99
+
100
+ server_info = get_server_info()
101
+ @expiration_time = find_expiration_time(server_info[:tokenexpires])
98
102
  end
99
103
 
100
104
  # Disconnects from the server.
101
105
  def disconnect()
102
106
  logger.debug("disconnect()") if logger
107
+
108
+ token_path = get_token_file(user_id)
109
+ if token_path
110
+ logger.debug("deleting token path()") if logger
111
+ File.delete(token_path)
112
+ end
113
+
103
114
  @key = nil
104
115
  @start_time = nil
116
+ @expiration_time = nil
105
117
  @base_url = nil
106
118
  @proxy = nil
107
119
  end
108
120
 
121
+ def find_expiration_time(expiration_in_mins)
122
+ # Expiration time is measured in minutes, i.e. 49.4
123
+ exp_mins = expiration_in_mins.to_i # drop the fractional bit
124
+ @start_time + (exp_mins * 60)
125
+ end
126
+
109
127
  # Returns true if the session has expired.
110
128
  def expired?
111
- return false
112
- #logger.debug("expired?") too annoying
113
-
114
- # The key is only good for an hour. If it's been over an hour,
115
- # then we count it as expired.
116
- #return true if (@start_time == nil)
117
- #return (Time.now - @start_time > EXPIRATION_TIME_IN_SECS)
129
+ current_time = Time.now
130
+ has_expired = (@expiration_time != nil) && (current_time > @expiration_time)
131
+ if (has_expired)
132
+ logger.debug("#{@expiration_time} > #{current_time}, expired == true") if logger
133
+ end
134
+ has_expired
118
135
  end
119
136
 
120
137
  # Returns a parsable URI object from the base API URL and the parameters.
@@ -134,6 +151,11 @@ module Toodledo
134
151
  return output_string
135
152
  end
136
153
 
154
+ def reconnect(base_url, proxy)
155
+ disconnect()
156
+ connect(base_url, proxy)
157
+ end
158
+
137
159
  # Calls Toodledo with the method name, the parameters and the session key.
138
160
  # Returns the text inside the document root, if any.
139
161
  def call(method, params, key = nil)
@@ -147,17 +169,16 @@ module Toodledo
147
169
  end
148
170
 
149
171
  # If it's been more than an hour, then ask for a new key.
150
- if (@key != nil && expired?)
151
- logger.debug("call(#{method}) connection expired, reconnecting...") if logger
172
+ if (@key && expired? && method != "getServerInfo")
173
+ logger.info("call(#{method}) connection expired, reconnecting...") if logger
152
174
 
153
175
  # Save the connection information (we'll need it)
154
176
  base_url = @base_url
155
177
  proxy = @proxy
156
- disconnect() # ensures that key == nil, which is crucial to avoid an endless loop...
157
- connect(base_url, proxy)
158
-
178
+ reconnect(base_url, proxy)
179
+
159
180
  # swap out the key (if any) before we start assembling the request
160
- if (key != nil)
181
+ if (key)
161
182
  key = @key
162
183
  end
163
184
  end
@@ -213,9 +234,12 @@ module Toodledo
213
234
 
214
235
  root_node = doc.root
215
236
  if (root_node.name == 'error')
216
- error_text = root_node.text
237
+ error_text = root_node.text
217
238
  if (error_text == 'Invalid ID number')
218
239
  raise Toodledo::ItemNotFoundError.new(error_text)
240
+ elsif (error_text == 'key did not validate')
241
+ raise Toodledo::InvalidKeyError.new(error_text)
242
+ # May want to get uncached token here as a recovery mechanism?
219
243
  else
220
244
  raise Toodledo::ServerError.new(error_text)
221
245
  end
@@ -259,13 +283,13 @@ module Toodledo
259
283
 
260
284
  # Returns true if the file is more than an hour old, false otherwise.
261
285
  def is_too_old(token_path)
262
- last_modified_time = File.new(token_path).mtime
263
- expiration_time = Time.now - EXPIRATION_TIME_IN_SECS
264
- too_old = expiration_time - last_modified_time > 0
265
-
266
- logger.debug "is_too_old: time = #{last_modified_time}, expires = #{expiration_time}, too_old = #{too_old}" if logger
286
+ last_modified_time = File.new(token_path).mtime.to_i
287
+ expiration_time = Time.now.to_i - FILE_EXPIRATION_TIME_IN_SECS
288
+ too_old_by = last_modified_time - expiration_time
289
+
290
+ logger.debug "is_too_old: expires in #{too_old_by} seconds" if logger
267
291
 
268
- return too_old
292
+ return too_old_by < 0
269
293
  end
270
294
 
271
295
  # Gets there full path of the token file.
data/lib/toodledo.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  module Toodledo
6
6
 
7
7
  # Required for gem
8
- VERSION = '1.3.3'
8
+ VERSION = '1.3.4'
9
9
 
10
10
  # Returns the configuration object.
11
11
  def self.get_config()
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 3
9
- version: 1.3.3
8
+ - 4
9
+ version: 1.3.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Will Sargent
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-04 00:00:00 -07:00
17
+ date: 2010-10-08 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency