Class: Rdgem::PeopleFields

Inherits:
Object
  • Object
show all
Defined in:
lib/rdgem/people_fields.rb

Class Method Summary collapse

Class Method Details

.assert_fields(fields, app_key, testing = 0) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rdgem/people_fields.rb', line 41

def self.assert_fields(fields, app_key, testing=0)
		@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
		if testing == "assert"
			return @field_key
		end

		# Integrate: create missing fields
		fields.each_with_index do |create, index|
			if @field_key[index].nil?
				@field_key[index] = create_field(app_key, create, testing)
			end
		end
		if testing == "create"
			return @field_key
		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, testing = 0) ⇒ Object

even if populated the field key will always be replaced



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rdgem/people_fields.rb', line 7

def self.create_field(app_key, field_name, testing=0)
	field_api_key = false
	field_api_id = 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"]


          	#for testing we just need the ID to delete it right away
          	if testing == "create"
          		return field_api_id
          	end

          	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

.delete_field(app_key, field_id) ⇒ Object

removes a field from the owner app_key’s account (used for testing only)



82
83
84
85
86
87
88
# File 'lib/rdgem/people_fields.rb', line 82

def self.delete_field(app_key, field_id)

	query = "#{PIPEDRIVE_API}personFields/#{field_id}?#{TOKEN}#{app_key}"
       response = HTTParty.delete(query)

       return response["success"]
end