Skip to content

Commit

Permalink
Rework client and mail. Add response class
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiezane committed May 25, 2015
1 parent 0d4978e commit 230fdf5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
27 changes: 16 additions & 11 deletions lib/sendgrid/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module SendGrid
class Client
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint, :user_agent
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint,
:user_agent
attr_writer :adapter, :conn

def initialize(params = {})
Expand All @@ -11,38 +12,42 @@ def initialize(params = {})
self.protocol = params.fetch(:protocol, 'https')
self.host = params.fetch(:host, 'api.sendgrid.com')
self.port = params.fetch(:port, nil)
self.url = params.fetch(:url, self.protocol + '://' + self.host + (self.port ? ":#{self.port}" : ''))
self.url = params.fetch(:url, protocol + '://' + host + (port ? ":#{port}" : ''))
self.endpoint = params.fetch(:endpoint, '/api/mail.send.json')
self.adapter = params.fetch(:adapter, self.adapter)
self.conn = params.fetch(:conn, self.conn)
self.adapter = params.fetch(:adapter, adapter)
self.conn = params.fetch(:conn, conn)
self.user_agent = params.fetch(:user_agent, "sendgrid/#{SendGrid::VERSION};ruby")
yield self if block_given?
end

def send(mail)
res = self.conn.post do |req|
res = conn.post do |req|
payload = mail.to_h

req.url(self.endpoint)
req.url(endpoint)

# Check if using username + password or API key
if self.api_user
if api_user
# Username + password
payload.merge({api_user: self.api_user, api_key: self.api_key})
payload = payload.merge(api_user: api_user, api_key: api_key)
else
# API key
req.headers['Authorization'] = "Bearer #{self.api_key}"
req.headers['Authorization'] = "Bearer #{api_key}"
end

req.body = payload
end

fail SendGrid::Exception, res.body if res.status != 200

SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body)
end

def conn
@conn ||= Faraday.new(url: self.url) do |conn|
@conn ||= Faraday.new(url: url) do |conn|
conn.request :multipart
conn.request :url_encoded
conn.adapter self.adapter
conn.adapter adapter
end
end

Expand Down
58 changes: 25 additions & 33 deletions lib/sendgrid/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

module SendGrid
class Mail
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
:bcc, :reply_to, :date, :smtpapi, :attachments
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
:bcc, :reply_to, :date, :smtpapi, :attachments

def initialize(params = {})
params.each do |k, v|
Expand Down Expand Up @@ -91,7 +91,7 @@ def add_bcc_name(name)
def add_attachment(path, name = nil)
file = File.new(path)
name ||= File.basename(file)
@attachments << {file: file, name: name}
attachments << {file: file, name: name}
end

def headers
Expand All @@ -108,39 +108,31 @@ def smtpapi

def to_h
payload = {
:from => from,
:fromname => from_name,
:subject => subject,
:to => to,
:toname => to_name,
:date => date,
:replyto => reply_to,
:cc => cc,
:bcc => bcc,
:text => text,
:html => html,
:'x-smtpapi' => smtpapi.to_json,
:files => ({} unless attachments.empty?)
}.reject {|k,v| v.nil?}

# smtpapi bandaid
if to.nil? && !smtpapi.to.empty?
payload[:to] = payload[:from]
end

payload[:to] = payload[:from] if no_adressee?

return if @attachments.empty?
@attachments.each do |file|
from: from,
fromname: from_name,
subject: subject,
to: to,
toname: to_name,
date: date,
replyto: reply_to,
cc: cc,
bcc: bcc,
text: text,
html: html,
'x-smtpapi': smtpapi.to_json,
files: ({} unless attachments.empty?)
}.reject {|_, v| v.nil? || v.empty?}

payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'

payload[:to] = payload[:from] unless payload[:to].nil? && smtpapi.to.empty?

return payload if attachments.empty?

attachments.each do |file|
payload[:files][file[:name]] = file[:file]
end
payload
end


private
def smtpapi_bandaid

end
end
end
14 changes: 14 additions & 0 deletions lib/sendgrid/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'json'

module SendGrid
class Response
attr_reader :code, :headers, :body, :raw_body

def initialize(params)
@code = params[:code]
@headers = params[:headers]
@body = JSON.parse(params[:body])
@raw_body = params[:body]
end
end
end
1 change: 1 addition & 0 deletions lib/sendgrid-ruby.rb → lib/sendgrid_ruby.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'sendgrid/client'
require_relative 'sendgrid/exceptions'
require_relative 'sendgrid/mail'
require_relative 'sendgrid/response'
require_relative 'sendgrid/version'

1 comment on commit 230fdf5

@djamison
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 128: Should that unless be if?

As written, if I set mail.to, then line 128 changes mail.to to be mail.from.

Please sign in to comment.