Sendgrid::API
A Ruby interface to the SendGrid API.
API Coverage
Check which SendGrid APIs are currently being covered by this gem:
https://github.com/renatosnrg/sendgrid-api/wiki/SendGrid-API-Coverage
Installation
Add this line to your application's Gemfile:
gem 'sendgrid-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sendgrid-api
Configuration
client = Sendgrid::API::Client.new('YOUR_USER', 'YOUR_KEY')
Web API
Profile
profile = Sendgrid::API::Entities::Profile.new(:first_name => 'Your first name',
:last_name => 'Your last name')
profile = client.profile.get
response = client.profile.set(profile)
Mail
response = client.mail.send(:to => 'johndoe@example.com',
:from => 'brian@example.com',
:subject => 'test using sendgrid api',
:text => 'this is a test')
x_smtpapi = {:category => ['sendgrid-api test']}
response = client.mail.send(:to => 'johndoe@example.com',
:from => 'brian@example.com',
:subject => 'test using sendgrid api',
:text => 'this is a test',
:x_smtpapi => x_smtpapi)
text = StringIO.new("This is my file content")
file = Faraday::UploadIO.new(text, 'plain/text', 'sample.txt')
response = client.mail.send(:to => 'johndoe@example.com',
:from => 'brian@example.com',
:subject => 'test using sendgrid api',
:text => 'this is a test',
:files => {'sample.txt' => file})
Statistics
stats = client.stats.advanced(:start_date => '2013-01-01', :data_type => 'global')
Marketing Email API
Lists
list = Sendgrid::API::Entities::List.new(:list => 'sendgrid-api list test')
response = client.lists.add(list)
response = client.lists.edit(list, 'new name')
selected_list = client.lists.get(list)
all_lists = client.lists.get
response = client.lists.delete(list)
Emails
email1 = Sendgrid::API::Entities::Email.new(
:email => 'johndoe@example.com',
:name => 'John Doe'
)
email2 = Sendgrid::API::Entities::Email.new(
:email => 'brian@example.com',
:name => 'Brian'
)
emails = [email1, email2]
listname = 'sendgrid-api list test'
response = client.emails.add(listname, emails)
all_emails = client.emails.get(listname)
selected_emails = client.emails.get(listname, email1)
response = client.emails.delete(listname, email1)
Sender Adresses
address = Sendgrid::API::Entities::SenderAddress.new(
:identity => 'sendgrid-api sender address test',
:name => 'Sendgrid',
:email => 'contact@sendgrid.com',
:address => '1065 N Pacificenter Drive, Suite 425',
:city => 'Anaheim',
:state => 'CA',
:zip => '92806',
:country => 'US'
)
response = client.sender_addresses.add(address)
address.city = 'Pleasanton'
response = client.sender_addresses.edit(address, 'new identity')
addresses = client.sender_addresses.list
address = client.sender_addresses.get(address)
response = client.sender_addresses.delete(address)
Marketing Emails
marketing_email = Sendgrid::API::Entities::MarketingEmail.new(
:identity => 'sendgrid-api sender address test',
:name => 'sendgrid-api marketing email test',
:subject => 'My Marketing Email Test',
:text => 'My text',
:html => 'My HTML'
)
response = client.marketing_emails.add(marketing_email)
marketing_email.html = 'My new HTML'
response = client.marketing_emails.edit(marketing_email, 'new name')
newsletter = client.marketing_emails.get(marketing_email)
newsletters = client.marketing_emails.list
response = client.marketing_emails.delete(marketing_email)
Categories
category = Sendgrid::API::Entities::Category.new(:category => 'sendgrid-api test')
marketing_email = 'sendgrid-api marketing email test'
response = client.categories.create(category)
response = client.categories.add(marketing_email, category)
response = client.categories.remove(marketing_email, category)
categories = client.categories.list
Recipients
listname = 'sendgrid-api list test'
marketing_email = 'sendgrid-api marketing email test'
response = client.recipients.add(listname, marketing_email)
lists = client.recipients.get(marketing_email)
response = client.recipients.delete(listname, marketing_email)
Schedule
marketing_email = 'sendgrid-api marketing email test'
response = client.schedule.add(marketing_email)
response = client.schedule.add(marketing_email, :after => 20)
response = client.schedule.add(marketing_email, :at => (Time.now + 10*60))
schedule = client.schedule.get(marketing_email)
response = client.schedule.delete(marketing_email)
Tests
This gem has offline and online tests written using RSpec. Offline tests use Webmock to stub HTTP requests, while online tests performs the requests to Sendgrid to ensure all the calls and responses are correctly.
The online tests are written in a way that they don't change the state of the objects already existing on SendGrid account. It means if something is added, it's removed at the end of the test. If something is changed, it's changed back after the test finishes. If it's necessary to remove something, it's added before the test starts.
Use the SENDGRID_USER and SENDGRID_KEY environment variables to authenticate the online tests.
To run all tests (both offline and online):
$ ALL=1 SENDGRID_USER=your_user SENDGRID_KEY=your_key bundle exec rake spec
To run only online tests:
$ SENDGRID_USER=your_user SENDGRID_KEY=your_key bundle exec rake spec[online]
To run only offline tests (default):
$ bundle exec rake spec
Contributing
If you want to contribute to cover more APIs or improve something already implemented, follow these steps:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes - do not forget tests (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request