Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement broadcasts #83

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/api_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ def remove
end

create
list
remove
# list
# remove
34 changes: 34 additions & 0 deletions examples/broadcasts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative "../lib/resend"

raise if ENV["RESEND_API_KEY"].nil?

Resend.api_key = ENV["RESEND_API_KEY"]

params = {
from: "[email protected]",
subject: "Hello",
audience_id: "78b8d3bc-a55a-45a3-aee6-6ec0a5e13d7e", # replace with an existing audience id
text: "Hello, how are you?",
}

broadcast = Resend::Broadcasts.create(params)
puts "created broadcast: #{broadcast[:id]}"

send_params = {
broadcast_id: broadcast[:id],
scheduled_at: "in 1 min",
}

sent_broadcast = Resend::Broadcasts.send(send_params)
puts "sent broadcast: #{sent_broadcast[:id]}"

broadcasts = Resend::Broadcasts.list
puts broadcasts

retrieved = Resend::Broadcasts.get(broadcast[:id])
puts "retrieved #{retrieved[:id]}"

Resend::Broadcasts.remove(broadcast[:id])
puts "removed #{broadcast[:id]}"
41 changes: 41 additions & 0 deletions lib/resend/broadcasts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "resend/request"
require "resend/errors"

module Resend
# broadcasts api wrapper
module Broadcasts
class << self
# https://resend.com/docs/api-reference/broadcasts/create-broadcast
def create(params = {})
path = "broadcasts"
Resend::Request.new(path, params, "post").perform
end

# https://resend.com/docs/api-reference/broadcasts/send-broadcast
def send(params = {})
path = "broadcasts/#{params[:broadcast_id]}/send"
Resend::Request.new(path, params, "post").perform
end

# https://resend.com/docs/api-reference/broadcasts/list-broadcasts
def list
path = "broadcasts"
Resend::Request.new(path, {}, "get").perform
end

# https://resend.com/docs/api-reference/broadcasts/delete-broadcast
def remove(broadcast_id = "")
path = "broadcasts/#{broadcast_id}"
Resend::Request.new(path, {}, "delete").perform
end

# https://resend.com/docs/api-reference/broadcasts/get-broadcast
def get(broadcast_id = "")
path = "broadcasts/#{broadcast_id}"
Resend::Request.new(path, {}, "get").perform
end
end
end
end
7 changes: 4 additions & 3 deletions lib/resend/client.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require "resend/audiences"
require "resend/api_keys"
require "resend/domains"
require "resend/emails"
require "resend/broadcasts"
require "resend/batch"
require "resend/audiences"
require "resend/contacts"
require "resend/domains"
require "resend/emails"
require "httparty"

module Resend
Expand Down
129 changes: 129 additions & 0 deletions spec/broadcasts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# frozen_string_literal: true

RSpec.describe "Broadcasts" do

before do
Resend.configure do |config|
config.api_key = "re_123"
end
end

describe "create" do
it "should create broadcast" do
resp = {
"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
}
params = {
"audience_id": "123123"
}
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp)
expect(Resend::Broadcasts.create(params)[:id]).to eql("49a3999c-0ce1-4ea6-ab68-afcd6dc2e794")
end
end

describe "send" do
it "should send broadcast" do
resp = {
"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
}
params = {
"broadcast_id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794",
"scheduled_at": "in 1 min"
}
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp)
expect(Resend::Broadcasts.send(params)[:id]).to eql("49a3999c-0ce1-4ea6-ab68-afcd6dc2e794")
end
end

describe "list" do
it "should list broadcasts" do
resp = {
"object": "list",
"data": [
{
"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794",
"audience_id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"status": "draft",
"created_at": "2024-11-01T15:13:31.723Z",
"scheduled_at": nil,
"sent_at": nil
},
{
"id": "559ac32e-9ef5-46fb-82a1-b76b840c0f7b",
"audience_id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"status": "sent",
"created_at": "2024-12-01T19:32:22.980Z",
"scheduled_at": "2024-12-02T19:32:22.980Z",
"sent_at": "2024-12-02T19:32:22.980Z"
}
]
}
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp)

broadcasts = Resend::Broadcasts.list[:data]

expect(broadcasts.length).to eql(2)

expect(broadcasts[0][:id]).to eql("49a3999c-0ce1-4ea6-ab68-afcd6dc2e794")
expect(broadcasts[0][:audience_id]).to eql("78261eea-8f8b-4381-83c6-79fa7120f1cf")
expect(broadcasts[0][:status]).to eql("draft")
expect(broadcasts[0][:created_at]).to eql("2024-11-01T15:13:31.723Z")
expect(broadcasts[0][:scheduled_at]).to eql(nil)
expect(broadcasts[0][:sent_at]).to eql(nil)

expect(broadcasts[1][:id]).to eql("559ac32e-9ef5-46fb-82a1-b76b840c0f7b")
expect(broadcasts[1][:audience_id]).to eql("78261eea-8f8b-4381-83c6-79fa7120f1cf")
expect(broadcasts[1][:status]).to eql("sent")
expect(broadcasts[1][:created_at]).to eql("2024-12-01T19:32:22.980Z")
expect(broadcasts[1][:scheduled_at]).to eql("2024-12-02T19:32:22.980Z")
expect(broadcasts[1][:sent_at]).to eql("2024-12-02T19:32:22.980Z")
end
end

describe "remove" do
it "should remove broadcast" do
allow_any_instance_of(Resend::Request).to receive(:perform).and_return("")
expect { Resend::Broadcasts.remove }.not_to raise_error
end
end

describe "get broadcast" do

it "should retrieve a broadcast" do

resp = {
"object": "broadcast",
"id": "559ac32e-9ef5-46fb-82a1-b76b840c0f7b",
"name": "Announcements",
"audience_id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"from": "Acme <[email protected]>",
"subject": "hello world",
"reply_to": nil,
"preview_text": "Check out our latest announcements",
"status": "draft",
"created_at": "2024-12-01T19:32:22.980Z",
"scheduled_at": nil,
"sent_at": nil
}

allow(resp).to receive(:body).and_return(resp)
allow(HTTParty).to receive(:send).and_return(resp)

broadcast = Resend::Broadcasts.get("559ac32e-9ef5-46fb-82a1-b76b840c0f7b")

expect(broadcast[:object]).to eql "broadcast"
expect(broadcast[:id]).to eql "559ac32e-9ef5-46fb-82a1-b76b840c0f7b"
expect(broadcast[:name]).to eql "Announcements"
expect(broadcast[:audience_id]).to eql "78261eea-8f8b-4381-83c6-79fa7120f1cf"
expect(broadcast[:from]).to eql "Acme <[email protected]>"
expect(broadcast[:subject]).to eql "hello world"
expect(broadcast[:reply_to]).to eql nil
expect(broadcast[:preview_text]).to eql "Check out our latest announcements"
expect(broadcast[:status]).to eql "draft"
expect(broadcast[:created_at]).to eql "2024-12-01T19:32:22.980Z"
expect(broadcast[:scheduled_at]).to eql nil
expect(broadcast[:sent_at]).to eql nil
end
end

end
Loading