Class: SendGrid::Client
- Inherits:
-
Object
- Object
- SendGrid::Client
- Defined in:
- lib/sendgrid/client.rb
Instance Attribute Summary collapse
- #adapter ⇒ Object
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_user ⇒ Object
Returns the value of attribute api_user.
- #conn ⇒ Object
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#raise_exceptions ⇒ Object
writeonly
Sets the attribute raise_exceptions.
-
#template ⇒ Object
Returns the value of attribute template.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#initialize(params = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
- #raise_exceptions? ⇒ Boolean
- #send(mail) ⇒ Object
Constructor Details
#initialize(params = {}) {|_self| ... } ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sendgrid/client.rb', line 9 def initialize(params = {}) self.api_user = params.fetch(:api_user, nil) self.api_key = params.fetch(:api_key, nil) 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, protocol + '://' + host + (port ? ":#{port}" : '')) self.endpoint = params.fetch(:endpoint, '/api/mail.send.json') self.adapter = params.fetch(:adapter, adapter) self.conn = params.fetch(:conn, conn) self.user_agent = params.fetch(:user_agent, "sendgrid/#{SendGrid::VERSION};ruby") self.raise_exceptions = params.fetch(:raise_exceptions, true) yield self if block_given? end |
Instance Attribute Details
#adapter ⇒ Object
54 55 56 |
# File 'lib/sendgrid/client.rb', line 54 def adapter @adapter ||= Faraday.default_adapter end |
#api_key ⇒ Object
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def api_key @api_key end |
#api_user ⇒ Object
Returns the value of attribute api_user.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def api_user @api_user end |
#conn ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/sendgrid/client.rb', line 46 def conn @conn ||= Faraday.new(url: url) do |conn| conn.request :multipart conn.request :url_encoded conn.adapter adapter end end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def endpoint @endpoint end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def protocol @protocol end |
#raise_exceptions=(value) ⇒ Object (writeonly)
Sets the attribute raise_exceptions
7 8 9 |
# File 'lib/sendgrid/client.rb', line 7 def raise_exceptions=(value) @raise_exceptions = value end |
#template ⇒ Object
Returns the value of attribute template.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def template @template end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def url @url end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
5 6 7 |
# File 'lib/sendgrid/client.rb', line 5 def user_agent @user_agent end |
Instance Method Details
#raise_exceptions? ⇒ Boolean
58 59 60 |
# File 'lib/sendgrid/client.rb', line 58 def raise_exceptions? @raise_exceptions end |
#send(mail) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sendgrid/client.rb', line 24 def send(mail) res = conn.post do |req| payload = mail.to_h req.url(endpoint) # Check if using username + password or API key if api_user # Username + password payload = payload.merge(api_user: api_user, api_key: api_key) else # API key req.headers['Authorization'] = "Bearer #{api_key}" end req.body = payload end fail SendGrid::Exception, res.body if raise_exceptions? && res.status != 200 SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body) end |