Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Send attachments (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Oct 28, 2023
1 parent 6c120ac commit 988bf98
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ group :test do
gem 'rspec-rails'
gem 'selenium-webdriver'
gem 'webmock'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'simplecov', require: false
end

Expand Down
5 changes: 0 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webdrivers (5.2.0)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
Expand Down Expand Up @@ -329,7 +325,6 @@ DEPENDENCIES
stripe
tzinfo-data
web-console (>= 4.1.0)
webdrivers
webmock

RUBY VERSION
Expand Down
16 changes: 14 additions & 2 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def deliver
SendGrid::Email.new(email: to),
content
)
attachments.each do |a|
attachment = SendGrid::Attachment.new
attachment.content = a['content']
attachment.filename = a['filename']
attachment.type = a['type']
mail.add_attachment(attachment)
end
sg = SendGrid::API.new(api_key: Rails.application.credentials.sendgrid[:api_key])
sg.client.mail._('send').post(request_body: mail.to_json)
end
Expand All @@ -23,9 +30,14 @@ def self.from_params(params)
notifications = emails.map do |email|
user = User.find_or_initialize_by(email:)
user.save(validate: false)
user.notifications.create!(
{ to: email.squish }.merge(params.permit(:subject, :body))
notification_params = { to: email.squish }.merge(
params.permit(:subject, :body, attachments: %i[
content
type
filename
])
)
user.notifications.create!(notification_params)
end
if notifications.count == 1
notifications[0]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20231028141221_add_attachments_to_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAttachmentsToNotifications < ActiveRecord::Migration[6.1]
def change
add_column :notifications, :attachments, :json, default: []
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions docs/http/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,38 @@ The HTTP API is how emails are sent from Continuous Integration environments.
|to|string|true|
|subject|string|true|
|body|string|false|
|attachments|array|false|

#### Attachments

|name|type|required|description|
|---|---|---|
|type|string|true|The content's MIME type|
|content|string|true|The Base64 encoded content of the attachment|
|filename|string|true|The attachment's filename|

### Example

#### application/x-www-form-urlencoded
```bash
curl --request POST 'https://www.cinotify.cc/api/notify' \
-d "[email protected]&subject=building main&body=hello."
-d "[email protected]&subject=building main&body=hello.&attachments[][type]=text/plain&attachments[][content]=aGVsbG8sIHdvcmxkIQ==&attachments[][filename]=hello.txt"
```

#### json
```bash
curl -X POST 'https://www.cinotify.cc/api/notify' \
-H "Content-Type: application/json" \
-d '{"to":"[email protected]", "subject": "building main", "body":"hello."}'
-d '{
"to": "[email protected]",
"subject": "building main",
"body": "hello.",
"attachments": [
{
"type": "text/plain",
"content": "aGVsbG8sIHdvcmxkIQ==",
"filename": "hello.txt"
}
]
}'
```
21 changes: 21 additions & 0 deletions spec/controllers/api/api_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,25 @@
}
end.to change { Notification.count }.by(2)
end
it 'supports attachments' do
stub_request(:post, 'https://api.sendgrid.com/v3/mail/send')
.with(
body: '{"from":{"email":"[email protected]"},"subject":"hello w/ attachments","personalizations":[{"to":[{"email":"[email protected]"}]}],"content":[{"type":"text/plain","value":"Please see the attached file(s)"}],"attachments":[{"content":"VGhpcyBpcyBhIGZpbGUgdG8gYmUgYXR0YWNoZWQgdG8gYW4gZW1haWwuCg==","type":"text/plain","filename":"attachment.txt"}]}'
)
.to_return(status: 200, body: '[]', headers: {})
expect do
post :notify, params: {
to: '[email protected]',
subject: 'hello w/ attachments',
body: 'Please see the attached file(s)',
attachments: [
{
content: Base64.strict_encode64(File.read("spec/fixtures/attachment.txt")),
type: "text/plain",
filename: "attachment.txt"
}
]
}
end.to change { Notification.count }.by(1)
end
end
1 change: 1 addition & 0 deletions spec/fixtures/attachment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file to be attached to an email.
6 changes: 2 additions & 4 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
require 'webdrivers/chromedriver'

Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless
Capybara.default_driver = :selenium_headless
Capybara.javascript_driver = :selenium_headless
require 'webmock/rspec'
WebMock.disable_net_connect!(allow: %r{(__identify__|shutdown|session|chromedriver.storage.googleapis.com)})
# Add additional requires below this line. Rails is not loaded until this point!
Expand Down

0 comments on commit 988bf98

Please sign in to comment.