Working with the Nylas Contacts API and Ruby

Learn about the Nylas Contacts API, its available features, and how to work with them using the Nylas Ruby SDK!

Working with the Nylas Contacts API and Ruby

Managing contacts is an important task. They are usually created automatically when we receive an email from someone who’s not on our contact list, but this could contain only a minimal amount of information. By managing contacts, we can complete this information (provided that we get it) or simply delete it if it’s not essential. Using the Contacts API and Ruby will help us to resolve those issues in a fast and easy way.

You might have already read Working with the Nylas Contacts API and Python, and you may be wondering, how can I do the same using Ruby? Well, here’s how you can.

Is your system ready?

If you already have the Nylas Ruby SDK installed and your Ruby environment is configured, then continue along with the blog.

Otherwise, I would recommend that you read the post How to Send Emails with the Nylas Ruby SDK where the basic setup is clearly explained.

Return all contacts

An important task would be to review your contacts to ensure they are all in good shape. We will create a file called Read_Contacts.rb to return all contacts. Contacts come from two sources: inbox (auto-generated via email) and address_book (manually created by us).

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Access and print all contacts information
# We're accessing manually created contacts only
contacts = nylas.contacts.where({"source"=>"address_book"})
contacts.each{|contact|
	puts contact.to_json
	puts "\n"
}

We can run this by typing the following on the terminal:

$ ruby Read_Contacts.rb
Read contacts

Return a contact

From the previous result, we can see that each contact is associated with an id. We can use that id to return a particular contact. Let’s create a file called Return_Contact.rb

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Find and print a contact information
contact = nylas.contacts.find('{id}')
puts contact.to_json

We can run this by typing the following on the terminal:

$ ruby Return_Contact.rb
Return a contact

Now that we know how to find a contact and retrieve the information, it’s time for the next step, which is creating a new contact.

Create a new contact

When we create a new contact, it will be assigned to address_book. Keep in mind that we’re not going to be able to assign an image to the contact as the corresponding field picture_url is for reading only.

We’re going to call this file CreateContact.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Create the contact and specify the details
contact = nylas.contacts.create
contact.given_name = "Jade"
contact.surname = "Puget"
contact.emails = [{type: 'personal', email: "jade@afi.net"}]
contact.phone_numbers = [{type: 'mobile', number: "+15554567890"}]
# Save the contact
contact.save()
puts contact.to_json

We can run this by typing the following on the terminal:

$ ruby CreateContact.rb
Create a new contact

The script will return the details of the newly created contact.

Update a contact

Now that we know how to create a contact, we can also easily update it. We’re to create a file called UpdateContact.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Find the contact
contact = nylas.contacts.find({id})
# Save new information
contact.company_name = "A Fire Inside"
contact.notes = "Guitar for AFI"

# Save the contact
contact.save()
puts contact.to_json

We can run this by typing the following on the terminal:

$ ruby UpdateContact.rb
Update an existing contact

Just by adding the needed fields, our contact gets successfully updated.

Download contact’s picture

If we know that any of our contacts have a profile picture, then we can easily download it. We’re going to call this file DownloadContactPicture.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

# Find the contact
contact = nylas.contacts.find('{id}')
# Get the contact's image
picture = contact.picture
# Open a local file to save
File.open("Blag.jpg","wb") do |f|
# Write the contents of the image
	f.write File.open(picture, 'rb') {|file| file.read }
end

We can run this by typing the following on the terminal:

$ ruby DownloadContactPicture.rb
Download picture

The image will be downloaded to the exact location as our script.

Delete a Contact

Now we have done everything we can with our contacts, it’s time to learn how to delete them.

Let’s create a file called DeleteContact.rb:

#!/usr/bin/env ruby
# Import your dependencies
require 'dotenv/load'
require 'nylas'

# Initialize your Nylas API client
nylas = Nylas::API.new(
    app_id: ENV["CLIENT_ID"],
    app_secret: ENV["CLIENT_SECRET"],
    access_token: ENV["ACCESS_TOKEN"]
)

contact = nylas.contacts.find('{id}').destroy
puts contact.to_json

We can run this by typing the following on the terminal:

$ ruby DeleteContact.rb
Delete an existing contact

This job_status_id means that the contact is in the queue to be deleted, and it will be deleted in the next few seconds.

Working with the Contacts API and Ruby becomes an easy job, when we use the right tools.

If you want to learn more, please go to our documentation page.

You May Also Like

Transactional Email APIs vs Contextual Email APIs
Best email tracker
Find the best email tracker and elevate your app’s email game
How to create and read Webhooks with PHP, Koyeb and Bruno

Subscribe for our updates

Please enter your email address and receive the latest updates.