Module: Rdgem

Defined in:
lib/rdgem.rb,
lib/rdgem/version.rb

Constant Summary collapse

PIPEDRIVE_API =

Your code goes here… global declarations

"https://api.pipedrive.com/v1/"
TOKEN =
"api_token="
HEADERS =
{'Accept'=>'application/json',
'Content-Type'=>'application/x-www-form-urlencoded',
'User-Agent'=>'Ruby.Pipedrive.Api' }
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.add_field_to_user(app_key, field_name) ⇒ Object

adds a new field to the owner app_key’s account



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rdgem.rb', line 63

def self.add_field_to_user(app_key, field_name)
	field_api_key = false

	input_query = PIPEDRIVE_API + 'personFields?' + TOKEN + app_key

       response = HTTParty.post(input_query, 
       			:body => {:name =>"#{field_name}", 
       			:field_type => "varchar"},
       			:headers => HEADERS )

        unless response["data"] == nil
          	field_api_id = response["data"]["id"]

          	key_query = PIPEDRIVE_API + 'personFields/'\
           			+ field_api_id.to_s + "?" + TOKEN + app_key


           field_key_response = HTTParty.get(key_query)

           unless field_key_response["data"] == nil
          		field_api_key = field_key_response["data"]["key"]
 			end
       end
       return field_api_key
end

.assert_fields(fields, app_key) ⇒ Object

checks if the key is valid and if the custom fields are created in the acc if any field is missing, create it



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rdgem.rb', line 91

def self.assert_fields(fields, app_key)
		@field_key = []

    query = PIPEDRIVE_API + 'personFields?' + TOKEN + app_key

  	response = HTTParty.get(query)

  	if response["success"]
  		#Assert: looks for the desired fields
  		fields.each_with_index do |assert, index|
  	response["data"].each do |search|
   			if search['name'] == assert
    			@field_key[index] = search['key']
  				end
  			end
		end

		# Integrate: create missing fields
		fields.each_with_index do |create, index|
			if @field_key[index].blank?
				@field_key[index] = create_field(app_key, create)
			end
		end
		 
  		#Hash the info
		custom_field = Hash[fields.zip(@field_key.map \
  			{|i| i.include?(',') ? (i.split /, /) : i})] #/
	else
		if response["error"] == "You need to be authorized to make this request."
			return false
		end
	end
	#ship it back
	return custom_field
end

.create_field(app_key, field_name) ⇒ Object

even if populated the field key will always be replaced



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rdgem.rb', line 128

def self.create_field(app_key, field_name)
	field_api_key = false

	input_query = PIPEDRIVE_API + 'personFields?' + TOKEN + app_key

       response = HTTParty.post(input_query, 
       				:body => {:name =>"#{field_name}",
       				:field_type => "varchar"},
       				:headers => HEADERS )

        unless response["data"] == nil
          	field_api_id = response["data"]["id"]

          	key_query = PIPEDRIVE_API + 'personFields/'\
           			+ field_api_id.to_s + "?" + TOKEN + app_key

           field_key_response = HTTParty.get(key_query)

           unless field_key_response["data"] == nil
          		field_api_key = field_key_response["data"]["key"]
 			end
       end
       return field_api_key
end

.get_or_create_company(app_key, company) ⇒ Object

queries for a company name. creates when it doesn’t exist.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rdgem.rb', line 25

def self.get_or_create_company(app_key, company)
	org_app_id = ""

	query = PIPEDRIVE_API + 'organizations/find?term=' \
           + company + '&start=0&' + TOKEN + app_key

       response = HTTParty.get(query)
       #if successfull and with content, get the company app_key
       #so as not to create another with the same name
       if response["success"]
         unless response["data"] == nil
           response["data"].each do |search|
             if search['name'] == company
               org_app_id = search['id']
               #not caring about duplicates. 
               #we will ensure we at least won't create any
               break
             end
           end
         else
           #didn't find, create one
		input_query = PIPEDRIVE_API + 'organizations?' + TOKEN + app_key

       	org_response = HTTParty.post(input_query, 
         					:body => {:name =>company},
         					:headers => HEADERS )

           unless org_response["data"] == nil
           	org_app_id = org_response["data"]["id"]
           end
         end
       else
         #error getting company
       end
       return org_app_id
end

.send_lead(app_key, lead_to_person) ⇒ Object

pushes the lead to pipedrive



16
17
18
19
20
21
22
# File 'lib/rdgem.rb', line 16

def self.send_lead(app_key, lead_to_person)
   	query = PIPEDRIVE_API + 'persons?' + TOKEN + app_key

   	response = HTTParty.post(query,
   				:body => lead_to_person,
   				:headers => HEADERS)
end