Skip to content

Commit 230fdf5

Browse files
committed
Rework client and mail. Add response class
1 parent 0d4978e commit 230fdf5

4 files changed

Lines changed: 56 additions & 44 deletions

File tree

lib/sendgrid/client.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
module SendGrid
44
class Client
5-
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint, :user_agent
5+
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint,
6+
:user_agent
67
attr_writer :adapter, :conn
78

89
def initialize(params = {})
@@ -11,38 +12,42 @@ def initialize(params = {})
1112
self.protocol = params.fetch(:protocol, 'https')
1213
self.host = params.fetch(:host, 'api.sendgrid.com')
1314
self.port = params.fetch(:port, nil)
14-
self.url = params.fetch(:url, self.protocol + '://' + self.host + (self.port ? ":#{self.port}" : ''))
15+
self.url = params.fetch(:url, protocol + '://' + host + (port ? ":#{port}" : ''))
1516
self.endpoint = params.fetch(:endpoint, '/api/mail.send.json')
16-
self.adapter = params.fetch(:adapter, self.adapter)
17-
self.conn = params.fetch(:conn, self.conn)
17+
self.adapter = params.fetch(:adapter, adapter)
18+
self.conn = params.fetch(:conn, conn)
1819
self.user_agent = params.fetch(:user_agent, "sendgrid/#{SendGrid::VERSION};ruby")
1920
yield self if block_given?
2021
end
2122

2223
def send(mail)
23-
res = self.conn.post do |req|
24+
res = conn.post do |req|
2425
payload = mail.to_h
2526

26-
req.url(self.endpoint)
27+
req.url(endpoint)
2728

2829
# Check if using username + password or API key
29-
if self.api_user
30+
if api_user
3031
# Username + password
31-
payload.merge({api_user: self.api_user, api_key: self.api_key})
32+
payload = payload.merge(api_user: api_user, api_key: api_key)
3233
else
3334
# API key
34-
req.headers['Authorization'] = "Bearer #{self.api_key}"
35+
req.headers['Authorization'] = "Bearer #{api_key}"
3536
end
3637

3738
req.body = payload
3839
end
40+
41+
fail SendGrid::Exception, res.body if res.status != 200
42+
43+
SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body)
3944
end
4045

4146
def conn
42-
@conn ||= Faraday.new(url: self.url) do |conn|
47+
@conn ||= Faraday.new(url: url) do |conn|
4348
conn.request :multipart
4449
conn.request :url_encoded
45-
conn.adapter self.adapter
50+
conn.adapter adapter
4651
end
4752
end
4853

lib/sendgrid/mail.rb

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
module SendGrid
55
class Mail
6-
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
7-
:bcc, :reply_to, :date, :smtpapi, :attachments
6+
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
7+
:bcc, :reply_to, :date, :smtpapi, :attachments
88

99
def initialize(params = {})
1010
params.each do |k, v|
@@ -91,7 +91,7 @@ def add_bcc_name(name)
9191
def add_attachment(path, name = nil)
9292
file = File.new(path)
9393
name ||= File.basename(file)
94-
@attachments << {file: file, name: name}
94+
attachments << {file: file, name: name}
9595
end
9696

9797
def headers
@@ -108,39 +108,31 @@ def smtpapi
108108

109109
def to_h
110110
payload = {
111-
:from => from,
112-
:fromname => from_name,
113-
:subject => subject,
114-
:to => to,
115-
:toname => to_name,
116-
:date => date,
117-
:replyto => reply_to,
118-
:cc => cc,
119-
:bcc => bcc,
120-
:text => text,
121-
:html => html,
122-
:'x-smtpapi' => smtpapi.to_json,
123-
:files => ({} unless attachments.empty?)
124-
}.reject {|k,v| v.nil?}
125-
126-
# smtpapi bandaid
127-
if to.nil? && !smtpapi.to.empty?
128-
payload[:to] = payload[:from]
129-
end
130-
131-
payload[:to] = payload[:from] if no_adressee?
132-
133-
return if @attachments.empty?
134-
@attachments.each do |file|
111+
from: from,
112+
fromname: from_name,
113+
subject: subject,
114+
to: to,
115+
toname: to_name,
116+
date: date,
117+
replyto: reply_to,
118+
cc: cc,
119+
bcc: bcc,
120+
text: text,
121+
html: html,
122+
'x-smtpapi': smtpapi.to_json,
123+
files: ({} unless attachments.empty?)
124+
}.reject {|_, v| v.nil? || v.empty?}
125+
126+
payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'
127+
128+
payload[:to] = payload[:from] unless payload[:to].nil? && smtpapi.to.empty?
129+
130+
return payload if attachments.empty?
131+
132+
attachments.each do |file|
135133
payload[:files][file[:name]] = file[:file]
136134
end
137135
payload
138136
end
139-
140-
141-
private
142-
def smtpapi_bandaid
143-
144-
end
145137
end
146138
end

lib/sendgrid/response.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'json'
2+
3+
module SendGrid
4+
class Response
5+
attr_reader :code, :headers, :body, :raw_body
6+
7+
def initialize(params)
8+
@code = params[:code]
9+
@headers = params[:headers]
10+
@body = JSON.parse(params[:body])
11+
@raw_body = params[:body]
12+
end
13+
end
14+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative 'sendgrid/client'
22
require_relative 'sendgrid/exceptions'
33
require_relative 'sendgrid/mail'
4+
require_relative 'sendgrid/response'
45
require_relative 'sendgrid/version'

0 commit comments

Comments
 (0)