Skip to content

Instantly share code, notes, and snippets.

@WagnerMatos
Created October 27, 2021 18:06
Show Gist options
  • Select an option

  • Save WagnerMatos/a063543cb7360f81773f3a6931cbac24 to your computer and use it in GitHub Desktop.

Select an option

Save WagnerMatos/a063543cb7360f81773f3a6931cbac24 to your computer and use it in GitHub Desktop.

Revisions

  1. WagnerMatos renamed this gist Oct 27, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. WagnerMatos created this gist Oct 27, 2021.
    72 changes: 72 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # controller
    class MyController < ApplicationController
    TEMPLATE_ID = "TEMPLATE_ID"

    def create
    authorize ...

    if send_email
    redirect_to @referer, success: success_message
    else
    redirect_to @referer, error: error_message
    end
    end

    private

    def set_case_attachable
    ...
    end

    def send_email
    ...
    Notify::Email.new(
    client_email: "email@email.com",
    template_id: TEMPLATE_ID,
    ...
    ).deliver_now
    end

    def magic_link_url(name, gid)
    ...
    end

    def success_message
    ...
    end

    def error_message
    ...
    end
    end

    # PORO
    module Notify
    class Email
    include PoroAttributes

    NOTIFY_API_KEY = SOME_KEY

    attribute :client_email
    attribute :template_id
    attribute :personalisation, default: -> {}

    def deliver_now
    send_email
    rescue Notifications::Client::RequestError
    raise # Retry elsewhere
    end

    private

    def send_email
    notify.send_email(...)
    end

    def notify
    @notify ||=
    ExternalLibrary::Client.new(SOME_API_KEY)
    end
    end
    end