Updated

app/use_cases/accounts/apps/evolution_apis/message / send.rb

B
94 lines of codes
9 methods
6.8 complexity/method
10 churn
61.24 complexity
0 duplications
class Accounts::Apps::EvolutionApis::Message::Send
  1. Accounts::Apps::EvolutionApis::Message::Send has no descriptive comment
def initialize(event) @event = event @evolution_api = event.app @phone = sending_to_group? ? event.contact.additional_attributes['group_id'] : @event.contact.phone end def call if @event.attachment.present? send_message_with_attachment else send_request('sendText', build_message_text_body) end end def send_message_with_attachment if @event.attachment.audio? send_request('sendWhatsAppAudio', build_message_audio_body) else send_request('sendMedia', build_message_file_body) end end def send_request(type, body) request = Faraday.post( "#{@evolution_api.endpoint_url}/message/#{type}/#{@evolution_api.instance}", body.to_json, @evolution_api.request_instance_headers ) if request.status == 201 { ok: JSON.parse(request.body) }
  1. Accounts::Apps::EvolutionApis::Message::Send#send_request calls 'JSON.parse(request.body)' 2 times Locations: 0 1
  2. Accounts::Apps::EvolutionApis::Message::Send#send_request calls 'request.body' 2 times Locations: 0 1
else { error: JSON.parse(request.body) }
  1. Accounts::Apps::EvolutionApis::Message::Send#send_request calls 'JSON.parse(request.body)' 2 times Locations: 0 1
  2. Accounts::Apps::EvolutionApis::Message::Send#send_request calls 'request.body' 2 times Locations: 0 1
end end def build_message_file_body file_media_type = if @event.attachment.image? || @event.attachment.video?
  1. Accounts::Apps::EvolutionApis::Message::Send#build_message_file_body calls '@event.attachment' 4 times Locations: 0 1 2
@event.attachment.file_type
  1. Accounts::Apps::EvolutionApis::Message::Send#build_message_file_body calls '@event.attachment' 4 times Locations: 0 1 2
else 'document' end { "number": normalize_phone, "options": { "delay": 1200, "presence": 'composing', "linkPreview": false }, "mediaMessage": { "mediatype": file_media_type, "caption": @event.generate_content_hash('content', @event.content)['content'], "media": @event.attachment.download_url
  1. Accounts::Apps::EvolutionApis::Message::Send#build_message_file_body calls '@event.attachment' 4 times Locations: 0 1 2
} } end def build_message_audio_body { "number": normalize_phone, "options": { "delay": 1200, "presence": 'recording', "linkPreview": false }, "audioMessage": { "audio": @event.attachment.download_url } } end def build_message_text_body { "number": normalize_phone, "options": { "delay": 1200, "presence": 'composing', "linkPreview": false }, "textMessage": { "text": @event.content } } end def normalize_phone @phone.sub(/^\+/, '') end def sending_to_group? @event.contact.additional_attributes['group_id'].present? end end