resend 0.3.0 → 0.5.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: fdc5399f47d9b5faaaa0a1406099a416bfafd29d9e07557ff8496617f1f35091
4
- data.tar.gz: 5cfe523a3a1f8788551811de440a6e179c40802cbc6a1879180e022e8e9fc38e
3
+ metadata.gz: d3e4d80d6041ac5d061ec2eafce26da90a106d14be6ae2b23dc66741b29a7d55
4
+ data.tar.gz: 3d486cd3522bda376cc61ee41c0ef1bdc91b6af2db07b4f05530d01d5a74f616
5
5
  SHA512:
6
- metadata.gz: bb127ddf8f4cfc28793690add92c1b38d84c25d54b20cbe00344dc4a72dcdeb1823eaa765a576c1ef68dd50ac27b8cb15da9d7349b71026b8522e2dc01c3c602
7
- data.tar.gz: c5bde3db647e5fb1c7c819842a25420f4e296d9a9735e37c6415e3958e93331cc31a577e2e0fd40a7294b08c368522dadadffde1ac1d0769e6a686a9b2afdbbd
6
+ metadata.gz: 548f3a9603ecba1a0d44f7846d08e2194bd29d5baae28c6df8bdd3c1d5f3827210dacddad009556ff4d33c9eb1c567be60d522f3eda6f240fe7b1a3e0b3e6e49
7
+ data.tar.gz: a775247f3a2c7ab2a17e277d30f1b44700a5b9a17082de247aeff0a781e17c728bd67aa9bf1ad7e7807d78f43be43bd717c6c77ed464e333540a70efb325afd8
data/README.md CHANGED
@@ -16,7 +16,7 @@ gem install resend
16
16
 
17
17
  Via Gemfile:
18
18
  ```
19
- gem 'resend', '~>0.2.1'
19
+ gem 'resend', '~>0.3.0'
20
20
  ```
21
21
 
22
22
  ## Setup
@@ -9,19 +9,19 @@ module Resend
9
9
  class << self
10
10
  # https://resend.com/docs/api-reference/api-keys/create-api-key
11
11
  def create(params)
12
- path = "/api-keys"
12
+ path = "api-keys"
13
13
  Resend::Request.new(path, params, "post").perform
14
14
  end
15
15
 
16
16
  # https://resend.com/docs/api-reference/api-keys/list-api-keys
17
17
  def list
18
- path = "/api-keys"
18
+ path = "api-keys"
19
19
  Resend::Request.new(path, {}, "get").perform
20
20
  end
21
21
 
22
22
  # https://resend.com/docs/api-reference/api-keys/delete-api-key
23
23
  def remove(api_key_id = "")
24
- path = "/api-keys/#{api_key_id}"
24
+ path = "api-keys/#{api_key_id}"
25
25
  Resend::Request.new(path, {}, "delete").perform
26
26
  end
27
27
  end
@@ -9,25 +9,31 @@ module Resend
9
9
  class << self
10
10
  # https://resend.com/docs/api-reference/domains/create-domain
11
11
  def create(params)
12
- path = "/domains"
12
+ path = "domains"
13
13
  Resend::Request.new(path, params, "post").perform
14
14
  end
15
15
 
16
+ # https://resend.com/docs/api-reference/domains/get-domain
17
+ def get(domain_id = "")
18
+ path = "domains/#{domain_id}"
19
+ Resend::Request.new(path, {}, "get").perform
20
+ end
21
+
16
22
  # https://resend.com/docs/api-reference/api-keys/list-api-keys
17
23
  def list
18
- path = "/domains"
24
+ path = "domains"
19
25
  Resend::Request.new(path, {}, "get").perform
20
26
  end
21
27
 
22
28
  # https://resend.com/docs/api-reference/domains/delete-domain
23
29
  def remove(domain_id = "")
24
- path = "/domains/#{domain_id}"
30
+ path = "domains/#{domain_id}"
25
31
  Resend::Request.new(path, {}, "delete").perform
26
32
  end
27
33
 
28
34
  # https://resend.com/docs/api-reference/domains/verify-domain
29
35
  def verify(domain_id = "")
30
- path = "/domains/#{domain_id}/verify"
36
+ path = "domains/#{domain_id}/verify"
31
37
  Resend::Request.new(path, {}, "post").perform
32
38
  end
33
39
  end
data/lib/resend/errors.rb CHANGED
@@ -21,6 +21,7 @@ module Resend
21
21
  NotFoundError = Class.new(ServerError)
22
22
 
23
23
  ERRORS = {
24
+ 401 => Resend::Error::InvalidRequestError,
24
25
  404 => Resend::Error::InvalidRequestError,
25
26
  422 => Resend::Error::InvalidRequestError,
26
27
  400 => Resend::Error::InvalidRequestError,
data/lib/resend/mailer.rb CHANGED
@@ -12,31 +12,65 @@ module Resend
12
12
  raise Resend::ResendError.new("Config requires api_key", @config) unless @config.key?(:api_key)
13
13
 
14
14
  @settings = { return_response: true } # avoids NilError exception
15
- @resend_client = Resend::Client.new config[:api_key]
16
15
  end
17
16
 
18
17
  def deliver!(mail)
19
18
  params = build_resend_params(mail)
20
- resp = @resend_client.send_email(params)
19
+ resp = Resend::Emails.send(params)
21
20
  mail.message_id = resp[:id] if resp[:error].nil?
22
21
  resp
23
22
  end
24
23
 
25
- # rubocop:disable Metrics/AbcSize
26
24
  def build_resend_params(mail)
27
25
  params = {
28
- from: mail[:from].to_s,
26
+ from: get_from(mail.from),
29
27
  to: mail.to,
30
28
  subject: mail.subject
31
29
  }
32
- params[:cc] = mail[:cc].to_s if mail[:cc].present?
33
- params[:bcc] = mail[:bcc].to_s if mail[:bcc].present?
34
- params[:reply_to] = mail[:reply_to].to_s if mail[:reply_to].present?
35
- params[:html] = mail.body.decoded
30
+ params.merge!(get_addons(mail))
31
+ params[:attachments] = get_attachments(mail) if mail.attachments.present?
32
+ params.merge!(get_contents(mail))
36
33
  params
37
34
  end
38
- # rubocop:enable Metrics/AbcSize
39
35
 
40
- attr_reader :resend_client
36
+ def get_addons(mail)
37
+ params = {}
38
+ params[:cc] = mail.cc if mail.cc.present?
39
+ params[:bcc] = mail.bcc if mail.bcc.present?
40
+ params[:reply_to] = mail.reply_to if mail.reply_to.present?
41
+ params
42
+ end
43
+
44
+ def get_contents(mail)
45
+ params = {}
46
+ case mail.mime_type
47
+ when "text/plain"
48
+ params[:text] = mail.body.decoded
49
+ when "text/html"
50
+ params[:html] = mail.body.decoded
51
+ when "multipart/alternative", "multipart/mixed", "multipart/related"
52
+ params[:text] = mail.text_part.decoded if mail.text_part
53
+ params[:html] = mail.html_part.decoded if mail.html_part
54
+ end
55
+ params
56
+ end
57
+
58
+ def get_from(input)
59
+ return input.first if input.is_a? Array
60
+
61
+ input
62
+ end
63
+
64
+ def get_attachments(mail)
65
+ attachments = []
66
+ mail.attachments.each do |part|
67
+ attachment = {
68
+ filename: part.filename,
69
+ content: part.body.decoded.bytes
70
+ }
71
+ attachments.append(attachment)
72
+ end
73
+ attachments
74
+ end
41
75
  end
42
76
  end
@@ -31,10 +31,11 @@ module Resend
31
31
  options = {
32
32
  headers: @headers
33
33
  }
34
+
34
35
  options[:body] = @body.to_json unless @body.empty?
35
36
  resp = HTTParty.send(@verb.to_sym, "#{BASE_URL}#{@path}", options)
36
37
  resp.transform_keys!(&:to_sym) unless resp.body.empty?
37
- handle_error!(resp) if resp[:statusCode] && resp[:statusCode] != 200
38
+ handle_error!(resp) if resp[:statusCode] && (resp[:statusCode] != 200 || resp[:statusCode] != 201)
38
39
  resp
39
40
  end
40
41
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resend
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.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.3.0
4
+ version: 0.5.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: 2023-04-30 00:00:00.000000000 Z
11
+ date: 2023-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty