From e76f5af8a3d0e580c13d75ba2006f342740ff717 Mon Sep 17 00:00:00 2001 From: Derich Pacheco Date: Wed, 27 Dec 2023 15:52:10 -0300 Subject: [PATCH] feat: Impl audiences api (#55) --- Gemfile.lock | 6 ++- examples/audiences.rb | 26 ++++++++++++ lib/resend/audiences.rb | 35 ++++++++++++++++ lib/resend/client.rb | 1 + spec/audiences_spec.rb | 90 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 examples/audiences.rb create mode 100644 lib/resend/audiences.rb create mode 100644 spec/audiences_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index aeef7ce..060eddf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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) diff --git a/examples/audiences.rb b/examples/audiences.rb new file mode 100644 index 0000000..1838b89 --- /dev/null +++ b/examples/audiences.rb @@ -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 \ No newline at end of file diff --git a/lib/resend/audiences.rb b/lib/resend/audiences.rb new file mode 100644 index 0000000..21e3321 --- /dev/null +++ b/lib/resend/audiences.rb @@ -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 diff --git a/lib/resend/client.rb b/lib/resend/client.rb index 1f6b158..7040587 100644 --- a/lib/resend/client.rb +++ b/lib/resend/client.rb @@ -4,6 +4,7 @@ require "resend/domains" require "resend/emails" require "resend/batch" +require "resend/audiences" require "httparty" module Resend diff --git a/spec/audiences_spec.rb b/spec/audiences_spec.rb new file mode 100644 index 0000000..9bd8119 --- /dev/null +++ b/spec/audiences_spec.rb @@ -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