resend 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5654ce4fd9c6c9a097803ad5a55c064d5d21bb2c9fa3dddcbe7a334914fba8c3
4
- data.tar.gz: 729b8b8acf1a8dd6c016ce8cc5f509a39bfd8806cbfc11f044e0dd732a011088
3
+ metadata.gz: e3bd3c129bc391019cd0d3f1c30b40da0354cdc38ab1f2eb8b09830f2276afad
4
+ data.tar.gz: e7d83291e24db93d771f1a7588b5fc57353f62c86b97b5f9df1d09151f403284
5
5
  SHA512:
6
- metadata.gz: 36611a0d85ebc0938e0edb0df7e3bd9c9e69fb9ed87e1c0d3f19a4512f6305447b7169ac61ad7ce30cd710003d28c0a099bd7901b892f2cb1446f8a7910c4ed4
7
- data.tar.gz: 3ec1ae9d87ae9250a8e2eb0463878fca57013d1857ee8aee4fcab208fc7fa2e84c24cb02cc82bd5585b419c3174a24493af20dac27ceef99c7f3229545377dcd
6
+ metadata.gz: c27434e2d97b55af4a0b8bde50e5cf3c398498c460451e3ee5c367d6a78a47c8f65d3ecf7c5b7455de2a40040c4e6533b587fa7a0ee2af57d7ec262f2c2e7bf0
7
+ data.tar.gz: 671f1d0e1fb99bcfbd254ac0fa70b15c52da9406d0800188e77585be420ed7ec6ef61d8a9a4778ec03414c5275f30fb28633a17825ce7a8e110fcbb180f80c15
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4
4
  ![Build](https://github.com/drish/resend-ruby/actions/workflows/build.yml/badge.svg)
5
5
  [![Gem Version](https://badge.fury.io/rb/resend.svg)](https://badge.fury.io/rb/resend)
6
+
6
7
  ---
7
8
 
8
9
  ## Installation
@@ -10,11 +11,13 @@
10
11
  To install Resend Ruby and Rails SDK, simply execute the following command in a terminal:
11
12
 
12
13
  Via RubyGems:
14
+
13
15
  ```
14
16
  gem install resend
15
17
  ```
16
18
 
17
19
  Via Gemfile:
20
+
18
21
  ```
19
22
  gem 'resend'
20
23
  ```
@@ -37,6 +40,19 @@ Resend.configure do |config|
37
40
  end
38
41
  ```
39
42
 
43
+ The `#api_key` method also accepts a block without arguments, which can be useful for lazy or dynamic loading of API keys.
44
+
45
+ ```ruby
46
+ require "resend"
47
+ Resend.api_key = -> { ENV["RESEND_API_KEY"] }
48
+ ```
49
+
50
+ ```ruby
51
+ Resend.configure do |config|
52
+ config.api_key = -> { Current.user.resend_api_key } # Assumes the user has a `resend_api_key` attribute.
53
+ end
54
+ ```
55
+
40
56
  ## Example
41
57
 
42
58
  ```rb
@@ -56,7 +72,6 @@ puts r
56
72
 
57
73
  You can view all the examples in the [examples folder](https://github.com/drish/resend-ruby/tree/main/examples)
58
74
 
59
-
60
75
  # Rails and ActiveMailer support
61
76
 
62
77
  This gem can be used as an ActionMailer delivery method, add this to your `config/environments/environment.rb` file.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "resend/request"
4
+ require "resend/errors"
5
+
6
+ module Resend
7
+ # broadcasts api wrapper
8
+ module Broadcasts
9
+ class << self
10
+ # https://resend.com/docs/api-reference/broadcasts/create-broadcast
11
+ def create(params = {})
12
+ path = "broadcasts"
13
+ Resend::Request.new(path, params, "post").perform
14
+ end
15
+
16
+ # https://resend.com/docs/api-reference/broadcasts/send-broadcast
17
+ def send(params = {})
18
+ path = "broadcasts/#{params[:broadcast_id]}/send"
19
+ Resend::Request.new(path, params, "post").perform
20
+ end
21
+
22
+ # https://resend.com/docs/api-reference/broadcasts/list-broadcasts
23
+ def list
24
+ path = "broadcasts"
25
+ Resend::Request.new(path, {}, "get").perform
26
+ end
27
+
28
+ # https://resend.com/docs/api-reference/broadcasts/delete-broadcast
29
+ def remove(broadcast_id = "")
30
+ path = "broadcasts/#{broadcast_id}"
31
+ Resend::Request.new(path, {}, "delete").perform
32
+ end
33
+
34
+ # https://resend.com/docs/api-reference/broadcasts/get-broadcast
35
+ def get(broadcast_id = "")
36
+ path = "broadcasts/#{broadcast_id}"
37
+ Resend::Request.new(path, {}, "get").perform
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/resend/client.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "resend/audiences"
3
4
  require "resend/api_keys"
4
- require "resend/domains"
5
- require "resend/emails"
5
+ require "resend/broadcasts"
6
6
  require "resend/batch"
7
- require "resend/audiences"
8
7
  require "resend/contacts"
8
+ require "resend/domains"
9
+ require "resend/emails"
9
10
  require "httparty"
10
11
 
11
12
  module Resend
data/lib/resend/mailer.rb CHANGED
@@ -7,6 +7,17 @@ module Resend
7
7
  class Mailer
8
8
  attr_accessor :config, :settings
9
9
 
10
+ # These are set as `headers` by the Rails API, but these will be filtered out
11
+ # when constructing the Resend API payload, since they're are sent as post params.
12
+ # https://resend.com/docs/api-reference/emails/send-email
13
+ IGNORED_HEADERS = %w[
14
+ cc bcc
15
+ from reply-to to subject mime-version
16
+ html text
17
+ content-type tags scheduled_at
18
+ headers
19
+ ].freeze
20
+
10
21
  def initialize(config)
11
22
  @config = config
12
23
  raise Resend::Error.new("Make sure your API Key is set", @config) unless Resend.api_key
@@ -51,7 +62,19 @@ module Resend
51
62
  end
52
63
 
53
64
  #
54
- # Add custom headers fields
65
+ # Add custom headers fields.
66
+ #
67
+ # Both ways are supported:
68
+ #
69
+ # 1. Through the `#mail()` method ie:
70
+ # mail(headers: { "X-Custom-Header" => "value" })
71
+ #
72
+ # 2. Through the Rails `#headers` method ie:
73
+ # headers["X-Custom-Header"] = "value"
74
+ #
75
+ #
76
+ # setting the header values through the `#mail` method will overwrite values set
77
+ # through the `#headers` method using the same key.
55
78
  #
56
79
  # @param Mail mail Rails Mail object
57
80
  #
@@ -59,7 +82,37 @@ module Resend
59
82
  #
60
83
  def get_headers(mail)
61
84
  params = {}
62
- params[:headers] = mail[:headers].unparsed_value if mail[:headers].present?
85
+
86
+ if mail[:headers].present? || unignored_headers(mail).present?
87
+ params[:headers] = {}
88
+ params[:headers].merge!(headers_values(mail)) if unignored_headers(mail).present?
89
+ params[:headers].merge!(mail_headers_values(mail)) if mail[:headers].present?
90
+ end
91
+
92
+ params
93
+ end
94
+
95
+ # Gets the values of the headers that are set through the `#mail` method
96
+ #
97
+ # @param Mail mail Rails Mail object
98
+ # @return Hash hash with mail headers values
99
+ def mail_headers_values(mail)
100
+ params = {}
101
+ mail[:headers].unparsed_value.each do |k, v|
102
+ params[k.to_s] = v
103
+ end
104
+ params
105
+ end
106
+
107
+ # Gets the values of the headers that are set through the `#headers` method
108
+ #
109
+ # @param Mail mail Rails Mail object
110
+ # @return Hash hash with headers values
111
+ def headers_values(mail)
112
+ params = {}
113
+ unignored_headers(mail).each do |h|
114
+ params[h.name.to_s] = h.unparsed_value
115
+ end
63
116
  params
64
117
  end
65
118
 
@@ -144,5 +197,16 @@ module Resend
144
197
  end
145
198
  attachments
146
199
  end
200
+
201
+ #
202
+ # Get all headers that are not ignored
203
+ #
204
+ # @param Mail mail
205
+ #
206
+ # @return Array headers
207
+ #
208
+ def unignored_headers(mail)
209
+ @unignored_headers ||= mail.header_fields.reject { |h| IGNORED_HEADERS.include?(h.name.downcase) }
210
+ end
147
211
  end
148
212
  end
@@ -13,7 +13,9 @@ module Resend
13
13
  attr_accessor :body, :verb
14
14
 
15
15
  def initialize(path = "", body = {}, verb = "POST")
16
- raise if Resend.api_key.nil?
16
+ raise if (api_key = Resend.api_key).nil?
17
+
18
+ api_key = api_key.call if api_key.is_a?(Proc)
17
19
 
18
20
  @path = path
19
21
  @body = body
@@ -22,7 +24,7 @@ module Resend
22
24
  "Content-Type" => "application/json",
23
25
  "Accept" => "application/json",
24
26
  "User-Agent" => "resend-ruby:#{Resend::VERSION}",
25
- "Authorization" => "Bearer #{Resend.api_key}"
27
+ "Authorization" => "Bearer #{api_key}"
26
28
  }
27
29
  end
28
30
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resend
4
- VERSION = "0.15.0"
4
+ VERSION = "0.17.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derich Pacheco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-15 00:00:00.000000000 Z
11
+ date: 2024-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -49,6 +49,7 @@ files:
49
49
  - lib/resend/api_keys.rb
50
50
  - lib/resend/audiences.rb
51
51
  - lib/resend/batch.rb
52
+ - lib/resend/broadcasts.rb
52
53
  - lib/resend/client.rb
53
54
  - lib/resend/contacts.rb
54
55
  - lib/resend/domains.rb