Skip to content

Commit

Permalink
Added Taskrabbit::Client class and refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrichardlai committed Nov 4, 2011
1 parent 1b5ddd0 commit f89a5d6
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/taskrabbit.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Taskrabbit
autoload :Client, "taskrabbit/client"
autoload :Association, "taskrabbit/association"
autoload :Config, "taskrabbit/config"
autoload :Version, "taskrabbit/version"
Expand Down
2 changes: 1 addition & 1 deletion lib/taskrabbit/account.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Taskrabbit
class Account < User
def fetch
api.request('get', "account", self.class)
reload('get', "account")
end
end
end
23 changes: 5 additions & 18 deletions lib/taskrabbit/api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Taskrabbit
class Api
include APISmith::Client
include Client
include Association

attr_accessor :user_token
Expand All @@ -18,6 +18,10 @@ def self.collection_transformers
has_many :tasks, Task
has_many :cities, City

def account
@account ||= Account.new({}, self)
end

def initialize(user_token = nil, attrs = {})
attrs = Taskrabbit.options.merge(attrs)
# set the configuration for the api
Expand All @@ -42,26 +46,9 @@ def request_params(transformer, options = {})
}
end

def check_response_errors(response)
if response and response.respond_to?(:response)
case response.response
when Net::HTTPClientError, Net::HTTPServerError
error = "#{response.response.code} #{response.response.message}"
if response.is_a?(Hash)
error = response['error']
end
raise Taskrabbit::Error.new(error)
end
end
end

def request(method, path, transformer, options = {})
send(method, path, request_params(transformer, options))
end

def account
@account ||= Account.new({}, self)
end

end
end
2 changes: 1 addition & 1 deletion lib/taskrabbit/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class City < Smash
property :links

def fetch
api.request('get', "cities/#{id.to_s}", self.class)
reload('get', "cities/#{id.to_s}")
end

class << self
Expand Down
38 changes: 38 additions & 0 deletions lib/taskrabbit/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Taskrabbit
module Client
def self.included(base)
base.class_eval do
include APISmith::Client
include InstanceMethods
end
end

module InstanceMethods
def transform_response(response, options)
transformer = options[:transform] || options[:transformer]
if transformer
obj = transformer.call response
obj.api = self if obj.respond_to?(:api=)
obj
else
response
end
end

def check_response_errors(response)
if response and response.respond_to?(:response)
case response.response
when Net::HTTPClientError, Net::HTTPServerError
error = "#{response.response.code} #{response.response.message}"
if response.is_a?(Hash)
error = response['error']
# if errors key is present then it's a validation error
raise Smash::Error.new(error, response) if response['errors']
end
raise Taskrabbit::Error.new(error, response)
end
end
end
end
end
end
25 changes: 21 additions & 4 deletions lib/taskrabbit/smash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,35 @@ def initialize(options = {}, api = nil)
def request(*args)
api.request *args
end

def load

def valid?
errors.nil? and error.nil?
end

def clear_errors
%w{error errors}.map { |k| self.delete(k) }
end

def reload(method, path, options = {})
self.loaded = true
self.merge!(fetch)
response = api.request(method, path, self.class, options)
self.merge!(response)
clear_errors
true
rescue Smash::Error => e
self.merge!(e.response) if e.response.is_a?(Hash)
false
end

def fetch; end

def [](property)
value = nil
return value unless (value = super(property)).nil?
if api and !loaded
# load the object if trying to access a property
load
self.loaded = true
fetch
end
super(property)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/taskrabbit/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class User < Smash
property :links

def fetch
api.request('get', "users/#{id.to_s}", self.class)
reload('get', "users/#{id.to_s}")
end

has_many :tasks, Task
Expand Down
2 changes: 1 addition & 1 deletion spec/taskrabbit/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
tr = Taskrabbit::Api.new(TR_USERS[:with_card][:secret])
VCR.use_cassette('account/properties', :record => :new_episodes) do
@user = tr.account
@user.load
@user.fetch
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/taskrabbit/city_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
tr = Taskrabbit::Api.new
VCR.use_cassette('cities/properties', :record => :new_episodes) do
@city = tr.cities.find(3)
@city.load
@city.fetch
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/taskrabbit/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
tr = Taskrabbit::Api.new
VCR.use_cassette('tasks/properties', :record => :new_episodes) do
@tr_task = tr.tasks.find(22545)
@tr_task.load
@tr_task.fetch
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/taskrabbit/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
tr = Taskrabbit::Api.new(TR_USERS[:with_card][:secret])
VCR.use_cassette('users/properties', :record => :new_episodes) do
@user = tr.users.find(TR_USERS[:with_card][:id])
@user.load
@user.fetch
end
end

Expand Down

0 comments on commit f89a5d6

Please sign in to comment.