courrier 1.1.0 → 1.2.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: 802112a2d410713a25a67fca11af1a9021cf8f5bd15efe55590bb6d41dcf188b
4
- data.tar.gz: ac7fd7ab96871921876c79e20a623baa5d7a8fd9ab5ad13b23e3ae9e2631cc57
3
+ metadata.gz: 60ea7fcf4323b5727c0f3bac3fce9f42aee41b767bd0a1726e0f4efd2db1634b
4
+ data.tar.gz: 7c94cbcaf930feb9e5e2b8f63034fcba6a10a9c53c8afb5a6f281861a6048bf5
5
5
  SHA512:
6
- metadata.gz: 350843ff6b853bb92e797d87dd5b485ad095d3f8a70a2129863ea141e11dcf6f0fd90c8a680bf5f731ce38690e0bc0f47dba60ca61cb3ab9089d7b8d524bfaf8
7
- data.tar.gz: 222f3088acd972aa5f282e4a29d5f7a2035b2373c10804ff97db89651cb09ead6938117734144c182b2b7268ff64285833674782b38d966145635818a49d2a14
6
+ metadata.gz: c74c7c8c95c5c587739b9f3915d12ffa2f34983a6592f9ff1441129c2ac10e97852df67cae2e6546e12dcab4ab0e3410edff2a9be1917bafd36c28f4c3977460
7
+ data.tar.gz: 84a1d6a2f6fa149f96f6f957c6870df6d66a8c964836bfa849c4affc6abb39163f50bb47f2cc76516c98359e38083740890f20bb4c60bc5a59f7c4155eaba247
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- courrier (1.1.0)
4
+ courrier (1.2.0)
5
5
  logger (>= 1.5, < 3)
6
6
  nokogiri (>= 1.18, < 2)
7
7
 
@@ -22,10 +22,10 @@ module Courrier
22
22
  name
23
23
  end
24
24
 
25
- def quote_escaped(name) = %("#{name.gsub('"', '\\"')}")
25
+ def quote_escaped(name) = %("#{name.gsub(/["\\]/) { |character| "\\#{character}" }}")
26
26
 
27
27
  def special_characters_in?(name)
28
- name =~ /[(),.:;<>@\[\]"]/
28
+ name =~ /[(),.:;<>@\[\]"\\]/
29
29
  end
30
30
  end
31
31
 
@@ -20,13 +20,13 @@ module Courrier
20
20
  def no_layouts? = @email.class.layouts.nil?
21
21
 
22
22
  def layouts
23
- FORMATS.map(&:to_sym).to_h do |format|
23
+ FORMATS.map(&:to_sym).filter_map do |format|
24
24
  template = @email.class.layouts[format]
25
25
 
26
26
  next if template.nil?
27
27
 
28
28
  [format, render(template)]
29
- end
29
+ end.to_h
30
30
  end
31
31
 
32
32
  def render(template)
@@ -56,7 +56,7 @@ module Courrier
56
56
 
57
57
  next wrapped if !layout
58
58
 
59
- layout % {content: wrapped}
59
+ layout.gsub("%{content}") { wrapped }
60
60
  end
61
61
  end
62
62
 
@@ -41,22 +41,41 @@ module Courrier
41
41
  def default_headers = {}
42
42
 
43
43
  def address_list(value, as: :email)
44
- list = value&.to_s&.split(",")&.map(&:strip)&.reject(&:empty?)
45
- return unless list && !list.empty?
44
+ list = split_addresses(value).map(&:strip).reject(&:empty?)
45
+ return if list.empty?
46
46
 
47
47
  list.map { |address| address_element(address, as) }
48
48
  end
49
49
 
50
50
  def address_line(value) = address_list(value, as: :plain)&.join(", ")
51
51
 
52
+ def split_addresses(value)
53
+ value.to_s.scan(/(?:"(?:\\.|[^"\\])*"|[^,])+/)
54
+ end
55
+
52
56
  def address_element(address, as)
53
57
  case as
54
- when :email then {"email" => address}
58
+ when :email then email_and_name(address)
55
59
  when :address then {"address" => address}
56
60
  else address
57
61
  end
58
62
  end
59
63
 
64
+ def email_and_name(address, email_key: "email", name_key: "name")
65
+ match = address.match(/\A(?<name>.*?)\s*<(?<email>[^<>]+)>\z/m)
66
+ return {email_key => address} unless match
67
+
68
+ name = unquote(match[:name].strip)
69
+
70
+ {email_key => match[:email].strip, name_key => (name unless name.empty?)}.compact
71
+ end
72
+
73
+ def unquote(name)
74
+ return name unless name.length >= 2 && name.start_with?('"') && name.end_with?('"')
75
+
76
+ name[1..-2].gsub(/\\(.)/m, '\1')
77
+ end
78
+
60
79
  def provider = self.class.name.split("::").last
61
80
  end
62
81
  end
@@ -8,11 +8,11 @@ module Courrier
8
8
 
9
9
  def body
10
10
  {
11
- "from" => @options.from,
12
- "to" => @options.to,
13
- "reply_to" => @options.reply_to,
14
- "cc" => @options.cc,
15
- "bcc" => @options.bcc,
11
+ "from" => recipients(@options.from)&.first,
12
+ "to" => recipients(@options.to),
13
+ "reply_to" => recipients(@options.reply_to)&.first,
14
+ "cc" => bare_addresses(@options.cc),
15
+ "bcc" => bare_addresses(@options.bcc),
16
16
  "subject" => @options.subject,
17
17
  "text" => @options.text,
18
18
  "html" => @options.html
@@ -21,6 +21,13 @@ module Courrier
21
21
 
22
22
  private
23
23
 
24
+ # The API documents cc/bcc as plain address strings (named objects only for from/to)
25
+ def bare_addresses(value) = recipients(value)&.map { |recipient| recipient["address"] }
26
+
27
+ def recipients(value)
28
+ address_list(value, as: :plain)&.map { |address| email_and_name(address, email_key: "address") }
29
+ end
30
+
24
31
  def endpoint_url
25
32
  account_id = @provider_options.account_id
26
33
  raise Courrier::ArgumentError, "Cloudflare requires an `account_id`" unless account_id
@@ -12,10 +12,10 @@ module Courrier
12
12
  {
13
13
  "route" => @provider_options.route,
14
14
  "from" => @options.from,
15
- "to" => @options.to.to_s.split(",").map(&:strip),
16
- "cc" => @options.cc&.split(",")&.map(&:strip),
17
- "bcc" => @options.bcc&.split(",")&.map(&:strip),
18
- "reply_to" => @options.reply_to&.split(",")&.map(&:strip),
15
+ "to" => address_list(@options.to, as: :plain),
16
+ "cc" => address_list(@options.cc, as: :plain),
17
+ "bcc" => address_list(@options.bcc, as: :plain),
18
+ "reply_to" => address_list(@options.reply_to, as: :plain),
19
19
  "subject" => @options.subject,
20
20
  "html" => @options.html,
21
21
  "text" => @options.text
@@ -12,14 +12,12 @@ module Courrier
12
12
  {
13
13
  "Messages" => [
14
14
  {
15
- "From" => {
16
- "Email" => @options.from
17
- },
15
+ "From" => addresses(@options.from)&.first,
18
16
 
19
17
  "To" => addresses(@options.to),
20
18
  "Cc" => addresses(@options.cc),
21
19
  "Bcc" => addresses(@options.bcc),
22
- "ReplyTo" => reply_to_object,
20
+ "ReplyTo" => addresses(@options.reply_to)&.first,
23
21
 
24
22
  "Subject" => @options.subject,
25
23
  "TextPart" => @options.text,
@@ -38,15 +36,7 @@ module Courrier
38
36
  end
39
37
 
40
38
  def addresses(value)
41
- address_list(value, as: :plain)&.map { |address| {"Email" => address} }
42
- end
43
-
44
- def reply_to_object
45
- return if @options.reply_to.nil?
46
-
47
- {
48
- "Email" => @options.reply_to
49
- }
39
+ address_list(value, as: :plain)&.map { |address| email_and_name(address, email_key: "Email", name_key: "Name") }
50
40
  end
51
41
  end
52
42
  end
@@ -8,9 +8,7 @@ module Courrier
8
8
 
9
9
  def body
10
10
  {
11
- "from" => {
12
- "email" => @options.from
13
- },
11
+ "from" => address_list(@options.from)&.first,
14
12
  "personalizations" => [
15
13
  {
16
14
  "to" => address_list(@options.to),
@@ -18,7 +16,7 @@ module Courrier
18
16
  "bcc" => address_list(@options.bcc)
19
17
  }.compact
20
18
  ],
21
- "reply_to" => reply_to_object,
19
+ "reply_to" => address_list(@options.reply_to)&.first,
22
20
 
23
21
  "subject" => @options.subject,
24
22
  "content" => [
@@ -41,14 +39,6 @@ module Courrier
41
39
  "Authorization" => "Bearer #{@api_key}"
42
40
  }
43
41
  end
44
-
45
- def reply_to_object
46
- return if @options.reply_to.nil?
47
-
48
- {
49
- "email" => @options.reply_to
50
- }
51
- end
52
42
  end
53
43
  end
54
44
  end
@@ -28,12 +28,12 @@ module Courrier
28
28
  {
29
29
  "FromEmailAddress" => @options.from,
30
30
  "Destination" => {
31
- "ToAddresses" => Array(@options.to),
32
- "CcAddresses" => @options.cc ? Array(@options.cc) : nil,
33
- "BccAddresses" => @options.bcc ? Array(@options.bcc) : nil
31
+ "ToAddresses" => address_list(@options.to, as: :plain),
32
+ "CcAddresses" => address_list(@options.cc, as: :plain),
33
+ "BccAddresses" => address_list(@options.bcc, as: :plain)
34
34
  }.compact,
35
35
 
36
- "ReplyToAddresses" => @options.reply_to ? Array(@options.reply_to) : nil,
36
+ "ReplyToAddresses" => address_list(@options.reply_to, as: :plain),
37
37
  "Content" => {
38
38
  "Simple" => {
39
39
  "Subject" => {"Data" => @options.subject},
@@ -9,17 +9,24 @@ module Courrier
9
9
  def body
10
10
  {
11
11
  "sender" => @options.from,
12
- "to" => @options.to.to_s.split(",").map(&:strip),
13
- "cc" => @options.cc&.split(",")&.map(&:strip),
14
- "bcc" => @options.bcc&.split(",")&.map(&:strip),
12
+ "to" => address_list(@options.to, as: :plain),
13
+ "cc" => address_list(@options.cc, as: :plain),
14
+ "bcc" => address_list(@options.bcc, as: :plain),
15
15
  "subject" => @options.subject,
16
16
  "html_body" => @options.html,
17
- "text_body" => @options.text
17
+ "text_body" => @options.text,
18
+ "custom_headers" => reply_to_header
18
19
  }.compact
19
20
  end
20
21
 
21
22
  private
22
23
 
24
+ def reply_to_header
25
+ return unless (reply_to = address_line(@options.reply_to))
26
+
27
+ [{"header" => "Reply-To", "value" => reply_to}]
28
+ end
29
+
23
30
  def default_headers
24
31
  {"X-Smtp2go-Api-Key" => @api_key}
25
32
  end
@@ -19,7 +19,7 @@ module Courrier
19
19
  private
20
20
 
21
21
  def parse_body(body)
22
- return {} if @response.nil?
22
+ return {} if body.nil? || body.empty?
23
23
 
24
24
  begin
25
25
  JSON.parse(body)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "uri"
3
4
  require "courrier/subscriber/base"
4
5
 
5
6
  module Courrier
@@ -27,7 +28,10 @@ module Courrier
27
28
  private
28
29
 
29
30
  def subscription_id(publication_id, email)
30
- response = request(:get, "#{ENDPOINT_URL}/#{publication_id}/subscriptions?email=#{email}")
31
+ response = request(
32
+ :get,
33
+ "#{ENDPOINT_URL}/#{publication_id}/subscriptions?email=#{URI.encode_www_form_component(email)}"
34
+ )
31
35
 
32
36
  return nil unless response.success?
33
37
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "uri"
3
4
  require "courrier/subscriber/base"
4
5
 
5
6
  module Courrier
@@ -12,7 +13,7 @@ module Courrier
12
13
  end
13
14
 
14
15
  def destroy(email)
15
- request(:delete, "#{ENDPOINT_URL}/#{email}")
16
+ request(:delete, "#{ENDPOINT_URL}/#{URI.encode_www_form_component(email)}")
16
17
  end
17
18
 
18
19
  private
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "digest"
3
4
  require "courrier/subscriber/base"
4
5
 
5
6
  module Courrier
@@ -23,11 +24,13 @@ module Courrier
23
24
 
24
25
  raise Courrier::ConfigurationError, "Mailchimp requires `dc` and `list_id` in subscriber configuration" unless dc && list_id
25
26
 
26
- request(:delete, "https://#{dc}.api.mailchimp.com/3.0/lists/#{list_id}/members/#{email}")
27
+ request(:delete, "https://#{dc}.api.mailchimp.com/3.0/lists/#{list_id}/members/#{subscriber_hash(email)}")
27
28
  end
28
29
 
29
30
  private
30
31
 
32
+ def subscriber_hash(email) = Digest::MD5.hexdigest(email.downcase)
33
+
31
34
  def headers
32
35
  {
33
36
  "Authorization" => "Bearer #{@api_key}",
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "uri"
3
4
  require "courrier/subscriber/base"
4
5
 
5
6
  module Courrier
@@ -12,7 +13,7 @@ module Courrier
12
13
  end
13
14
 
14
15
  def destroy(email)
15
- request(:delete, "#{ENDPOINT_URL}/#{email}")
16
+ request(:delete, "#{ENDPOINT_URL}/#{URI.encode_www_form_component(email)}")
16
17
  end
17
18
 
18
19
  private
@@ -17,7 +17,7 @@ module Courrier
17
17
  private
18
18
 
19
19
  def parsed(body)
20
- return {} if @response.nil?
20
+ return {} if body.nil? || body.empty?
21
21
 
22
22
  begin
23
23
  JSON.parse(body)
@@ -1,3 +1,3 @@
1
1
  module Courrier
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: courrier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rails Designer
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.6.7
137
+ rubygems_version: 3.6.9
138
138
  specification_version: 4
139
139
  summary: API-powered email delivery for Ruby apps
140
140
  test_files: []