Hey there, fellow Ruby enthusiast! Ready to supercharge your CRM game? Let's dive into integrating the Agile CRM API into your Ruby project. This powerful combo will have you managing contacts like a pro in no time.
Before we jump in, make sure you've got:
Got those? Great! Let's roll.
First things first, let's get our project off the ground:
mkdir agile_crm_integration cd agile_crm_integration bundle init
Now, let's add some firepower to our Gemfile
:
gem 'httparty' gem 'dotenv'
Run bundle install
, and we're locked and loaded.
Security first! Let's keep those API credentials safe:
.env
file in your project rootAGILE_CRM_API_KEY=your_api_key
AGILE_CRM_EMAIL=your_email
AGILE_CRM_DOMAIN=your_domain
Now, let's set up basic authentication:
require 'dotenv/load' require 'httparty' class AgileCRM include HTTParty base_uri "https://#{ENV['AGILE_CRM_DOMAIN']}.agilecrm.com/dev/api" basic_auth ENV['AGILE_CRM_EMAIL'], ENV['AGILE_CRM_API_KEY'] end
Time to flex those API muscles! Let's start with the basics:
def get_contacts self.class.get('/contacts') end
def create_contact(contact_data) self.class.post('/contacts', body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' }) end
def update_contact(id, contact_data) self.class.put("/contacts/#{id}", body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' }) end
def delete_contact(id) self.class.delete("/contacts/#{id}") end
Let's make sense of what the API's telling us:
def handle_response(response) case response.code when 200..299 JSON.parse(response.body) else raise "API request failed: #{response.code} - #{response.message}" end end
Let's wrap it all up in a neat little package:
class AgileCRM # ... previous code ... def get_contacts handle_response(self.class.get('/contacts')) end def create_contact(contact_data) handle_response(self.class.post('/contacts', body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' })) end def update_contact(id, contact_data) handle_response(self.class.put("/contacts/#{id}", body: contact_data.to_json, headers: { 'Content-Type' => 'application/json' })) end def delete_contact(id) handle_response(self.class.delete("/contacts/#{id}")) end private def handle_response(response) # ... previous handle_response method ... end end
Let's make sure everything's working as smooth as butter:
require 'minitest/autorun' require 'webmock/minitest' class AgileCRMTest < Minitest::Test def setup @crm = AgileCRM.new end def test_get_contacts stub_request(:get, "https://#{ENV['AGILE_CRM_DOMAIN']}.agilecrm.com/dev/api/contacts") .to_return(status: 200, body: '[{"id": 1, "name": "John Doe"}]') contacts = @crm.get_contacts assert_equal 1, contacts.first['id'] assert_equal 'John Doe', contacts.first['name'] end # Add more tests for create, update, and delete end
Remember, play nice with the API:
And there you have it! You've just built a sleek Agile CRM API integration in Ruby. Pretty cool, right? Now go forth and manage those contacts like a boss!
For more in-depth info, check out the Agile CRM API docs. Happy coding!