diff --git a/lib/pipedrive/base.rb b/lib/pipedrive/base.rb index 9a0adb0..bebadcc 100644 --- a/lib/pipedrive/base.rb +++ b/lib/pipedrive/base.rb @@ -88,7 +88,7 @@ def all(response = nil, options={},get_absolutely_all=false) res = response || get(resource_path, options) if res.ok? data = res['data'].nil? ? [] : res['data'].map{|obj| new(obj)} - if get_absolutely_all && res['additional_data']['pagination'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection'] + if get_absolutely_all && has_pagination?(res) options[:query] = options[:query].merge({:start => res['additional_data']['pagination']['next_start']}) data += self.all(nil,options,true) end @@ -98,6 +98,10 @@ def all(response = nil, options={},get_absolutely_all=false) end end + def has_pagination?(res) + res['additional_data'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection'] + end + def create( opts = {} ) res = post resource_path, :body => opts if res.success? diff --git a/test/data/create_stages_body.json b/test/data/create_stages_body.json new file mode 100644 index 0000000..483b279 --- /dev/null +++ b/test/data/create_stages_body.json @@ -0,0 +1,75 @@ +{ + "success": true, + "data": + [ + { + "id": 1, + "order_nr": 1, + "name": "Idea", + "active_flag": true, + "deal_probability": 100, + "pipeline_id": 1, + "rotten_flag": false, + "rotten_days": null, + "add_time": "2015-01-06 12:08:01", + "update_time": null, + "pipeline_name": "Pipeline" + }, + + { + "id": 2, + "order_nr": 2, + "name": "Contact Made", + "active_flag": true, + "deal_probability": 100, + "pipeline_id": 1, + "rotten_flag": false, + "rotten_days": null, + "add_time": "2015-01-06 12:08:01", + "update_time": null, + "pipeline_name": "Pipeline" + }, + + { + "id": 3, + "order_nr": 3, + "name": "Needs Discovered", + "active_flag": true, + "deal_probability": 100, + "pipeline_id": 1, + "rotten_flag": false, + "rotten_days": null, + "add_time": "2015-01-06 12:08:01", + "update_time": null, + "pipeline_name": "Pipeline" + }, + + { + "id": 4, + "order_nr": 4, + "name": "Proposal Presented", + "active_flag": true, + "deal_probability": 100, + "pipeline_id": 1, + "rotten_flag": false, + "rotten_days": null, + "add_time": "2015-01-06 12:08:01", + "update_time": null, + "pipeline_name": "Pipeline" + }, + + { + "id": 5, + "order_nr": 5, + "name": "In Negotiation", + "active_flag": true, + "deal_probability": 100, + "pipeline_id": 1, + "rotten_flag": false, + "rotten_days": null, + "add_time": "2015-01-06 12:08:01", + "update_time": null, + "pipeline_name": "Pipeline" + } + ] +} diff --git a/test/test_pipedrive_stage.rb b/test/test_pipedrive_stage.rb new file mode 100644 index 0000000..0c4bd91 --- /dev/null +++ b/test/test_pipedrive_stage.rb @@ -0,0 +1,40 @@ +require 'helper' + +class TestPipedriveStage < Test::Unit::TestCase + def setup + Pipedrive.authenticate("some-token") + end + + should "execute a valid stage request" do + body = {} + stub_request(:get, "http://api.pipedrive.com/v1/stages?api_token=some-token"). + with(:body => body, + :headers => { + 'Accept'=>'application/json', + 'Content-Type'=>'application/x-www-form-urlencoded', + 'User-Agent'=>'Ruby.Pipedrive.Api' + }). + to_return( + :status => 200, + :body => File.read(File.join(File.dirname(__FILE__), "data", "create_stages_body.json")), + :headers => { + "server" => "nginx/1.2.4", + "date" => "Fri, 01 Mar 2013 14:01:03 GMT", + "content-type" => "application/json", + "content-length" => "1260", + "connection" => "keep-alive", + "access-control-allow-origin" => "*" + } + ) + + stages = ::Pipedrive::Stage.all + first_stage = stages.first; + + assert_equal 5, stages.count + + assert_equal "Idea", first_stage.name + assert_equal 1, first_stage.order_nr + assert_equal 1, first_stage.pipeline_id + assert_equal "Pipeline", first_stage.pipeline_name + end +end