Class: SendgridNotification::SendgridClient

Inherits:
Object
  • Object
show all
Defined in:
app/models/sendgrid_notification/sendgrid_client.rb

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

SUPPRESSION_TYPES =
%w(invalid_emails bounces blocks).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSendgridClient

Returns a new instance of SendgridClient.



41
42
43
44
45
46
47
48
49
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 41

def initialize
  @api_key = Rails.application.config.sendgrid_notification.api_key
  if api_key.blank?
    raise Error, "no sendgrid api_key. set config.sendgrid_notification.api_key or " \
                 "SENDGRID_API_KEY environment variable."
  end
  @api = SendGrid::API.new(api_key: api_key)
  @last_response = nil
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



37
38
39
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 37

def api
  @api
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



37
38
39
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 37

def api_key
  @api_key
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



37
38
39
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 37

def last_response
  @last_response
end

Instance Method Details

#mail_send(from:, from_name:, to:, subject:, body:) ⇒ Object

TODO: status_code が 500 系およびタイムアウトのときにリトライできるようにするPOST /mail/send



53
54
55
56
57
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 53

def mail_send(from:, from_name:, to:, subject:, body:)
  mail = build_mail(from, from_name, to, subject, body)
  res = client.mail._('send').post(request_body: mail.to_json)
  @last_response = Response.new(res)
end

#suppression_get(suppression_type, start_time:, end_time:, limit: 100, offset: 0) ⇒ Object

GET /suppression/#type/



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/sendgrid_notification/sendgrid_client.rb', line 62

def suppression_get(suppression_type, start_time:, end_time:, limit: 100, offset: 0)
  unless SUPPRESSION_TYPES.include?(suppression_type.to_s)
    # 警告のみ
    Rails.logger.error("Unknown suppresssion type '#{suppression_type}'")
  end

  params = {
    start_time: start_time.to_i,
    end_time: end_time.to_i,
    limit: limit,
    offset: offset,
  }
  res = client.suppression._(suppression_type).get(query_params: params)
  @last_response = Response.new(res)
end