From bbbc14448b5a76c92b0a29f559debf9cf33fed42 Mon Sep 17 00:00:00 2001 From: Ender Ahmet Yurt Date: Sat, 5 Oct 2019 12:15:32 +0300 Subject: [PATCH] Refactor for attachment_params method (#108) --- app/builders/messages/message_builder.rb | 52 ++++++++++++++---------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index cdf16dc9f..3b2eb5e84 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -70,35 +70,43 @@ Assumptions def attachment_params(attachment) file_type = attachment['type'].to_sym - params = { - file_type: file_type, - account_id: @message.account_id - } + params = { file_type: file_type, account_id: @message.account_id } + if [:image, :file, :audio, :video].include? file_type - params.merge!( - { - external_url: attachment['payload']['url'], - remote_file_url: attachment['payload']['url'] - }) + params.merge!(file_type_params(attachment)) elsif file_type == :location - lat, long = attachment['payload']['coordinates']['lat'], attachment['payload']['coordinates']['long'] - params.merge!( - { - external_url: attachment['url'], - coordinates_lat: lat, - coordinates_long: long, - fallback_title: attachment['title'] - }) + params.merge!(location_params(attachment)) elsif file_type == :fallback - params.merge!( - { - fallback_title: attachment['title'], - external_url: attachment['url'] - }) + params.merge!(fallback_params(attachment)) end + params end + def file_type_params(attachment) + { + external_url: attachment['payload']['url'], + remote_file_url: attachment['payload']['url'] + } + end + + def location_params(attachment) + lat, long = attachment['payload']['coordinates']['lat'], attachment['payload']['coordinates']['long'] + { + external_url: attachment['url'], + coordinates_lat: lat, + coordinates_long: long, + fallback_title: attachment['title'] + } + end + + def fallback_params(attachment) + { + fallback_title: attachment['title'], + external_url: attachment['url'] + } + end + def conversation_params { account_id: @inbox.account_id,