Class: Toodledo::Session
- Inherits:
-
Object
- Object
- Toodledo::Session
- Defined in:
- lib/toodledo/session.rb
Overview
The Session. This is responsible for calling to the server and handling most functionality.
Constant Summary collapse
- DEFAULT_API_URL =
'http://www.toodledo.com/api.php'
- USER_AGENT =
"Ruby/#{Toodledo::VERSION} (#{RUBY_PLATFORM})"
- HEADERS =
{ 'User-Agent' => USER_AGENT, 'Connection' => 'keep-alive', 'Keep-Alive' => '300' }
- EXPIRATION_TIME_IN_SECS =
The key should be good for four hours.
60 * 60 * 4
- DATE_FORMAT =
'%Y-%m-%d'
- DATETIME_FORMAT =
'%Y-%m-%d %H:%M:%S'
- TIME_FORMAT =
'%I:%M %p'
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#add_context(title) ⇒ Object
Adds the context to Toodledo, with the title.
-
#add_folder(title, is_private = 1) ⇒ Object
Adds a folder.
-
#add_goal(title, level = 0, contributes = 0) ⇒ Object
Adds a new goal with the given title, the level (short to long term) and the contributing goal id.
-
#add_task(title, params = {}) ⇒ Object
Adds a task to Toodledo.
-
#call(method, params, key = nil) ⇒ Object
Calls Toodledo with the method name, the parameters and the session key.
-
#connect(base_url = DEFAULT_API_URL, proxy = nil) ⇒ Object
Connects to the server, asking for a new key that’s good for an hour.
-
#delete_context(id) ⇒ Object
Deletes the context from Toodledo, using the id.
-
#delete_folder(id) ⇒ Object
Deletes the folder with the id.
-
#delete_goal(id) ⇒ Object
Delete the goal with the given id.
-
#delete_task(id) ⇒ Object
Deletes the task, using the task id.
-
#disconnect ⇒ Object
Disconnects from the server.
-
#edit_folder(id, params = {}) ⇒ Object
Edits a folder.
-
#edit_task(id, params = {}) ⇒ Object
-
id : The id number of the task to edit.
-
-
#escape_text(input) ⇒ Object
escape the & character as %26 and the ; character as %3B.
-
#expired? ⇒ Boolean
Returns true if the session has expired.
-
#flush_contexts ⇒ Object
Deletes the cached contexts.
-
#flush_folders ⇒ Object
Nils out the cached folders.
-
#flush_goals ⇒ Object
Nils the cached goals.
-
#get_account_info ⇒ Object
Returns the information associated with this account.
-
#get_context_by_id(context_id) ⇒ Object
Returns the context with the given id.
-
#get_context_by_name(context_name) ⇒ Object
Returns the context with the given name.
-
#get_contexts(flush = false) ⇒ Object
Gets the array of contexts.
-
#get_deleted(after) ⇒ Object
Returns deleted tasks.
-
#get_folder_by_id(folder_id) ⇒ Object
Gets the folder with the given id.
-
#get_folder_by_name(folder_name) ⇒ Object
Gets the folder by the name.
-
#get_folders(flush = false) ⇒ Object
Gets all the folders.
-
#get_goal_by_id(goal_id) ⇒ Object
Returns the goal with the given id.
-
#get_goal_by_name(goal_name) ⇒ Object
Returns the goal with the given name.
-
#get_goals(flush = false) ⇒ Object
Returns the array of goals.
-
#get_server_info ⇒ Object
The “getServerInfo” API call will return some information about the server and your current API session.
-
#get_task_by_id(id) ⇒ Object
Gets a single task by its id, and returns the task.
-
#get_tasks(params = {}) ⇒ Object
-
title: * folder: * context: * goal: * duedate: * duetime * repeat: * priority: * parent: * shorter: * longer: * before * after * modbefore * modafter * compbefore * compafter * notcomp * star * status * startdate.
-
-
#get_token(user_id, app_id) ⇒ Object
Gets the token method, given the id.
-
#get_token_file(user_id) ⇒ Object
Gets there full path of the token file.
-
#get_tokens_directory ⇒ Object
Make sure that there is a “.toodledo/tokens” directory.
-
#get_uncached_token(user_id, app_id) ⇒ Object
Calls the server to get a token.
-
#get_user_id(email, password) ⇒ Object
Returns the user id.
- #handle_boolean(myhash, params, symbol) ⇒ Object
-
#handle_context(myhash, params) ⇒ Object
Takes in a context (in the form of a string, a Context object or a context id) and adds it to myhash as a context_id.
- #handle_date(myhash, params, symbol) ⇒ Object
-
#handle_datetime(myhash, params, symbol) ⇒ Object
Handles a generic date time value.
-
#handle_folder(myhash, params) ⇒ Object
Handles a folder (in the form of a folder name, object or id) and puts the folder_id in myhash.
-
#handle_goal(myhash, params) ⇒ Object
Takes a goal (as a goal title, a goal object or a goal id) and sets it in myhash.
-
#handle_number(myhash, params, symbol) ⇒ Object
Helper methods follow.
-
#handle_parent(myhash, params) ⇒ Object
Handles the parent task object.
-
#handle_priority(myhash, params) ⇒ Object
Handles the priority.
-
#handle_repeat(myhash, params) ⇒ Object
Handles the repeat parameter.
-
#handle_status(myhash, params) ⇒ Object
Handles the status parameter.
- #handle_string(myhash, params, symbol) ⇒ Object
- #handle_time(myhash, params, symbol) ⇒ Object
-
#initialize(user_id, password, logger = nil, app_id = nil) ⇒ Session
constructor
Creates a new session, using the given user name and password.
-
#is_too_old(token_path) ⇒ Object
Returns true if the file is more than an hour old, false otherwise.
-
#make_uri(method, params) ⇒ Object
Returns a parsable URI object from the base API URL and the parameters.
-
#md5(input_string) ⇒ Object
Hashes the input string and returns a string hex digest.
-
#read_token(user_id) ⇒ Object
Reads a token from the file system, if the given user_id exists and the token is not too old.
-
#write_token(user_id, token) ⇒ Object
Writes the token file to the filesystem.
Constructor Details
#initialize(user_id, password, logger = nil, app_id = nil) ⇒ Session
Creates a new session, using the given user name and password. throws InvalidConfigurationError if user_id or password are nil.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/toodledo/session.rb', line 45 def initialize(user_id, password, logger = nil, app_id = nil) raise InvalidConfigurationError.new("Nil user_id") if (user_id == nil) raise InvalidConfigurationError.new("Nil password") if (password == nil) @user_id = user_id @password = password @app_id = app_id @folders = nil @contexts = nil @goals = nil @key = nil @folders_by_id = nil @goals_by_id = nil @contexts_by_id = nil @logger = logger end |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
41 42 43 |
# File 'lib/toodledo/session.rb', line 41 def app_id @app_id end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
41 42 43 |
# File 'lib/toodledo/session.rb', line 41 def base_url @base_url end |
#logger ⇒ Object
Returns the value of attribute logger.
39 40 41 |
# File 'lib/toodledo/session.rb', line 39 def logger @logger end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
41 42 43 |
# File 'lib/toodledo/session.rb', line 41 def proxy @proxy end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
41 42 43 |
# File 'lib/toodledo/session.rb', line 41 def user_id @user_id end |
Instance Method Details
#add_context(title) ⇒ Object
Adds the context to Toodledo, with the title.
757 758 759 760 761 762 763 764 765 766 |
# File 'lib/toodledo/session.rb', line 757 def add_context(title) logger.debug("add_context(#{title})") if logger raise "Nil title" if (title == nil) result = call('addContext', { :title => title }, @key) flush_contexts() return result.text end |
#add_folder(title, is_private = 1) ⇒ Object
Adds a folder.
-
title : A text string up to 32 characters.
-
private : A boolean value that describes if this folder can be shared.
Returns the id of the newly added folder.
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
# File 'lib/toodledo/session.rb', line 961 def add_folder(title, is_private = 1) logger.debug("add_folder(#{title}, #{is_private})") if logger raise "Nil title" if (title == nil) if (is_private.kind_of? TrueClass) is_private = 1 elsif (is_private.kind_of? FalseClass) is_private = 0 end myhash = { :title => title, :private => is_private} result = call('addFolder', myhash, @key) flush_folders() return result.text end |
#add_goal(title, level = 0, contributes = 0) ⇒ Object
Adds a new goal with the given title, the level (short to long term) and the contributing goal id.
860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/toodledo/session.rb', line 860 def add_goal(title, level = 0, contributes = 0) logger.debug("add_goal(#{title}, #{level}, #{contributes})") if logger raise "Nil title" if (title == nil) params = { :title => title, :level => level, :contributes => contributes } result = call('addGoal', params, @key) flush_goals() return result.text end |
#add_task(title, params = {}) ⇒ Object
Adds a task to Toodledo.
Required Parameters:
title: a String. This is the only required property.
Optional Parameters:
tag: a String
folder: folder id or String matching the folder name
context: context id or String matching the context name
goal: goal id or String matching the Goal Name
duedate: Time or String object "YYYY-MM-DD". If this is a string, it
may take an optional modifier.
duetime: Time or String object "MM:SS p"}
parent: parent id }
repeat: one of { :none, :weekly, :monthly :yearly :daily :biweekly,
:bimonthly, :semiannually, :quarterly }
length: a Number, number of minutes
priority: one of { :negative, :low, :medium, :high, :top }
Returns: the id of the added task as a String.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/toodledo/session.rb', line 558 def add_task(title, params={}) logger.debug("add_task(#{title}, #{params.inspect})") if logger raise "Nil id" if (title == nil) myhash = {:title => title} handle_string(myhash, params, :tag) # If the folder is a string, then we assume we're supposed to find out what # the id is. handle_folder(myhash, params) # Context handling handle_context(myhash, params) # Goal handling handle_goal(myhash, params) # Add the start date if it's been added. handle_date(myhash, params, :startdate) # duedate handling. Take either a string or a Time object.'YYYY-MM-DD' handle_date(myhash, params, :duedate) # duetime handling. Take either a string or a Time object. handle_time(myhash, params, :duetime) # parent handling. handle_parent(myhash, params) # repeat: use the map to change from the symbol to the raw numeric value. handle_repeat(myhash, params) # priority use the map to change from the symbol to the raw numeric value. handle_priority(myhash, params) # Handle the star. handle_boolean(myhash, params, :star) # Handle the status date. handle_status(myhash, params) # Handle the note. handle_string(myhash, params, :note) result = call('addTask', myhash, @key) return result.text end |
#call(method, params, key = nil) ⇒ Object
Calls Toodledo with the method name, the parameters and the session key. Returns the text inside the document root, if any.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/toodledo/session.rb', line 139 def call(method, params, key = nil) raise 'Nil method' if (method == nil) raise 'Nil params' if (params == nil) raise 'Wrong type of params' if (! params.kind_of? Hash) raise 'Wrong method type' if (! method.kind_of? String) if (@base_url == nil) raise 'Must call connect() before this method' end # If it's been more than an hour, then ask for a new key. if (@key != nil && expired?) logger.debug("call(#{method}) connection expired, reconnecting...") if logger # Save the connection information (we'll need it) base_url = @base_url proxy = @proxy disconnect() # ensures that key == nil, which is crucial to avoid an endless loop... connect(base_url, proxy) # swap out the key (if any) before we start assembling the request if (key != nil) key = @key end end # Break all the parameters down into key=value seperated by semi colons stringified_params = (key != nil) ? ';key=' + key : '' params.each { |k, v| stringified_params += ';' + k.to_s + '=' + escape_text(v) } url = make_uri(method, stringified_params) # Establish the proxy if (@proxy != nil) logger.debug("call(#{method}) establishing proxy...") if logger proxy_host = @proxy['host'] proxy_port = @proxy['port'] proxy_user = @proxy['user'] proxy_password = @proxy['password'] if (proxy_user == nil || proxy_password == nil) http = Net::HTTP::Proxy(proxy_host, proxy_port).new(url.host, url.port) else http = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_password).new(url.host, url.port) end else http = Net::HTTP.new(url.host, url.port) end if (url.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = true end if logger logger.debug("call(#{method}) request: #{url.path}?#{url.query}#{url.fragment}") end start_time = Time.now # make the call response = http.request_get(url.request_uri, HEADERS) body = response.body # body = url.read end_time = Time.now doc = REXML::Document.new body if logger logger.debug("call(#{method}) response: " + doc.to_s) logger.debug("call(#{method}) time: " + (end_time - start_time).to_s + ' seconds') end root_node = doc.root if (root_node.name == 'error') error_text = root_node.text if (error_text == 'Invalid ID number') raise Toodledo::ItemNotFoundError.new(error_text) else raise Toodledo::ServerError.new(error_text) end end return root_node end |
#connect(base_url = DEFAULT_API_URL, proxy = nil) ⇒ Object
Connects to the server, asking for a new key that’s good for an hour. Optionally takes a base URL as a parameter. Defaults to DEFAULT_API_URL.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/toodledo/session.rb', line 71 def connect(base_url = DEFAULT_API_URL, proxy = nil) logger.debug("connect(#{base_url}, #{proxy.inspect})") if logger # XXX It looks like get_user_id doesn't work reliably. It always # returns 1 even when we pass in a valid email and password. # @user_id = get_user_id(@email, @password) # logger.debug("user_id = #{@user_id}, #{@email} #{@password}") if (@user_id == '1') raise InvalidConfigurationError.new("Invalid user_id") end if (@user_id == '0') raise InvalidConfigurationError.new("Invalid password") end # Set the base URL. @base_url = base_url # Get the proxy information if it exists. @proxy = proxy session_token = get_token(@user_id, @app_id) key = md5(md5(@password).to_s + session_token + @user_id); @key = key @start_time = Time.now end |
#delete_context(id) ⇒ Object
Deletes the context from Toodledo, using the id.
771 772 773 774 775 776 777 778 779 780 |
# File 'lib/toodledo/session.rb', line 771 def delete_context(id) logger.debug("delete_context(#{id})") if logger raise "Nil id" if (id == nil) result = call('deleteContext', { :id => id }, @key) flush_contexts(); return (result.text == '1') end |
#delete_folder(id) ⇒ Object
Deletes the folder with the id. id : The folder id.
Returns true if the delete was successful.
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 |
# File 'lib/toodledo/session.rb', line 1022 def delete_folder(id) logger.debug("delete_folder(#{id})") if logger raise "Nil id" if (id == nil) result = call('deleteFolder', { :id => id }, @key) flush_folders() return (result.text == '1') end |
#delete_goal(id) ⇒ Object
Delete the goal with the given id.
875 876 877 878 879 880 881 882 883 884 |
# File 'lib/toodledo/session.rb', line 875 def delete_goal(id) logger.debug("delete_goal(#{id})") if logger raise "Nil id" if (id == nil) result = call('deleteGoal', { :id => id }, @key) flush_goals() return (result.text == '1') end |
#delete_task(id) ⇒ Object
Deletes the task, using the task id.
671 672 673 674 675 676 677 678 |
# File 'lib/toodledo/session.rb', line 671 def delete_task(id) logger.debug("delete_task(#{id})") if logger raise "Nil id" if (id == nil) result = call('deleteTask', { :id => id }, @key) return (result.text == '1') end |
#disconnect ⇒ Object
Disconnects from the server.
101 102 103 104 105 106 107 |
# File 'lib/toodledo/session.rb', line 101 def disconnect() logger.debug("disconnect()") if logger @key = nil @start_time = nil @base_url = nil @proxy = nil end |
#edit_folder(id, params = {}) ⇒ Object
Edits a folder.
-
id : The id number of the folder to edit.
-
title : A text string up to 32 characters.
-
private : A boolean value (0 or 1) that describes if this folder can be shared. A value of 1 means that this folder is private.
-
archived : A boolean value (0 or 1) that describes if this folder is archived.
Returns true if the edit was successful.
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
# File 'lib/toodledo/session.rb', line 999 def edit_folder(id, params = {}) logger.debug("edit_folder(#{id}, #{params.inspect})") if logger raise "Nil id" if (id == nil) myhash = { :id => id } handle_string(myhash, params, :title) handle_boolean(myhash, params, :private) handle_boolean(myhash, params, :archived) result = call('editFolder', myhash, @key) flush_folders() return (result.text == '1') end |
#edit_task(id, params = {}) ⇒ Object
-
id : The id number of the task to edit.
-
title : A text string up to 255 characters representing the name of the task.
-
folder : The id number of the folder.
-
context : The id number of the context.
-
goal : The id number of the goal.
-
completed : true or false.
-
duedate : A date formatted as YYYY-MM-DD with an optional modifier attached to the front. Examples: “2007-01-23” , “=2007-01-23” , “>2007-01-23”. To unset the date, set it to ‘0000-00-00’.
-
duetime : A date formated as HH:MM MM.
-
repeat : Use the REPEAT_MAP with the relevant symbol here.
-
length : An integer representing the number of minutes that the task will take to complete.
-
priority : Use the PRIORITY_MAP with the relevant symbol here.
-
note : A text string.
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/toodledo/session.rb', line 622 def edit_task(id, params = {}) logger.debug("edit_task(#{id}, #{params.inspect})") if logger raise "Nil id" if (id == nil) myhash = { :id => id } handle_string(myhash, params, :tag) # If the folder is a string, then we assume we're supposed to find out what # the id is. handle_folder(myhash, params) # Context handling handle_context(myhash, params) # Goal handling handle_goal(myhash, params) # duedate handling. Take either a string or a Time object.'YYYY-MM-DD' handle_date(myhash, params, :duedate) # duetime handling. Take either a string or a Time object. handle_time(myhash, params, :duetime) # parent handling. handle_parent(myhash, params) # Handle completion. handle_boolean(myhash, params, :completed) # Handle star handle_boolean(myhash, params, :star) # repeat: use the map to change from the symbol to the raw numeric value. handle_repeat(myhash, params) # priority use the map to change from the symbol to the raw numeric value. handle_priority(myhash, params) handle_string(myhash, params, :note) result = call('editTask', myhash, @key) return (result.text == '1') end |
#escape_text(input) ⇒ Object
escape the & character as %26 and the ; character as %3B. throws an exception if input is nil.
128 129 130 131 132 133 134 135 |
# File 'lib/toodledo/session.rb', line 128 def escape_text(input) raise "Nil input" if (input == nil) return input.to_s if (! input.kind_of? String) output_string = input.gsub('&', '%26') output_string = output_string.gsub(';', '%3B') return output_string end |
#expired? ⇒ Boolean
Returns true if the session has expired.
110 111 112 113 114 115 116 117 118 |
# File 'lib/toodledo/session.rb', line 110 def expired? return false #logger.debug("expired?") too annoying # The key is only good for an hour. If it's been over an hour, # then we count it as expired. #return true if (@start_time == nil) #return (Time.now - @start_time > EXPIRATION_TIME_IN_SECS) end |
#flush_contexts ⇒ Object
Deletes the cached contexts.
785 786 787 788 789 790 791 |
# File 'lib/toodledo/session.rb', line 785 def flush_contexts() logger.debug('flush_contexts()') if logger @contexts_by_id = nil @contexts_by_name = nil @contexts = nil end |
#flush_folders ⇒ Object
Nils out the cached folders.
983 984 985 986 987 988 989 |
# File 'lib/toodledo/session.rb', line 983 def flush_folders() logger.debug("flush_folders()") if logger @folders = nil @folders_by_name = nil @folders_by_id = nil end |
#flush_goals ⇒ Object
Nils the cached goals.
889 890 891 892 893 894 895 |
# File 'lib/toodledo/session.rb', line 889 def flush_goals() logger.debug('flush_goals()') if logger @goals = nil @goals_by_name = nil @goals_by_id = nil end |
#get_account_info ⇒ Object
Returns the information associated with this account.
pro : Whether or not the user is a Pro member. You need to know this if you want to use subtasks.
dateformat : The user's prefered format for representing dates. (0=M D, Y, 1=M/D/Y, 2=D/M/Y, 3=Y-M-D)
timezone : The number of half hours that the user's timezone is offset from the server's timezone. A value of -4 means that the user's timezone is 2 hours earlier than the server's timezone.
hidemonths : If the task is due this many months into the future, the user wants them to be hidden.
hotlistpriority : The priority value above which tasks should appear on the hotlist.
hotlistduedate : The due date lead-time by which tasks should will appear on the hotlist.
lastaddedit: last time this was edited
lastdelete:
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/toodledo/session.rb', line 332 def get_account_info() result = call('getAccountInfo', {}, @key) pro = (result.elements['pro'].text.to_i == 1) ? true : false #<lastaddedit>2008-01-24 12:26:45</lastaddedit> #<lastdelete>2008-01-23 15:45:55</lastdelete> fmt = DATETIME_FORMAT lastaddedit = result.elements['lastaddedit'].text if (lastaddedit != nil) last_modified_date = DateTime.strptime(lastaddedit, fmt) else last_modified_date = nil end lastdelete = result.elements['lastdelete'].text if (lastdelete != nil) logger.debug("lastdelete = #{lastdelete}") last_deleted_date = DateTime.strptime(lastdelete, fmt) else last_deleted_date = nil end hash = { :userid => result.elements['userid'].text, :alias => result.elements['alias'].text, :pro => pro, :dateformat => result.elements['dateformat'].text.to_i, :timezone => result.elements['timezone'].text.to_i, :hidemonths => result.elements['hidemonths'].text.to_i, :hotlistpriority => result.elements['hotlistpriority'].text.to_i, :hotlistduedate => result.elements['hotlistduedate'].text.to_i, :lastaddedit => last_modified_date, :lastdelete => last_deleted_date } return hash end |
#get_context_by_id(context_id) ⇒ Object
Returns the context with the given id.
719 720 721 722 723 724 725 726 727 728 |
# File 'lib/toodledo/session.rb', line 719 def get_context_by_id(context_id) logger.debug("get_context_by_id(#{context_id})") if logger if (@contexts_by_id == nil) get_contexts(true) end context = @contexts_by_id[context_id] return context end |
#get_context_by_name(context_name) ⇒ Object
Returns the context with the given name.
705 706 707 708 709 710 711 712 713 714 |
# File 'lib/toodledo/session.rb', line 705 def get_context_by_name(context_name) logger.debug("get_context_by_name(#{context_name})") if logger if (@contexts_by_name == nil) get_contexts(true) end context = @contexts_by_name[context_name.downcase] return context end |
#get_contexts(flush = false) ⇒ Object
Gets the array of contexts.
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
# File 'lib/toodledo/session.rb', line 733 def get_contexts(flush = false) logger.debug("get_contexts(#{flush})") if logger return @contexts unless (flush || @contexts == nil) result = call('getContexts', {}, @key) contexts_by_name = {} contexts_by_id = {} contexts = [] result.elements.each { |el| context = Context.parse(self, el) contexts << context contexts_by_id[context.server_id] = context contexts_by_name[context.name.downcase] = context } @contexts_by_id = contexts_by_id @contexts_by_name = contexts_by_name @contexts = contexts return contexts end |
#get_deleted(after) ⇒ Object
Returns deleted tasks.
after: a datetime object that indicates the start date after which deletes should be shown.
685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/toodledo/session.rb', line 685 def get_deleted(after ) logger.debug("get_deleted(#{after})") if logger formatted_after = after.strftime(Session::DATETIME_FORMAT) result = call('getDeleted', { :after => formatted_after }, @key) deleted_tasks = [] result.elements.each do |el| deleted_task = Task.parse_deleted(self, el) deleted_tasks << deleted_task end return deleted_tasks end |
#get_folder_by_id(folder_id) ⇒ Object
Gets the folder with the given id.
917 918 919 920 921 922 923 924 925 926 |
# File 'lib/toodledo/session.rb', line 917 def get_folder_by_id(folder_id) logger.debug("get_folder_by_id(#{folder_id})") if logger raise "Nil folder_id" if (folder_id == nil) if (@folders_by_id == nil) get_folders(true) end return @folders_by_id[folder_id] end |
#get_folder_by_name(folder_name) ⇒ Object
Gets the folder by the name. Case insensitive.
903 904 905 906 907 908 909 910 911 912 |
# File 'lib/toodledo/session.rb', line 903 def get_folder_by_name(folder_name) logger.debug("get_folders_by_name(#{folder_name})") if logger raise "Nil folder name" if (folder_name == nil) if (@folders_by_name == nil) get_folders(true) end return @folders_by_name[folder_name.downcase] end |
#get_folders(flush = false) ⇒ Object
Gets all the folders.
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 |
# File 'lib/toodledo/session.rb', line 929 def get_folders(flush = false) logger.debug("get_folders(#{flush})") if logger return @folders unless (flush || @folders == nil) result = call('getFolders', {}, @key) # <folders> # <folder id="123" private="0" archived="0">Shopping</folder> # <folder id="456" private="0" archived="0">Home Repairs</folder> # <folder id="789" private="0" archived="0">Vacation Planning</folder> # <folder id="234" private="0" archived="0">Chores</folder> # <folder id="567" private="1" archived="0">Work</folder> # </folders> folders = [] folders_by_name = {} folders_by_id = {} result.elements.each { |el| folder = Folder.parse(self, el) folders.push(folder) folders_by_name[folder.name.downcase] = folder # lowercase the key search folders_by_id[folder.server_id] = folder } @folders = folders @folders_by_name = folders_by_name @folders_by_id = folders_by_id return @folders end |
#get_goal_by_id(goal_id) ⇒ Object
Returns the goal with the given id.
813 814 815 816 817 818 819 820 821 |
# File 'lib/toodledo/session.rb', line 813 def get_goal_by_id(goal_id) logger.debug("get_goal_by_id(#{goal_id})") if logger if (@goals_by_id == nil) get_goals(true) end goal = @goals_by_id[goal_id] return goal end |
#get_goal_by_name(goal_name) ⇒ Object
Returns the goal with the given name. Case insensitive.
800 801 802 803 804 805 806 807 808 |
# File 'lib/toodledo/session.rb', line 800 def get_goal_by_name(goal_name) logger.debug("get_goal_by_name(#{goal_name})") if logger if (@goals_by_name == nil) get_goals(true) end goal = @goals_by_name[goal_name.downcase] return goal end |
#get_goals(flush = false) ⇒ Object
Returns the array of goals.
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/toodledo/session.rb', line 826 def get_goals(flush = false) logger.debug("get_goals(#{flush})") if logger return @goals unless (flush || @goals == nil) result = call('getGoals', {}, @key) goals_by_name = {} goals_by_id = {} goals = [] result.elements.each do |el| goal = Goal.parse(self, el) goals << goal goals_by_id[goal.server_id] = goal goals_by_name[goal.name.downcase] = goal end # Loop through and make sure we've got a reference for every contributing # goal. for goal in goals next if (goal.contributes_id == Goal::NO_GOAL.server_id) parent_goal = goals_by_id[goal.contributes_id] goal.contributes = parent_goal end @goals = goals @goals_by_name = goals_by_name @goals_by_id = goals_by_id return goals end |
#get_server_info ⇒ Object
The “getServerInfo” API call will return some information about the server and your current API session.
unixtime: the time since epoch of the server.
date: the date of the server.
tokenexpires: how long in minutes before the current token expires.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/toodledo/session.rb', line 379 def get_server_info() result = call('getServerInfo', {}, @key) # <server> #<unixtime>1204569838</unixtime> #<date>Mon, 3 Mar 2008 12:43:58 -0600</date> #<tokenexpires>45.4</tokenexpires> #</server> unixtime = result.elements['unixtime'].text.to_i server_date = Time.at(unixtime) server_offset = result.elements['serveroffset'].text.to_i token_expires = result.elements['tokenexpires'].text.to_f hash = { :unixtime => unixtime, :date => server_date, :tokenexpires => token_expires, :serveroffset => server_offset, } return hash end |
#get_task_by_id(id) ⇒ Object
Gets a single task by its id, and returns the task.
530 531 532 533 534 535 536 |
# File 'lib/toodledo/session.rb', line 530 def get_task_by_id(id) result = call('getTasks', {:id => id}, @key) result.elements.each do |el| task = Task.parse(self, el) return task end end |
#get_tasks(params = {}) ⇒ Object
-
title:
-
folder:
-
context:
-
goal:
-
duedate:
-
duetime
-
repeat:
-
priority:
-
parent:
-
shorter:
-
longer:
-
before
-
after
-
modbefore
-
modafter
-
compbefore
-
compafter
-
notcomp
-
star
-
status
-
startdate
Returns an array of tasks. This information is never cached.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/toodledo/session.rb', line 433 def get_tasks(params={}) logger.debug("get_tasks(#{params.inspect})") if logger myhash = {} # * title : A text string up to 255 characters. handle_string(myhash, params, :title) # If the folder is a string, then we assume we're supposed to find out what # the id is. handle_folder(myhash, params) # Context handling handle_context(myhash, params) # Goal handling handle_goal(myhash, params) # duedate handling. Take either a string or a Time object.'YYYY-MM-DD' # This does not need special handling, because if it's not a time object # then we don't pass through anything at all. handle_date(myhash, params, :duedate) # duetime handling. Take either a string or a Time object. handle_time(myhash, params, :duetime) # repeat: takes in an integer in the proper range.. handle_repeat(myhash, params) # priority: takes in an integer in the proper range. handle_priority(myhash, params) # * parent : This is used to Pro accounts that have access to subtasks. # Set this to the id number of the parent task and you will get its # subtasks. The default is 0, which is a special number that returns # tasks that do not have a parent. handle_parent(myhash, params) # * shorter : An integer representing minutes. This is used for finding # tasks with a duration that is shorter than the specified number of minutes. handle_number(myhash, params, :shorter) # * longer : An integer representing minutes. This is used for finding # tasks with a duration that is longer than the specified number of minutes. handle_number(myhash, params, :longer) # * before : A date formated as YYYY-MM-DD. Used to find tasks with # due-dates before this date. handle_date(myhash, params, :before) # * after : A date formated as YYYY-MM-DD. Used to find tasks with # due-dates after this date. handle_date(myhash, params, :after) # * modbefore : A date-time formated as YYYY-MM-DD HH:MM:SS. Used to find # tasks with a modified date and time before this dateand time. handle_datetime(myhash, params, :modbefore) # * modafter : A date-time formated as YYYY-MM-DD HH:MM:SS. Used to find # tasks with a modified date and time after this dateand time. handle_datetime(myhash, params, :modafter) # * compbefore : A date formated as YYYY-MM-DD. Used to find tasks with a # completed date before this date. handle_date(myhash, params, :compbefore) # * compafter : A date formated as YYYY-MM-DD. Used to find tasks with a # completed date after this date. handle_date(myhash, params, :compafter) # startbefore: handle_date(myhash, params, :startbefore) # startafter: handle_date(myhash, params, :startafter) # star handle_boolean(myhash, params, :star) # status handle_status(myhash, params) # * notcomp : Set to 1 to omit completed tasks. Omit variable, or set to 0 # to retrieve both completed and uncompleted tasks. handle_boolean(myhash, params, :notcomp) result = call('getTasks', myhash, @key) tasks = [] result.elements.each do |el| task = Task.parse(self, el) tasks << task end return tasks end |
#get_token(user_id, app_id) ⇒ Object
Gets the token method, given the id.
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/toodledo/session.rb', line 228 def get_token(user_id, app_id) raise "Nil user_id" if (user_id == nil || user_id.empty?) # If there is no token file, or the token file is out of date, pull in # a fresh token from the server and write it to the file system. token = read_token(user_id) unless token token = get_uncached_token(user_id, app_id) write_token(user_id, token) end return token end |
#get_token_file(user_id) ⇒ Object
Gets there full path of the token file.
272 273 274 275 276 277 278 279 |
# File 'lib/toodledo/session.rb', line 272 def get_token_file(user_id) tokens_dir = get_tokens_directory() token_path = File.(File.join(tokens_dir, user_id)) unless File.exist?(token_path) return nil end token_path end |
#get_tokens_directory ⇒ Object
Make sure that there is a “.toodledo/tokens” directory.
282 283 284 285 286 287 |
# File 'lib/toodledo/session.rb', line 282 def get_tokens_directory() toodledo_dir = "~/.toodledo" tokens_path = File.(File.join(toodledo_dir, "tokens")) FileUtils.mkdir_p tokens_path tokens_path end |
#get_uncached_token(user_id, app_id) ⇒ Object
Calls the server to get a token.
297 298 299 300 301 302 303 |
# File 'lib/toodledo/session.rb', line 297 def get_uncached_token(user_id, app_id) params = { :userid => user_id } params.merge!({:appid => app_id}) unless app_id.nil? result = call('getToken', params) return result.text end |
#get_user_id(email, password) ⇒ Object
Returns the user id. As far as I can tell, this method is broken and always returns false.
If the userid comes back as 0, it means that either the email or password that you sent was blank. If the userid comes back as 1, it means that the lookup failed. A valid userid will always be a 15 or 16 character hexadecimal string.
312 313 314 315 316 317 318 319 |
# File 'lib/toodledo/session.rb', line 312 def get_user_id(email, password) raise "Nil email" if (email == nil) raise "Nil password" if (password == nil) params = { :email => email, :pass => password } result = call('getUserid', params) return result.text end |
#handle_boolean(myhash, params, symbol) ⇒ Object
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'lib/toodledo/session.rb', line 1050 def handle_boolean(myhash, params, symbol) value = params[symbol] if (value == nil) return end case value when TrueClass, FalseClass bool = (value == true) ? '1' : '0' when String bool = ('true' == value.downcase) ? '1' : '0' when Fixnum bool = (value == 1) ? '1' : '0' else bool = value end myhash.merge!({ symbol => bool }) end |
#handle_context(myhash, params) ⇒ Object
Takes in a context (in the form of a string, a Context object or a context id) and adds it to myhash as a context_id.
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
# File 'lib/toodledo/session.rb', line 1166 def handle_context(myhash, params) context = params[:context] if (context == nil) return end case context when String context_obj = get_context_by_name(context) if (context_obj == nil) raise Toodledo::ItemNotFoundError.new("No context found with name '#{context}'") end context_id = context_obj.server_id when Context context_id = context.server_id else context_id = context end myhash.merge!({ :context => context_id }) end |
#handle_date(myhash, params, symbol) ⇒ Object
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 |
# File 'lib/toodledo/session.rb', line 1077 def handle_date(myhash, params, symbol) value = params[symbol] if (value == nil) return end case value when Time value = value.strftime('%Y-%m-%d') end myhash.merge!({ symbol => value }) end |
#handle_datetime(myhash, params, symbol) ⇒ Object
Handles a generic date time value.
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# File 'lib/toodledo/session.rb', line 1106 def handle_datetime(myhash, params, symbol) # YYYY-MM-DD HH:MM:SS value = params[symbol] if (value == nil) return end case value when Time value = value.strftime(DATETIME_FORMAT) end myhash.merge!({ symbol => value }) end |
#handle_folder(myhash, params) ⇒ Object
Handles a folder (in the form of a folder name, object or id) and puts the folder_id in myhash.
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 |
# File 'lib/toodledo/session.rb', line 1141 def handle_folder(myhash, params) folder = params[:folder] if (folder == nil) return end folder_id = nil case folder when String folder_obj = get_folder_by_name(folder) if (folder_obj == nil) raise Toodledo::ItemNotFoundError.new("No folder found with name #{folder}") end folder_id = folder_obj.server_id when Folder folder_id = folder.server_id else folder_id = folder end myhash.merge!({ :folder => folder_id }) end |
#handle_goal(myhash, params) ⇒ Object
Takes a goal (as a goal title, a goal object or a goal id) and sets it in myhash.
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/toodledo/session.rb', line 1190 def handle_goal(myhash, params) goal = params[:goal] if (goal == nil) return end case goal when String goal_obj = get_goal_by_name(goal) if (goal_obj == nil) raise Toodledo::ItemNotFoundError.new("No goal found with name '#{goal}'") end goal_id = goal_obj.server_id when Goal goal_id = goal.server_id else goal_id = goal end # Otherwise, assume it's a number. myhash.merge!({ :goal => goal_id }) end |
#handle_number(myhash, params, symbol) ⇒ Object
Helper methods follow.
These methods will convert the appropriate format for talking to the Toodledo server. They do not parse the XML that comes back from the server.
1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'lib/toodledo/session.rb', line 1041 def handle_number(myhash, params, symbol) value = params[symbol] if (value != nil) if (value.kind_of? FixNum) myhash.merge!({ symbol => value.to_s}) end end end |
#handle_parent(myhash, params) ⇒ Object
Handles the parent task object. Only takes a task object or id.
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/toodledo/session.rb', line 1122 def handle_parent(myhash, params) parent = params[:parent] if (parent == nil) return end parent_id = nil case parent when Task parent_id = parent.server_id else parent_id = parent end myhash.merge!({ :parent => parent_id }) end |
#handle_priority(myhash, params) ⇒ Object
Handles the priority. This must be one of several values.
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
# File 'lib/toodledo/session.rb', line 1242 def handle_priority(myhash, params) priority = params[:priority] if (priority == nil) return nil end if (! Priority.valid?(priority)) raise "Invalid priority value: #{priority}" end myhash.merge!({ :priority => priority }) end |
#handle_repeat(myhash, params) ⇒ Object
Handles the repeat parameter.
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/toodledo/session.rb', line 1214 def handle_repeat(myhash, params) repeat = params[:repeat] if (repeat == nil) return end if (! Repeat.valid?(repeat)) raise "Invalid repeat value: #{repeat}" end myhash.merge!({ :repeat => repeat }) end |
#handle_status(myhash, params) ⇒ Object
Handles the status parameter
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 |
# File 'lib/toodledo/session.rb', line 1229 def handle_status(myhash, params) status = params[:status] return if (status == nil) if (! Status.valid?(status)) raise "Invalid status value: #{status}" end myhash.merge!({ :status => status }) end |
#handle_string(myhash, params, symbol) ⇒ Object
1070 1071 1072 1073 1074 1075 |
# File 'lib/toodledo/session.rb', line 1070 def handle_string(myhash, params, symbol) value = params[symbol] if (value != nil) myhash.merge!({ symbol => value }) end end |
#handle_time(myhash, params, symbol) ⇒ Object
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/toodledo/session.rb', line 1091 def handle_time(myhash, params, symbol) value = params[symbol] if (value == nil) return end case value when Time value = value.strftime(TIME_FORMAT) end myhash.merge!({ symbol => value }) end |
#is_too_old(token_path) ⇒ Object
Returns true if the file is more than an hour old, false otherwise.
261 262 263 264 265 266 267 268 269 |
# File 'lib/toodledo/session.rb', line 261 def is_too_old(token_path) last_modified_time = File.new(token_path).mtime expiration_time = Time.now - EXPIRATION_TIME_IN_SECS too_old = expiration_time - last_modified_time > 0 logger.debug "is_too_old: time = #{last_modified_time}, expires = #{expiration_time}, too_old = #{too_old}" return too_old end |
#make_uri(method, params) ⇒ Object
Returns a parsable URI object from the base API URL and the parameters.
121 122 123 124 |
# File 'lib/toodledo/session.rb', line 121 def make_uri(method, params) url_string = URI.escape(@base_url + '?method=' + method + params) return URI.parse(url_string) end |
#md5(input_string) ⇒ Object
Hashes the input string and returns a string hex digest.
65 66 67 |
# File 'lib/toodledo/session.rb', line 65 def md5(input_string) return Digest::MD5.hexdigest(input_string) end |
#read_token(user_id) ⇒ Object
Reads a token from the file system, if the given user_id exists and the token is not too old.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/toodledo/session.rb', line 244 def read_token(user_id) token_path = get_token_file(user_id) unless token_path logger.debug("read_token: no token found for #{user_id.inspect}, returning nil") return nil end if is_too_old(token_path) File.delete(token_path) return nil end token = File.read(token_path) token end |
#write_token(user_id, token) ⇒ Object
Writes the token file to the filesystem.
290 291 292 293 294 |
# File 'lib/toodledo/session.rb', line 290 def write_token(user_id, token) logger.debug("write_token: user_id = #{user_id.inspect}, token = #{token.inspect}") token_path = File.(File.join(get_tokens_directory(), user_id)) File.open(token_path, 'w') {|f| f.write(token) } end |