Class: SendgridClient
- Inherits:
-
Object
- Object
- SendgridClient
- Defined in:
- lib/email_api/email/client/sendgrid_client.rb
Overview
Client for sending email via Sendgrid v2 API
Class Method Summary collapse
-
.bad_req_code ⇒ Object
Accessor for the Bad Request Code.
-
.exp_msg ⇒ Object
Accessor for the expected success message.
-
.internal_err_code ⇒ Object
Accessor for the Internal Server Error Code.
-
.ok_code ⇒ Object
Accessor for the OK Code.
-
.send_email(email_object) ⇒ int
Sends an email over HTTPS.
Class Method Details
.bad_req_code ⇒ Object
Accessor for the Bad Request Code
23 24 25 |
# File 'lib/email_api/email/client/sendgrid_client.rb', line 23 def self.bad_req_code @bad_req_code end |
.exp_msg ⇒ Object
Accessor for the expected success message
13 14 15 |
# File 'lib/email_api/email/client/sendgrid_client.rb', line 13 def self.exp_msg @exp_msg end |
.internal_err_code ⇒ Object
Accessor for the Internal Server Error Code
28 29 30 |
# File 'lib/email_api/email/client/sendgrid_client.rb', line 28 def self.internal_err_code @internal_err_code end |
.ok_code ⇒ Object
Accessor for the OK Code
18 19 20 |
# File 'lib/email_api/email/client/sendgrid_client.rb', line 18 def self.ok_code @ok_code end |
.send_email(email_object) ⇒ int
Sends an email over HTTPS
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/email_api/email/client/sendgrid_client.rb', line 36 def self.send_email(email_object) # Parse Environment Values api_user = ENV['SENDGRID_API_USER'] api_key = ENV['SENDGRID_API_KEY'] return internal_err_code if api_user.nil? || api_key.nil? # Build Sendgrid-specific URL and POST data url = 'https://api.sendgrid.com/api/mail.send.json' post_data = parse_post_data(email_object, api_user, api_key) return bad_req_code if post_data.nil? # Send Email, return response begin response = RestClient.post url, post_data rescue StandardError => e # Log error and fail send -> occurs when Code 400 due to implementation puts "Error: #{e.message}" return bad_req_code end puts "Primary Client Response: #{response}" return bad_req_code if response.nil? || !JSON.parse(response).respond_to?(:[]) # Handle expected output. Note that it is API specific. json_response = JSON.parse(response) if !json_response.key?('message').nil? && json_response['message'] == exp_msg return ok_code end internal_err_code # Unhandled response end |