Created
June 22, 2016 04:14
-
-
Save mclosson/b1af9b0e4949f585df6a4778c9fe05ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'json' | |
module Pipedrive | |
class Client | |
def initialize(api_token) | |
self.api_token = api_token | |
end | |
def request(method, resource, options = {}) | |
body = options.to_json | |
headers = { 'Content-Type' => 'application/json' } | |
url = "#{baseurl}/#{resource}?api_token=#{api_token}" | |
HTTParty.send(method, url, headers: headers, body: body) | |
end | |
private | |
attr_accessor :api_token | |
def baseurl | |
"https://api.pipedrive.com/v1" | |
end | |
end | |
end | |
token = 'YOUR_API_KEY' | |
client = Pipedrive::Client.new(token) | |
org_id = nil | |
person_id = nil | |
# Create an organization | |
response = client.request(:post, 'organizations', name: 'The Business Company') | |
if response.created? | |
org_id = JSON.parse(response.body)['data']['id'] | |
else | |
puts response.code | |
puts response.body | |
puts "Failed to create organization!" | |
exit | |
end | |
# Create person in the organization | |
options = { | |
name: 'John Smith', | |
email: 'jsmith@example.org', | |
phone: ['321-555-1212', '8006671337x777'], | |
org_id: org_id | |
} | |
response = client.request(:post, 'persons', options) | |
if response.created? | |
person_id = JSON.parse(response.body)['data']['id'] | |
else | |
puts response.code | |
puts response.body | |
puts "Failed to create person!" | |
exit | |
end | |
# Create deal for person in organization | |
options = { | |
title: 'Big Deal', | |
person_id: person_id, | |
org_id: org_id | |
} | |
response = client.request(:post, 'deals', options) | |
if response.created? | |
deal_id = JSON.parse(response.body)['data']['id'] | |
else | |
puts response.code | |
puts response.body | |
puts "Failed to create deal!" | |
exit | |
end | |
puts "Deal #{deal_id} created for person #{person_id} in organization #{org_id}!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment