Skip to content

Instantly share code, notes, and snippets.

@graut
Created July 27, 2016 08:57
Show Gist options
  • Save graut/e230d8c74f583285d042200c3fc97e56 to your computer and use it in GitHub Desktop.
Save graut/e230d8c74f583285d042200c3fc97e56 to your computer and use it in GitHub Desktop.
#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"] = "4"
contact_data["lead_score"] = "10"
contact_data["tags"] = [
"State List",
"Radar"
]
properties = [
{
"type" => "SYSTEM",
"name" => "owner_id",
"value" => "6401217618247680"
},
{
"type" => "SYSTEM",
"name" => "first_name",
"value" => "gamion"
},
{
"type" => "SYSTEM",
"name" => "last_name",
"value" => "teller"
},
{
"type" => "SYSTEM",
"name" => "email",
"subtype" => "work",
"value" => "gha22@yopmail.com"
},
{
"type" => "SYSTEM",
"name" => "phone",
"subtype" => "work",
"value" => "8697802159"
},
{
"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)
@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}"
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://{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_email", "your_rest_api_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