diff --git a/lib/insightly/base.rb b/lib/insightly/base.rb index a6178b8..e7fc7dc 100644 --- a/lib/insightly/base.rb +++ b/lib/insightly/base.rb @@ -25,11 +25,11 @@ def self.custom_fields(*args) next if method.nil? or method == "" method_name = method.to_s.downcase.to_sym send :define_method, method_name do - @data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] + @data["#{base_insightly_class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] end method_name = "#{method.to_s.downcase}=".to_sym send :define_method, method_name do |value| - @data["#{self.class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] = value + @data["#{base_insightly_class.const_get(:CUSTOM_FIELD_PREFIX)}_#{index+1}"] = value end end end @@ -57,14 +57,20 @@ def initialize(id = nil) load(id) if id end + def base_insightly_class + self.class.ancestors.select do |anc| + anc.name.split("::").first == "Insightly" + end.first + end def url_base - self.class.url_base + base_insightly_class.url_base end def remote_id_field - return self.class.remote_id_field if self.class.remote_id_field - self.class.to_s.downcase.gsub("insightly::", "") + "_id" + return base_insightly_class.remote_id_field if base_insightly_class.remote_id_field + + base_insightly_class.name.demodulize.downcase + "_id" end def remote_id diff --git a/lib/insightly/read_write.rb b/lib/insightly/read_write.rb index 1cc2acb..e0f390d 100644 --- a/lib/insightly/read_write.rb +++ b/lib/insightly/read_write.rb @@ -23,13 +23,22 @@ def post_collection(path, params, content_selector = :json) else content_type = content_selector end - response = RestClient::Request.new(:method => :post, - :url => "#{config.endpoint}/#{path.to_s}", - :user => config.api_key, - :password => "", - :payload => params, - :headers => {:accept => content_type, :content_type => content_type}).execute - process(response, content_selector) + + begin + url = "#{config.endpoint}/#{path.to_s}" + + response = RestClient::Request.new(:method => :post, + :url => url, + :user => config.api_key, + :password => "", + :payload => params, + :headers => {:accept => content_type, :content_type => content_type}).execute + + process(response, content_selector) + rescue => e + puts "failure talking to Insight.ly #{url}" + raise e + end end def put_collection(path, params, content_selector = :json) @@ -47,4 +56,4 @@ def put_collection(path, params, content_selector = :json) process(response, content_selector) end end -end \ No newline at end of file +end diff --git a/spec/unit/base_spec.rb b/spec/unit/base_spec.rb index 835eb47..cb02b4c 100644 --- a/spec/unit/base_spec.rb +++ b/spec/unit/base_spec.rb @@ -3,4 +3,29 @@ describe Insightly::Base do before(:each) do end -end \ No newline at end of file + + context "url_base" do + it "uses the base_insightly_class's" do + base = Insightly::Base.new + base_class = double() + url = "some url" + base_class.should_receive(:url_base).and_return(url) + base.should_receive(:base_insightly_class).and_return(base_class) + base.url_base.should eql(url) + end + end + + context "subclass" do + before(:all) do + class TestTask < Insightly::Task + end + end + it "knows the base class's url" do + TestTask.new.url_base.should eql(Insightly::Task.new.url_base) + end + + it "knows the base class's remote_id_field" do + TestTask.new.remote_id_field.should eql(Insightly::Task.new.remote_id_field) + end + end +end