-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into rohankh-patch-1
- Loading branch information
Showing
24 changed files
with
504 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
engines: | ||
duplication: | ||
enabled: true | ||
config: | ||
languages: | ||
- ruby | ||
fixme: | ||
enabled: true | ||
rubocop: | ||
enabled: true | ||
bundler-audit: | ||
enabled: true | ||
ratings: | ||
paths: | ||
- "**.rb" | ||
exclude_paths: | ||
- examples/ | ||
- spec/ | ||
- test/ | ||
- gemfiles/ |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2014-2017 SendGrid, Inc. | ||
Copyright (c) 2014-2018 SendGrid, Inc. | ||
|
||
MIT License | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ Version 3.X.X+ of this library provides full support for all SendGrid [Web API v | |
|
||
This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create [issues](https://github.com/sendgrid/sendgrid-ruby/issues) and [pull requests](https://github.com/sendgrid/sendgrid-ruby/blob/master/CONTRIBUTING.md) or simply upvote or comment on existing issues or pull requests. | ||
|
||
Please browse the rest of this README for further detail. | ||
Please browse the rest of this README for further details. | ||
|
||
We appreciate your continued support, thank you! | ||
|
||
|
@@ -83,16 +83,17 @@ The following is the minimum needed code to send an email with the [/mail/send H | |
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
from = Email.new(email: '[email protected]') | ||
to = Email.new(email: '[email protected]') | ||
from = SendGrid::Email.new(email: '[email protected]') | ||
to = SendGrid::Email.new(email: '[email protected]') | ||
subject = 'Sending with SendGrid is Fun' | ||
content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') | ||
content = SendGrid::Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') | ||
mail = SendGrid::Mail.new(from, subject, to, content) | ||
|
||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
response = sg.client.mail._('send').post(request_body: mail.to_json) | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
@@ -131,6 +132,7 @@ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | |
response = sg.client.mail._("send").post(request_body: data) | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
@@ -142,6 +144,7 @@ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | |
response = sg.client.suppression.bounces.get() | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
@@ -153,6 +156,7 @@ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | |
response = sg.client._("suppression/bounces").get() | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ This documentation provides examples for specific use cases. Please [open an iss | |
# Table of Contents | ||
|
||
* [Transactional Templates](#transactional-templates) | ||
* [Legacy Templates](#legacy-templates) | ||
* [How to Setup a Domain Whitelabel](#domain-whitelabel) | ||
* [How to View Email Statistics](#email-statistics) | ||
|
||
|
@@ -13,6 +14,106 @@ For this example, we assume you have created a [transactional template](https:// | |
|
||
Template ID (replace with your own): | ||
|
||
```text | ||
d-2c214ac919e84170b21855cc129b4a5f | ||
``` | ||
Email Subject: | ||
```text | ||
{{subject}} | ||
``` | ||
|
||
Template Body: | ||
|
||
```html | ||
<html> | ||
<head> | ||
<title></title> | ||
</head> | ||
<body> | ||
Hello {{name}}, | ||
<br/><br/> | ||
I'm glad you are trying out the template feature! | ||
<br/><br/> | ||
I hope you are having a great day in {{city}} :) | ||
<br/><br/> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## With Mail Helper Class | ||
```ruby | ||
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
mail = Mail.new | ||
mail.from = Email.new(email: '[email protected]') | ||
personalization = Personalization.new | ||
personalization.add_to(Email.new(email: '[email protected]')) | ||
personalization.add_dynamic_template_data({ | ||
"subject" => "Testing Templates", | ||
"name" => "Example User", | ||
"city" => "Denver" | ||
}) | ||
mail.add_personalization(personalization) | ||
mail.template_id = 'd-2c214ac919e84170b21855cc129b4a5f' | ||
|
||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
begin | ||
response = sg.client.mail._("send").post(request_body: mail.to_json) | ||
rescue Exception => e | ||
puts e.message | ||
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
## Without Mail Helper Class | ||
|
||
```ruby | ||
require 'sendgrid-ruby' | ||
include SendGrid | ||
|
||
data = JSON.parse('{ | ||
"personalizations": [ | ||
{ | ||
"to": [ | ||
{ | ||
"email": "[email protected]" | ||
} | ||
], | ||
"dynamic_template_data": { | ||
"subject": "Testing Templates", | ||
"name": "Example User", | ||
"city": "Denver" | ||
} | ||
} | ||
], | ||
"from": { | ||
"email": "[email protected]" | ||
}, | ||
"template_id": "d-2c214ac919e84170b21855cc129b4a5f" | ||
}') | ||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
begin | ||
response = sg.client.mail._("send").post(request_body: data) | ||
rescue Exception => e | ||
puts e.message | ||
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
<a name="legacy-templates"></a> | ||
# Legacy Templates | ||
|
||
For this example, we assume you have created a [legacy template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. | ||
|
||
Template ID (replace with your own): | ||
|
||
```text | ||
13b8f94f-bcae-4ec6-b752-70d6cb59f932 | ||
``` | ||
|
@@ -57,7 +158,6 @@ personalization.add_to(Email.new(email: '[email protected]')) | |
personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User')) | ||
personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver')) | ||
mail.add_personalization(personalization) | ||
mail.add_content(Content.new(type: 'text/html', value: 'I\'m replacing the <strong>body tag</strong>')) | ||
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932' | ||
|
||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
|
@@ -68,6 +168,7 @@ rescue Exception => e | |
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
@@ -95,12 +196,6 @@ data = JSON.parse('{ | |
"from": { | ||
"email": "[email protected]" | ||
}, | ||
"content": [ | ||
{ | ||
"type": "text/html", | ||
"value": "I\'m replacing the <strong>body tag</strong>" | ||
} | ||
], | ||
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932" | ||
}') | ||
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) | ||
|
@@ -111,6 +206,7 @@ rescue Exception => e | |
end | ||
puts response.status_code | ||
puts response.body | ||
puts response.parsed_body | ||
puts response.headers | ||
``` | ||
|
||
|
@@ -141,3 +237,51 @@ Find more information about all of SendGrid's whitelabeling related documentatio | |
You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](https://github.com/sendgrid/sendgrid-ruby/blob/master/USAGE.md#stats). | ||
|
||
Alternatively, we can post events to a URL of your choice via our [Event Webhook](https://sendgrid.com/docs/API_Reference/Webhooks/event.html) about events that occur as SendGrid processes your email. | ||
|
||
You can also use the email statistics helper to make it easier to interact with the API. | ||
|
||
```ruby | ||
require 'sendgrid-ruby' | ||
require 'date' | ||
|
||
include SendGrid | ||
|
||
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client | ||
stats = SendGrid::EmailStats.new(sendgrid_client: sg_client) | ||
|
||
# Fetch stats by day, between 2 dates | ||
from = Date.new(2017, 10, 01) | ||
to = Date.new(2017, 10, 12) | ||
|
||
email_stats = stats.by_day(from, to) | ||
|
||
email_stats.metrics | ||
|
||
if !email_stats.error? | ||
email_stats.metrics.each do |metric| | ||
puts "Date - #{metric.date}" | ||
puts "Number of Requests - #{metric.requests}" | ||
puts "Bounces - #{metric.bounces}" | ||
puts "Opens - #{metric.opens}" | ||
puts "Clicks - #{metric.clicks}" | ||
end | ||
end | ||
|
||
# Fetch stats by week, between 2 dates for a category | ||
from = Date.new(2017, 10, 01) | ||
to = Date.new(2017, 10, 12) | ||
category = 'abcd' | ||
|
||
email_stats = stats.by_week(from, to, category) | ||
|
||
if !email_stats.error? | ||
email_stats.metrics.each do |metric| | ||
puts "Date - #{metric.date}" | ||
puts "Number of Requests - #{metric.requests}" | ||
puts "Bounces - #{metric.bounces}" | ||
puts "Opens - #{metric.opens}" | ||
puts "Clicks - #{metric.clicks}" | ||
end | ||
end | ||
|
||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM ruby:2.4.2-jessie | ||
|
||
# Clone sendgrid-ruby | ||
WORKDIR /sources | ||
RUN git clone https://github.com/sendgrid/sendgrid-ruby.git | ||
|
||
# Bundle | ||
WORKDIR /sources/sendgrid-ruby | ||
RUN bundle install | ||
|
||
# Install prism | ||
RUN curl https://raw.githubusercontent.com/stoplightio/prism/master/install.sh | sh |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Docker image for sendgrid-ruby | ||
|
||
## Quickstart | ||
1. [Install Docker](https://docs.docker.com/engine/installation/) on your machine. | ||
2. Run `docker run --rm -it sendgrid/sendgrid-ruby irb`. | ||
3. Run `require './lib/sendgrid-ruby.rb'`. | ||
|
||
## Poke around | ||
|
||
If you would like to just poke around in the image and check some examples: | ||
```sh | ||
docker run --rm -it sendgrid/sendgrid-ruby bash | ||
``` | ||
|
||
If you want to mount your fork or specific version of the gem: | ||
```sh | ||
docker run --rm -v /path/to/local/sendgrid-ruby:/sources/sendgrid-ruby -it sendgrid/sendgrid-ruby bash | ||
``` | ||
|
||
## Running tests | ||
|
||
If you would like to run the tests present in the image: | ||
```sh | ||
docker run --rm sendgrid/sendgrid-ruby rake | ||
``` | ||
|
||
If you want to run tests on your fork or a specific version, mount the codebase onto the image: | ||
```sh | ||
docker run --rm -v /path/to/local/sendgrid-ruby:/sources/sendgrid-ruby sendgrid/sendgrid-ruby rake | ||
``` |
Oops, something went wrong.