Sha256: e1e5ce64c35462cb33eb60108d35eee1e28612d82fe8ce6d8a8721f78a83c6c8

Contents?: true

Size: 1.32 KB

Versions: 26

Compression:

Stored size: 1.32 KB

Contents

class CastingInviter
  EMAIL_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/

  attr_reader :message, :invitees, :casting

  def initialize(attributes = {})
    @message = attributes[:message] || ""
    @invitees = attributes[:invitees] || ""
    @sender = attributes[:sender]
    @casting = attributes[:casting]
  end

  def valid?
    valid_message? && valid_invitees?
  end

  def deliver
    if valid?
      invitee_list.each do |email|
        invitation = create_invitation(email)
        Mailer.invitation_notification(invitation, @message)
      end
    else
      failure_message =
        "Your #{
          @casting
        } message couldn’t be sent. Invitees emails or message are invalid"
      invitation = create_invitation(@sender)
      Mailer.invitation_notification(invitation, failure_message)
    end
  end

  private

  def invalid_invitees
    @invalid_invitees ||=
      invitee_list.map { |item| item unless item.match(EMAIL_REGEX) }.compact
  end

  def invitee_list
    @invitee_list ||= @invitees.gsub(/\s+/, "").split(/[\n,;]+/)
  end

  def valid_message?
    @message.present?
  end

  def valid_invitees?
    invalid_invitees.empty?
  end

  def create_invitation(email)
    Invitation.create(
      casting: @casting,
      sender: @sender,
      invitee_email: email,
      status: "pending"
    )
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
coverband-6.0.2 test/fixtures/casting_invitor.rb
coverband-6.0.1 test/fixtures/casting_invitor.rb
coverband-6.0.1.rc.1 test/fixtures/casting_invitor.rb
coverband-6.0.0 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.6 test/fixtures/casting_invitor.rb
coverband-5.2.3.2 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.5 test/fixtures/casting_invitor.rb
coverband-5.2.3.1 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.4 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.3 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.2 test/fixtures/casting_invitor.rb
coverband-5.2.6.rc.1 test/fixtures/casting_invitor.rb
coverband-5.2.5 test/fixtures/casting_invitor.rb
coverband-5.2.5.rc.3 test/fixtures/casting_invitor.rb
coverband-5.2.5.rc.2 test/fixtures/casting_invitor.rb
coverband-5.2.5.rc.1 test/fixtures/casting_invitor.rb
coverband-5.2.4 test/fixtures/casting_invitor.rb
coverband-5.2.3 test/fixtures/casting_invitor.rb
coverband-5.2.2 test/fixtures/casting_invitor.rb
coverband-5.2.1 test/fixtures/casting_invitor.rb