Created
July 28, 2016 04:57
-
-
Save graut/36922886828a91533687da120a3c2a78 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
#Here is the first piece of relavant code. The member object is passed into this | |
#function and then the contact data hash is built before posting to Agile. | |
#AGILE_KEY, AGILE_EMAIL, AGILE_DOMAIN are enviornment variables. | |
require "net/http" | |
require "uri" | |
require 'json' | |
def self.transfer_member(member) | |
contact_data = {} | |
contact_data["star_value"] = "0" | |
contact_data["lead_score"] = "0" | |
contact_data["tags"] = [ | |
"State List", | |
"Radar" | |
] | |
properties = [ | |
{ | |
"type" => "SYSTEM", | |
"name" => "first_name", | |
"value" => "teko" | |
}, | |
{ | |
"type" => "SYSTEM", | |
"name" => "last_name", | |
"value" => "member.last_name" | |
}, | |
{ | |
"type"=> "SYSTEM", | |
"name"=> "email", | |
"value"=> "zed002@yahoo.com" | |
}, | |
{ | |
"type" => "SYSTEM", | |
"name" => "email", | |
"subtype" => "work", | |
"value" => "zed001@gmail.com" | |
}, | |
{ | |
"type" => "SYSTEM", | |
"name" => "phone", | |
"subtype" => "work", | |
"value" => "+1-541-754-3030" | |
}, | |
{ | |
"type"=> "SYSTEM", | |
"name"=> "website", | |
"subtype" => "YOUTUBE", | |
"value"=> "https://www.youtube.com/watch?v=8-zQMprfDgE" | |
}, | |
{ | |
"type" => "SYSTEM", | |
"name" => "address", | |
"value" => "{\"address\":\"MIG 106\",\"city\":\"Stockholm\",\"state\":\"Stockholm\",\"zip\":\"07460\",\"country\":\"SE\",\"countryname\":\"Sweden\"}" | |
}, | |
{ | |
"type" => "CUSTOM", | |
"name" => "Lead Source", | |
"value" => "State List" | |
}, | |
{ | |
"type" => "CUSTOM", | |
"name" => "Contact Status", | |
"value" => "New" | |
}, | |
{ | |
"type" => "CUSTOM", | |
"name" => "Type", | |
"value" => "Outbound" | |
}, | |
] | |
contact_data["properties"] = properties | |
begin | |
#puts "Sending Member: #{member.id} to AgileCRM" | |
@agile_contact = agile_api_request(:post, "contacts", contact_data) | |
puts @agile_contact.inspect | |
@agile_contact = ::JSON.parse(@agile_contact) | |
rescue | |
end | |
if @agile_contact | |
return @agile_contact | |
end | |
end | |
def self.agile_api_request(method, subject, data = {}) | |
path = "/dev/api/#{subject}" | |
puts "hello" | |
case method | |
when :get | |
request = Net::HTTP::Get.new(path) | |
when :post | |
request = Net::HTTP::Post.new(path) | |
request.body = data.to_json | |
when :put | |
request = Net::HTTP::Put.new(path) | |
request.body = data.to_json | |
when :delete | |
request = Net::HTTP::Delete.new(path) | |
else | |
raise "Unknown method: #{method}" | |
end | |
uri = URI.parse("https://"{your_domain}".agilecrm.com") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request["Content-Type"] = 'application/json' | |
request["Accept"] = 'application/json' | |
request.basic_auth "your_user_email", "your_rest_key" | |
response = http.request(request) | |
response.body | |
end | |
print(transfer_member("test")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment