Advertisement
gidpoiiohika

class Base

Apr 21st, 2020
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.70 KB | None | 0 0
  1. module Notifications
  2.   module SlackServices
  3.     class Base
  4.       SLACK_BOT_WEBHOOK_PATH = 'https://hooks.slack.com/services'.freeze
  5.  
  6.       attr_accessor :user, :slack_token, :client
  7.  
  8.       def initialize(user)
  9.         raise ArgumentError.new('SlackNotify Service [User] must be exists') unless user.present?
  10.         @user = user
  11.         @client = Slack::Notifier.new slack_token
  12.       end
  13.  
  14.       def perform
  15.         #notify unless Rails.env.development? || Rails.env.test?
  16.         binding.pry
  17.         notify
  18.       rescue StandardError => e
  19.         raise e
  20.       end
  21.  
  22.       #protected
  23.  
  24.         def notify
  25.           client.post text: notify_text,
  26.                       icon_url: ActionController::Base.helpers.asset_path('logo-arv-left-nav.svg'),
  27.                       attachments: [message]
  28.         end
  29.  
  30.         def notify_text
  31.           "#{ENV['APP_NAME']}: #{user&.full_name}"
  32.         end
  33.  
  34.       #private
  35.  
  36.         def slack_token
  37.           @slack_token = "#{SLACK_BOT_WEBHOOK_PATH}#{ENV['SLACK_BOT_CLIENT_NAME']}"
  38.         end
  39.  
  40.         def default_message_params
  41.           {
  42.             color: 'good',
  43.             fields: [
  44.               {
  45.                 title: 'First  Name',
  46.                 value: user&.first_name&.capitalize,
  47.                 short: true
  48.               },
  49.               {
  50.                 title: 'Last Name',
  51.                 value: user&.last_name&.capitalize,
  52.                 short: true
  53.               },
  54.               {
  55.                 title: 'Email',
  56.                 value: user.email,
  57.                 short: true
  58.               }
  59.             ]
  60.           }
  61.         end
  62.  
  63.         def message
  64.           default_message_params
  65.         end
  66.     end
  67.   end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement