lib/mite-rb.rb in mite-rb-0.1.0 vs lib/mite-rb.rb in mite-rb-0.2.0
- old
+ new
@@ -1,8 +1,5 @@
-$:.unshift(File.dirname(__FILE__))
-
-require 'rubygems'
require 'activesupport'
require 'activeresource'
# The official ruby library for interacting with the RESTful API of mite,
# a sleek time tracking webapp.
@@ -41,27 +38,57 @@
end
def resources
@resources ||= []
end
+
+ # Validates connection
+ # returns true when valid false when not
+ def validate
+ validate! rescue false
+ end
+
+ # Same as validate_connection
+ # but raises http-error when connection is invalid
+ def validate!
+ !!Mite::Account.find
+ end
end
self.host_format = '%s://%s%s'
self.domain_format = '%s.mite.yo.lk'
self.protocol = 'http'
self.port = ''
+
+ class MethodNotAvaible < StandardError; end
+
+ module NoWriteAccess
+ def save
+ raise MethodNotAvaible, "Cannot save #{self.class.name} over mite.api"
+ end
+ def create
+ raise MethodNotAvaible, "Cannot save #{self.class.name} over mite.api"
+ end
+
+ def destroy
+ raise MethodNotAvaible, "Cannot save #{self.class.name} over mite.api"
+ end
+ end
+
class Base < ActiveResource::Base
class << self
def inherited(base)
- Mite.resources << base
- class << base
- attr_accessor :site_format
+ unless base == Mite::SingletonBase
+ Mite.resources << base
+ class << base
+ attr_accessor :site_format
+ end
+ base.site_format = '%s'
+ base.timeout = 20
end
- base.site_format = '%s'
- base.timeout = 20
super
end
# Common shortcuts known from ActiveRecord
def all(options={})
@@ -73,33 +100,53 @@
end
def last(options={})
find_every(options).last
end
-
- # Undo destroy action on the resource with the ID in the +id+ parameter.
- def undo_destroy(id)
- returning(self.new(:id => id)) { |res| res.undo_destroy }
- end
end
+ end
+
+ class SingletonBase < Base
+ include NoWriteAccess
- # Undo destroy action.
- def undo_destroy
- path = element_path(prefix_options).sub(/\.([\w]+)/, '/undo_delete.\1')
-
- returning connection.post(path, "", self.class.headers) do |response|
- load_attributes_from_response(response)
+ class << self
+ def collection_name
+ element_name
end
+
+ def element_path(id, prefix_options = {}, query_options = nil)
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
+ "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
+ end
+
+ def collection_path(prefix_options = {}, query_options = nil)
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
+ "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
+ end
end
-
+
+ def find
+ super(1)
+ end
+
+ alias_method :first, :find
+ alias_method :last, :find
+
+ # Prevent collection methods
+ def all
+ raise MethodNotAvaible, "Method not supported on #{self.class.name}"
+ end
end
- class Error < StandardError; end
end
+$:.unshift(File.dirname(__FILE__))
+
require 'mite/customer'
require 'mite/project'
require 'mite/service'
require 'mite/time_entry'
require 'mite/time_entry_group'
require 'mite/tracker'
-require 'mite/user'
+require 'mite/user'
+require 'mite/myself'
+require 'mite/account'
\ No newline at end of file