Class: SendGrid::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



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

#adapterObject



54
55
56
# File 'lib/sendgrid/client.rb', line 54

def adapter
  @adapter ||= Faraday.default_adapter
end

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def api_key
  @api_key
end

#api_userObject

Returns the value of attribute api_user.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def api_user
  @api_user
end

#connObject



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

#endpointObject

Returns the value of attribute endpoint.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def endpoint
  @endpoint
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def host
  @host
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def port
  @port
end

#protocolObject

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

Parameters:

  • value

    the value to set the attribute raise_exceptions to.



7
8
9
# File 'lib/sendgrid/client.rb', line 7

def raise_exceptions=(value)
  @raise_exceptions = value
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def template
  @template
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/sendgrid/client.rb', line 5

def url
  @url
end

#user_agentObject

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

Returns:

  • (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