resend 0.3.0 → 0.4.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: e3e46d09490b1ad19c59f04e55fd16b0da3d3c2573ed5c9dcba3297f0f92f5fc
4
+ data.tar.gz: 6c9dfb7910fd6199bc9c75aa9f3c2e9200677ce9e2ba1e9a29e124908489e714
5
5
  SHA512:
6
- metadata.gz: bb127ddf8f4cfc28793690add92c1b38d84c25d54b20cbe00344dc4a72dcdeb1823eaa765a576c1ef68dd50ac27b8cb15da9d7349b71026b8522e2dc01c3c602
7
- data.tar.gz: c5bde3db647e5fb1c7c819842a25420f4e296d9a9735e37c6415e3958e93331cc31a577e2e0fd40a7294b08c368522dadadffde1ac1d0769e6a686a9b2afdbbd
6
+ metadata.gz: 58e0cdd7ae1d48654ff33761b1fcb8a4e96061142d154232f85bac348c38956505d11ee3a0cdf6d86592389d2d8c69eba48941d7d97e1c4de904d0c8b5b6d7a6
7
+ data.tar.gz: 2069dfd99a67245da05ebf8e58658c2771de51cd6999d8d824116174b0aeba38dcc4f4423f05cb73e63388f5497377dba4c4b98eebddba2b2205864530790d3a
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
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Resend
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.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.4.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-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty