Sha256: df7a96422d964aae74c4555356027d84e30855b1e13fb53391c32c8f07a6b38b

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

# -*- encoding: utf-8 -*-

module SendGrid4r::REST
  #
  # SendGrid Web API v3 Subusers
  #
  module Bounces
    include Request

    Bounce = Struct.new(:created, :email, :reason, :status)

    def self.url(email = nil)
      url = "#{BASE_URL}/suppression/bounces"
      url = "#{url}/#{email}" unless email.nil?
      url
    end

    def self.create_bounces(resp)
      return resp if resp.nil?
      resp.map { |bounce| Bounces.create_bounce(bounce) }
    end

    def self.create_bounce(resp)
      return resp if resp.nil?
      created = Time.at(resp['created']) unless resp['created'].nil?
      Bounce.new(created, resp['email'], resp['reason'], resp['status'])
    end

    def get_bounces(start_time: nil, end_time: nil, &block)
      params = {}
      params[:start_time] = start_time.to_i unless start_time.nil?
      params[:end_time] = end_time.to_i unless end_time.nil?
      resp = get(@auth, Bounces.url, params, &block)
      finish(resp, @raw_resp) { |r| Bounces.create_bounces(r) }
    end

    def delete_bounces(delete_all: nil, emails: nil, &block)
      if delete_all
        payload = { delete_all: delete_all }
      else
        payload = { emails: emails }
      end
      delete(@auth, Bounces.url, nil, payload, &block)
    end

    def get_bounce(email:, &block)
      resp = get(@auth, Bounces.url(email), &block)
      finish(resp, @raw_resp) { |r| Bounces.create_bounces(r) }
    end

    def delete_bounce(email:, &block)
      delete(@auth, Bounces.url(email), &block)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sendgrid4r-1.15.0 lib/sendgrid4r/rest/bounces.rb
sendgrid4r-1.14.0 lib/sendgrid4r/rest/bounces.rb
sendgrid4r-1.13.0 lib/sendgrid4r/rest/bounces.rb