2018-04-05 15:59:24 +02:00
|
|
|
class Pipedrive::API
|
2018-03-28 14:43:20 +02:00
|
|
|
PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted'
|
2018-03-28 14:42:01 +02:00
|
|
|
PIPEDRIVE_DEALS_URL = [PIPEDRIVE_API_URL, 'deals'].join("/")
|
|
|
|
PIPEDRIVE_PEOPLE_URL = [PIPEDRIVE_API_URL, 'persons'].join("/")
|
2018-05-24 16:40:39 +02:00
|
|
|
PIPEDRIVE_ORGANIZATIONS_URL = [PIPEDRIVE_API_URL, 'organizations'].join("/")
|
2018-03-28 14:43:20 +02:00
|
|
|
|
|
|
|
def self.get_persons_owned_by_user(user_id)
|
2018-03-28 14:53:12 +02:00
|
|
|
url = PIPEDRIVE_PEOPLE_URL
|
2018-03-28 14:35:34 +02:00
|
|
|
params = { user_id: user_id }
|
2018-03-28 14:43:20 +02:00
|
|
|
|
2018-03-28 14:53:12 +02:00
|
|
|
self.get(url, params)
|
2018-03-28 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_deals_for_person(person_id)
|
|
|
|
url = [PIPEDRIVE_PEOPLE_URL, person_id, "deals"].join('/')
|
2018-03-28 14:35:34 +02:00
|
|
|
params = { status: PIPEDRIVE_ALL_NOT_DELETED_DEALS }
|
2018-03-28 14:43:20 +02:00
|
|
|
|
|
|
|
self.get(url, params)
|
2018-04-05 15:59:24 +02:00
|
|
|
end
|
2018-03-28 14:31:07 +02:00
|
|
|
|
|
|
|
def self.put_deal(deal_id, params)
|
2018-03-28 14:42:01 +02:00
|
|
|
url = [PIPEDRIVE_DEALS_URL, deal_id].join("/")
|
2018-03-28 14:31:07 +02:00
|
|
|
|
|
|
|
self.put(url, params)
|
|
|
|
end
|
|
|
|
|
2018-05-24 16:40:39 +02:00
|
|
|
def self.post_deal(params)
|
|
|
|
self.post(PIPEDRIVE_DEALS_URL, params)
|
|
|
|
end
|
|
|
|
|
2018-03-28 14:31:07 +02:00
|
|
|
def self.put_person(person_id, params)
|
2018-03-28 14:42:01 +02:00
|
|
|
url = [PIPEDRIVE_PEOPLE_URL, person_id].join("/")
|
2018-03-28 14:31:07 +02:00
|
|
|
|
|
|
|
self.put(url, params)
|
|
|
|
end
|
|
|
|
|
2018-05-24 16:40:39 +02:00
|
|
|
def self.post_person(params)
|
|
|
|
self.post(PIPEDRIVE_PEOPLE_URL, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.post_organization(params)
|
|
|
|
self.post(PIPEDRIVE_ORGANIZATIONS_URL, params)
|
|
|
|
end
|
|
|
|
|
2018-03-28 14:31:07 +02:00
|
|
|
private
|
|
|
|
|
2018-03-28 14:43:20 +02:00
|
|
|
def self.get(url, params)
|
2018-03-28 14:35:34 +02:00
|
|
|
params.merge!({
|
|
|
|
start: 0,
|
|
|
|
limit: 500,
|
2018-08-22 17:57:23 +02:00
|
|
|
api_token: token
|
2018-03-28 14:35:34 +02:00
|
|
|
})
|
|
|
|
|
2020-03-05 13:50:38 +01:00
|
|
|
response = Typhoeus.get(url, params: params)
|
|
|
|
|
|
|
|
if response.success?
|
|
|
|
JSON.parse(response.body)['data']
|
|
|
|
end
|
2018-03-28 14:43:20 +02:00
|
|
|
end
|
|
|
|
|
2018-03-28 14:31:07 +02:00
|
|
|
def self.put(url, params)
|
2020-03-05 13:50:38 +01:00
|
|
|
Typhoeus.put(
|
|
|
|
url,
|
|
|
|
params: { api_token: token },
|
|
|
|
body: params.to_json,
|
|
|
|
headers: { 'content-type' => 'application/json' }
|
|
|
|
)
|
2018-03-28 14:31:07 +02:00
|
|
|
end
|
2018-05-24 16:40:39 +02:00
|
|
|
|
|
|
|
def self.post(url, params)
|
2020-03-05 13:50:38 +01:00
|
|
|
Typhoeus.post(
|
|
|
|
url,
|
|
|
|
params: { api_token: token },
|
|
|
|
body: params.to_json,
|
|
|
|
headers: { 'content-type' => 'application/json' }
|
|
|
|
)
|
2018-05-24 16:40:39 +02:00
|
|
|
end
|
2018-08-22 17:57:23 +02:00
|
|
|
|
|
|
|
def self.token
|
|
|
|
Rails.application.secrets.pipedrive[:key]
|
|
|
|
end
|
2018-04-05 15:59:24 +02:00
|
|
|
end
|