lib/todoly.rb in todoly-0.0.0 vs lib/todoly.rb in todoly-0.0.1
- old
+ new
@@ -1,121 +1,36 @@
-require "json"
-require "rest_client"
+require "todoly/rest_interface"
+require "todoly/project"
+require "todoly/filter"
+require "todoly/task"
-module Todoly
+class Todoly
+ def initialize(opt={})
+ @rest_if = RestInterface.new(opt)
+ @projects = nil
+ @filters = nil
+ @tasks = nil
+ end
- class TodolyError < StandardError; end
+ def projects
+ @projects ||= Project.list(@rest_if)
+ end
- TODOLY_API_URL = "https://todo.ly/api/"
+ def filters
+ @filters ||= Filter.list(@rest_if)
+ end
- class Client
- def rest_client_with_token(token)
- RestClient::Resource.new(@api_url,
- :headers => { :Token => token })
- end
+ def tasks
+ @tasks ||= Task.list(@rest_if)
+ end
- def rest_client_with_password(user, password)
- RestClient::Resource.new(@api_url,
- :user => user,
- :password => password)
+ def new_task(str, project = nil)
+ obj = {}
+ if project
+ obj["ProjectId"] = project.id
end
-
- {
- :get_token => {
- :path => "authentication/token",
- :method => :get,
- :member => "TokenString",
- },
- :delete_token => {
- :path => "authentication/token",
- :method => :delete,
- :member => "TokenString",
- },
- :projects => {
- :path => "projects",
- :method => :get,
- },
- :items_of_project => {
- :path => "projects/%d/items",
- :method => :get,
- },
- :done_items_of_project => {
- :path => "projects/%d/doneitems",
- :method => :get,
- },
- :filters => {
- :path => "filters",
- :method => :get,
- },
- :items_of_filter => {
- :path => "filters/%d/items",
- :method => :get,
- },
- :items => {
- :path => "items",
- :method => :get,
- },
- :item_by_id => {
- :path => "items/%d",
- :method => :get,
- },
- :create_item => {
- :path => "items",
- :method => :post,
- },
- :delete_item_by_id => {
- :path => "items/%d",
- :method => :delete,
- },
- :update_item_by_id => {
- :path => "items/%d",
- :method => :post,
- },
- }.each do |meth, data|
- class_eval do
- define_method(meth) do |*params|
- if /_(?:by|of)_/ =~ meth
- id, param = params
- path = sprintf(data[:path]+".json", id)
- else
- path = data[:path] + ".json"
- param, = params
- end
- if data[:method] == :post
- json = @api[path].send(data[:method], param.to_json)
- else
- json = @api[path].send(data[:method])
- end
- obj = JSON.parse(json)
- if obj.is_a?(Hash) and obj["ErrorMessage"]
- raise TodolyError, obj["ErrorMessage"]
- end
- if data[:member]
- unless obj.include?(data[:member])
- raise TodolyError, "expected member #{data[:member]} is not contained"
- end
- obj = obj[data[:member]]
- end
- obj
- end
- end
- end
-
- def initialize(opt={})
- if ENV["https_proxy"]
- RestClient.proxy = ENV["https_proxy"]
- elsif ENV["http_proxy"]
- RestClient.proxy = ENV["http_proxy"]
- end
- @api_url = opt[:api_url] || TODOLY_API_URL
- if opt[:token]
- @api = rest_client_with_token(token)
- elsif opt[:email] and opt[:password]
- @api = rest_client_with_password(opt[:email], opt[:password])
- @api = rest_client_with_token(get_token)
- else
- raise "Please specify :email and :password or :token"
- end
- end
+ t = Task.create(str, obj)
+ @tasks << t if @tasks
+ t
end
end