From d287eb0e5f0f4e824d9fbb92062991f7119d7ecb Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:43:20 +0200 Subject: [PATCH] Create Pipedrive::API get helpers --- app/lib/pipedrive/api.rb | 30 +++++++++++++++++++++++++++-- app/lib/pipedrive/deal_adapter.rb | 13 +------------ app/lib/pipedrive/person_adapter.rb | 9 +-------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 910e1be46..29c66350e 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -1,6 +1,28 @@ class Pipedrive::API - def self.get(url, params) - RestClient.get(url, params: params) + PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted' + + def self.get_persons_owned_by_user(user_id) + params = { + start: 0, + limit: 500, + user_id: user_id, + api_token: PIPEDRIVE_TOKEN + } + + self.get(PIPEDRIVE_PEOPLE_URL, params) + end + + def self.get_deals_for_person(person_id) + url = [PIPEDRIVE_PEOPLE_URL, person_id, "deals"].join('/') + + params = { + start: 0, + limit: 500, + status: PIPEDRIVE_ALL_NOT_DELETED_DEALS, + api_token: PIPEDRIVE_TOKEN + } + + self.get(url, params) end def self.put_deal(deal_id, params) @@ -17,6 +39,10 @@ class Pipedrive::API private + def self.get(url, params) + RestClient.get(url, params: params) + end + def self.put(url, params) RestClient.put(url, params.to_json, { content_type: :json }) end diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index 27cfc98d2..46379c61b 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -7,8 +7,6 @@ class Pipedrive::DealAdapter PIPEDRIVE_ORGANISMES_STOCK_STAGE_ID = 1 PIPEDRIVE_ORGANISMES_REFUSES_STOCK_STAGE_ID = 45 - PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted' - PIPEDRIVE_LOST_STATUS = "lost" PIPEDRIVE_LOST_REASON = "refusé depuis DS" @@ -26,16 +24,7 @@ class Pipedrive::DealAdapter end def self.fetch_waiting_deal_ids(person_id) - url = [PIPEDRIVE_PEOPLE_URL, person_id, "deals"].join('/') - - params = { - start: 0, - limit: 500, - status: PIPEDRIVE_ALL_NOT_DELETED_DEALS, - api_token: PIPEDRIVE_TOKEN - } - - response = Pipedrive::API.get(url, params) + response = Pipedrive::API.get_deals_for_person(person_id) json_data = JSON.parse(response.body)['data'] json_data.map { |datum| datum['id'] } diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index 6fdf5bb47..87cc74eba 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -3,14 +3,7 @@ class Pipedrive::PersonAdapter PIPEDRIVE_ROBOT_ID = '2748449' def self.get_demandes_from_persons_owned_by_robot - params = { - start: 0, - limit: 500, - user_id: PIPEDRIVE_ROBOT_ID, - api_token: PIPEDRIVE_TOKEN - } - - response = Pipedrive::API.get(PIPEDRIVE_PEOPLE_URL, params) + response = Pipedrive::API.get_persons_owned_by_user(PIPEDRIVE_ROBOT_ID) json_data = JSON.parse(response.body)['data'] json_data.map do |datum|