From 7cf8155e99c409e4a5bf7901f6b96c4c5fe5d5d8 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Fri, 21 Dec 2012 10:42:55 -0600 Subject: [PATCH 1/2] Make it possible to subclass the Insightly models --- lib/insightly/base.rb | 16 +++++++++++----- lib/insightly/read_write.rb | 25 +++++++++++++++++-------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/insightly/base.rb b/lib/insightly/base.rb index a6178b8..57af9a7 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.deconstantize == "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 From 515885e5b1f59ac14fe840aba431fd2e0c1cade1 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Fri, 21 Dec 2012 11:30:47 -0600 Subject: [PATCH 2/2] Adding some specs and breaking a dependancy on Rails. --- lib/insightly/base.rb | 2 +- spec/unit/base_spec.rb | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/insightly/base.rb b/lib/insightly/base.rb index 57af9a7..e7fc7dc 100644 --- a/lib/insightly/base.rb +++ b/lib/insightly/base.rb @@ -59,7 +59,7 @@ def initialize(id = nil) def base_insightly_class self.class.ancestors.select do |anc| - anc.name.deconstantize == "Insightly" + anc.name.split("::").first == "Insightly" end.first 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