resend 0.19.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bb6be8da121d99eb4df45bd57b2c241bcd79e73d13834e50a3d8d57d2f51bd2
4
- data.tar.gz: 0fb9c8c8308921308997bc879e1634d811045b4d0dbdd166132c7860640c5c98
3
+ metadata.gz: a211cd93c4479cba4bf63b849bf8e91de3413e9ccf9917c681454fcd7ed832e0
4
+ data.tar.gz: 4e9ba38c842d04fbe3eb7d5987784b54d327ee9b1eaeec8dbfbcebf08110b948
5
5
  SHA512:
6
- metadata.gz: e69c22d3ebf16c1c9b5502b9ab96a3a0669822177bed797de0c759809f5629e8e39fff36c4ecb7f9b3051147c835491fbe28c758472c52b3533161b802a3d8a7
7
- data.tar.gz: 15922ee8535078e181257cb2a8ccb5c04912b13ba6af36f7e75670c1dc8e3dd820453349d3969d163cf39af53a004096dbd0b8515ef0767de5893be07f2bb8ae
6
+ metadata.gz: 63c27d58c685bd8edd04ce28817bbca66ed468113067c0f43f6b51bdf3add9ebcfeeb1d434bf2150f84d5ae4c7661860f834d7dfe62f7c5146bb75051408ae77
7
+ data.tar.gz: 5c63d4c381f0c53dc04b1c8b933b131e9d0bdf7aaf2a4354a9d1c454d75a2116a35fec481865e8668719d0e1fab875688acb6fa2968f3c09a116aeaf1e6500f3
@@ -10,6 +10,12 @@ module Resend
10
10
  Resend::Request.new(path, params, "post").perform
11
11
  end
12
12
 
13
+ #
14
+ # Retrieves a contact from an audience
15
+ #
16
+ # @param audience_id [String] the audience id
17
+ # @param id [String] either the contact id or contact's email
18
+ #
13
19
  # https://resend.com/docs/api-reference/contacts/get-contact
14
20
  def get(audience_id, id)
15
21
  path = "audiences/#{audience_id}/contacts/#{id}"
data/lib/resend/emails.rb CHANGED
@@ -5,10 +5,10 @@ module Resend
5
5
  module Emails
6
6
  class << self
7
7
  # Sends or schedules an email.
8
- # see more: https://resend.com/docs/api-reference/send-email
9
- def send(params)
8
+ # see more: https://resend.com/docs/api-reference/emails/send-email
9
+ def send(params, options: {})
10
10
  path = "emails"
11
- Resend::Request.new(path, params, "post").perform
11
+ Resend::Request.new(path, params, "post", options: options).perform
12
12
  end
13
13
 
14
14
  # Retrieve a single email.
data/lib/resend/mailer.rb CHANGED
@@ -15,9 +15,11 @@ module Resend
15
15
  from reply-to to subject mime-version
16
16
  html text
17
17
  content-type tags scheduled_at
18
- headers
18
+ headers options
19
19
  ].freeze
20
20
 
21
+ SUPPORTED_OPTIONS = %w[idempotency_key].freeze
22
+
21
23
  def initialize(config)
22
24
  @config = config
23
25
  raise Resend::Error.new("Make sure your API Key is set", @config) unless Resend.api_key
@@ -35,7 +37,8 @@ module Resend
35
37
  #
36
38
  def deliver!(mail)
37
39
  params = build_resend_params(mail)
38
- resp = Resend::Emails.send(params)
40
+ options = get_options(mail) if mail[:options].present?
41
+ resp = Resend::Emails.send(params, options: options || {})
39
42
  mail.message_id = resp[:id] if resp[:error].nil?
40
43
  resp
41
44
  end
@@ -92,6 +95,22 @@ module Resend
92
95
  params
93
96
  end
94
97
 
98
+ #
99
+ # Adds additional options fields.
100
+ # Currently supports only :idempotency_key
101
+ #
102
+ # @param Mail mail Rails Mail object
103
+ # @return Hash hash with headers param
104
+ #
105
+ def get_options(mail)
106
+ opts = {}
107
+ if mail[:options].present?
108
+ opts.merge!(mail[:options].unparsed_value)
109
+ opts.delete_if { |k, _v| !SUPPORTED_OPTIONS.include?(k.to_s) }
110
+ end
111
+ opts
112
+ end
113
+
95
114
  # Remove nils from header values
96
115
  def cleanup_headers(headers)
97
116
  headers.delete_if { |_k, v| v.nil? }
@@ -6,9 +6,9 @@ module Resend
6
6
  class Request
7
7
  BASE_URL = ENV["RESEND_BASE_URL"] || "https://api.resend.com/"
8
8
 
9
- attr_accessor :body, :verb
9
+ attr_accessor :body, :verb, :options
10
10
 
11
- def initialize(path = "", body = {}, verb = "POST")
11
+ def initialize(path = "", body = {}, verb = "POST", options: {})
12
12
  raise if (api_key = Resend.api_key).nil?
13
13
 
14
14
  api_key = api_key.call if api_key.is_a?(Proc)
@@ -16,12 +16,15 @@ module Resend
16
16
  @path = path
17
17
  @body = body
18
18
  @verb = verb
19
+ @options = options
19
20
  @headers = {
20
21
  "Content-Type" => "application/json",
21
22
  "Accept" => "application/json",
22
23
  "User-Agent" => "resend-ruby:#{Resend::VERSION}",
23
24
  "Authorization" => "Bearer #{api_key}"
24
25
  }
26
+
27
+ set_idempotency_key
25
28
  end
26
29
 
27
30
  # Performs the HTTP call
@@ -29,7 +32,6 @@ module Resend
29
32
  options = {
30
33
  headers: @headers
31
34
  }
32
-
33
35
  options[:body] = @body.to_json unless @body.empty?
34
36
 
35
37
  resp = HTTParty.send(@verb.to_sym, "#{BASE_URL}#{@path}", options)
@@ -55,6 +57,15 @@ module Resend
55
57
 
56
58
  private
57
59
 
60
+ def set_idempotency_key
61
+ # Only set idempotency key if the verb is POST for now.
62
+ #
63
+ # Does not set it if the idempotency_key is nil or empty
64
+ if @verb.downcase == "post" && (!@options[:idempotency_key].nil? && !@options[:idempotency_key].empty?)
65
+ @headers["Idempotency-Key"] = @options[:idempotency_key]
66
+ end
67
+ end
68
+
58
69
  def check_json!(resp)
59
70
  if resp.body.is_a?(Hash)
60
71
  JSON.parse(resp.body.to_json)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resend
4
- VERSION = "0.19.0"
4
+ VERSION = "0.21.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.19.0
4
+ version: 0.21.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: 2025-03-25 00:00:00.000000000 Z
11
+ date: 2025-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty