Skip to content

Commit

Permalink
feat: Impl audiences api (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Dec 27, 2023
1 parent 129555a commit e76f5af
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
resend (0.8.0)
httparty (~> 0.19.1)
resend (0.8.1)
httparty (>= 0.19.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -120,6 +120,8 @@ GEM
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.10-arm64-darwin)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
Expand Down
26 changes: 26 additions & 0 deletions examples/audiences.rb
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
35 changes: 35 additions & 0 deletions lib/resend/audiences.rb
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
1 change: 1 addition & 0 deletions lib/resend/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "resend/domains"
require "resend/emails"
require "resend/batch"
require "resend/audiences"
require "httparty"

module Resend
Expand Down
90 changes: 90 additions & 0 deletions spec/audiences_spec.rb
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

0 comments on commit e76f5af

Please sign in to comment.