From 9d291638c468f1ba9ec471f4b17d1d1c07c8fb82 Mon Sep 17 00:00:00 2001 From: Arun Kumar Mohan Date: Wed, 2 Oct 2019 01:14:34 -0500 Subject: [PATCH] Refactor Attachment#push_event_data --- app/models/attachment.rb | 57 ++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 5d51362c0..b3c74df83 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -9,36 +9,47 @@ class Attachment < ApplicationRecord before_create :set_file_extension def push_event_data - data = { + return base_data.merge(location_metadata) if file_type.to_sym == :location + return base_data.merge(fallback_data) if file_type.to_sym == :fallback + + base_data.merge(file_metadata) + end + + private + + def file_metadata + { + extension: extension, + data_url: file_url, + thumb_url: file.try(:thumb).try(:url) # will exist only for images + } + end + + def location_metadata + { + coordinates_lat: coordinates_lat, + coordinates_long: coordinates_long, + fallback_title: fallback_title, + data_url: external_url + } + end + + def fallback_data + { + fallback_title: fallback_title, + data_url: external_url + } + end + + def base_data + { id: id, message_id: message_id, file_type: file_type, account_id: account_id } - if [:image, :file, :audio, :video].include? file_type.to_sym - data.merge!({ - extension: extension, - data_url: file_url, - thumb_url: file.try(:thumb).try(:url) #will exist only for images - }) - elsif :location == file_type.to_sym - data.merge!({ - coordinates_lat: coordinates_lat, - coordinates_long: coordinates_long, - fallback_title: fallback_title, - data_url: external_url - }) - elsif :fallback == file_type.to_sym - data.merge!({ - fallback_title: fallback_title, - data_url: external_url - }) - end - data end - private - def set_file_extension if self.external_url && !self.fallback? self.extension = Pathname.new(URI(external_url).path).extname rescue nil