KaniLaser
SendGrid v3 Mail Send API Ruby Client that validates API request body using JSON Schema.
Installation
Add this line to your application's Gemfile:
gem 'kani_laser'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kani_laser
Usage
require 'kani_laser'
client = KaniLaser::Client.new(api_key: 'ZAPZAPZAP')
# see https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
client.send_mail(
personalizations: [
{
to: [
{
email: 'john@example.com'
}
],
subject: 'Hello, World!'
}
],
from: {
email: 'from_address@example.com'
},
content: [
{
type: 'text/plain',
value: 'Hello, World!'
}
]
)
For testing
client = KaniLaser::Client.new(options) do |faraday|
faraday.adapter :test, Faraday::Adapter::Test::Stubs.new do |stub|
stub.post('/v3/mail/send') do |_env|
[202, {}, '']
end
end
end
client.send_mail(...)
Use On-Behalf-Of
KaniLaser::Client.new(options) do |faraday|
faraday.headers['On-Behalf-Of'] = '<subuser_username>'
end
Request parameter validation error example
# Invalid Parameter
{
personalizations: [{ to: [{ email: 'sugawara@winebarrel.jp' }] }],
subject: 'Hello, World!',
from: { email: 'from_address@example.com' },
content: [{ value: 'Hello, World!' }],
}
JSON::Schema::ValidationError: The property '#/content/0' did not contain a required property of 'type'
Update schema
rake schema:update