-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
2 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
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../lib/resend" | ||
|
||
raise if ENV["RESEND_API_KEY"].nil? | ||
|
||
Resend.api_key = ENV["RESEND_API_KEY"] | ||
|
||
def example | ||
params = { | ||
name: "New Audience One", | ||
} | ||
audience = Resend::Audiences.create(params) | ||
puts "Created new audience: #{audience[:id]}" | ||
|
||
Resend::Audiences.get(audience[:id]) | ||
puts "retrieved audience id: #{audience[:id]}" | ||
|
||
audiences = Resend::Audiences.list | ||
puts audiences | ||
|
||
Resend::Audiences.remove audience[:id] | ||
puts "deleted #{audience[:id]}" | ||
end | ||
|
||
example |
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "resend/request" | ||
require "resend/errors" | ||
|
||
module Resend | ||
# Audiences api wrapper | ||
module Audiences | ||
class << self | ||
# https://resend.com/docs/api-reference/audiences/create-audience | ||
def create(params) | ||
path = "audiences" | ||
Resend::Request.new(path, params, "post").perform | ||
end | ||
|
||
# https://resend.com/docs/api-reference/audiences/get-audience | ||
def get(audience_id = "") | ||
path = "audiences/#{audience_id}" | ||
Resend::Request.new(path, {}, "get").perform | ||
end | ||
|
||
# https://resend.com/docs/api-reference/audiences/list-audiences | ||
def list | ||
path = "audiences" | ||
Resend::Request.new(path, {}, "get").perform | ||
end | ||
|
||
# https://resend.com/docs/api-reference/audiences/delete-audience | ||
def remove(audience_id = "") | ||
path = "audiences/#{audience_id}" | ||
Resend::Request.new(path, {}, "delete").perform | ||
end | ||
end | ||
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
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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Audiences" do | ||
|
||
describe "create audience" do | ||
|
||
before do | ||
Resend.configure do |config| | ||
config.api_key = "re_123" | ||
end | ||
end | ||
|
||
it "should create an audience record" do | ||
resp = { | ||
"object": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users" | ||
} | ||
params = { | ||
"name": "Registered Users", | ||
} | ||
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp) | ||
audience = Resend::Audiences.create(params) | ||
expect(audience[:id]).to eql("78261eea-8f8b-4381-83c6-79fa7120f1cf") | ||
expect(audience[:name]).to eql("Registered Users") | ||
expect(audience[:object]).to eql("audience") | ||
end | ||
end | ||
|
||
describe "get audience" do | ||
it "should retrieve audience" do | ||
resp = { | ||
"object": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users", | ||
"created_at": "2023-10-06T22:59:55.977Z" | ||
} | ||
allow(resp).to receive(:body).and_return(resp) | ||
allow(HTTParty).to receive(:send).and_return(resp) | ||
|
||
audience = Resend::Audiences.get(resp[:id]) | ||
|
||
expect(audience[:object]).to eql "audience" | ||
expect(audience[:id]).to eql "78261eea-8f8b-4381-83c6-79fa7120f1cf" | ||
expect(audience[:name]).to eql "Registered Users" | ||
expect(audience[:created_at]).to eql "2023-10-06T22:59:55.977Z" | ||
end | ||
end | ||
|
||
describe "list audiences" do | ||
it "should list audiences" do | ||
resp = { | ||
"object": "list", | ||
"data": [ | ||
{ | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"name": "Registered Users", | ||
"created_at": "2023-10-06T22:59:55.977Z" | ||
} | ||
] | ||
} | ||
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp) | ||
audiences = Resend::Audiences.list | ||
expect(audiences[:object]).to eql "list" | ||
expect(audiences[:data].empty?).to be false | ||
expect(audiences[:data].length).to eql(1) | ||
expect(audiences[:data].first[:id]).to eql("78261eea-8f8b-4381-83c6-79fa7120f1cf") | ||
expect(audiences[:data].first[:name]).to eql("Registered Users") | ||
end | ||
end | ||
|
||
describe "remove audience" do | ||
it "should remove audience by id" do | ||
|
||
resp = { | ||
"object": "audience", | ||
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", | ||
"deleted": true | ||
} | ||
|
||
allow(resp).to receive(:body).and_return(resp) | ||
allow(HTTParty).to receive(:send).and_return(resp) | ||
|
||
deleted = Resend::Audiences.remove("78261eea-8f8b-4381-83c6-79fa7120f1cf") | ||
expect(deleted[:object]).to eql("audience") | ||
expect(deleted[:id]).to eql("78261eea-8f8b-4381-83c6-79fa7120f1cf") | ||
expect(deleted[:deleted]).to be true | ||
end | ||
end | ||
end |