class StealthWebhook::Webhook
Constants
- StructUser
Attributes
errors[R]
Public Class Methods
new()
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 6 def initialize @errors = {} end
Public Instance Methods
deliver(recipient, message)
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 29 def deliver(recipient, message) Stealth::ScheduledReplyJob.perform_in(0, 'facebook', recipient, 'webhook', 'say_notification', message) true end
send_message(params)
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 10 def send_message(params) if validate_and_send_message(params) [200, { message: 'Message sent with success' }] else [@errors[:status], @errors.to_json] end rescue => e [500, { message: e.message }] end
validate_and_send_message(params)
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 21 def validate_and_send_message(params) return unless validate(params) user = recipient(params) return unless user deliver(user.fb_id, params[:message]) end
Private Instance Methods
recipient(params)
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 52 def recipient(params) if params.dig(:recipient, :fb_id) user = StructUser.new(params.dig(:recipient, :fb_id)) else query_params = { solar_id: params.dig(:recipient, :assistido_id), cpf: params.dig(:recipient, :assistido_cpf) }.compact user = User.where(query_params).first end @errors= {message: "recipient not found: #{query_params.to_json})", code: 404, status: :not_found} unless user user end
validate(params)
click to toggle source
# File lib/stealth-webhook/webhook.rb, line 38 def validate(params) unless params[:recipient].respond_to?(:dig) @errors = {message: ":recipient must be a hash : #{params[:recipient].inspect}", code: 400, status: :bad_request} return end unless params.dig(:recipient, :assistido_id) || params.dig(:recipient, :assistido_cpf) || params.dig(:recipient, :fb_id) @errors= {message: ':recipient must have the follow keys: assistido_id ou assistido_cpf', code: 400, status: :bad_request} return end true end