From 43e28339824e5a5e65e91ae50ea53349dc0d4ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Fri, 15 Sep 2023 16:07:39 +0200 Subject: [PATCH] refactor: DRY `handle_response` I use the same pattern from this PR here: https://github.com/tactilenews/100eyes/pull/1686 Looks like it's always better to pass `URI` instead of `string`: https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html Because `@uri` and `@host` get initialized. --- app/adapters/signal_adapter/api.rb | 28 ++++++++++++++++++ app/adapters/signal_adapter/outbound/file.rb | 30 ++++---------------- app/adapters/signal_adapter/outbound/text.rb | 30 ++++---------------- 3 files changed, 40 insertions(+), 48 deletions(-) create mode 100644 app/adapters/signal_adapter/api.rb diff --git a/app/adapters/signal_adapter/api.rb b/app/adapters/signal_adapter/api.rb new file mode 100644 index 000000000..3e563023f --- /dev/null +++ b/app/adapters/signal_adapter/api.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module SignalAdapter + class Api + class << self + def perform_request(request) + uri = request.uri + response = Net::HTTP.start(uri.host, uri.port) do |http| + http.request(request) + end + case response + when Net::HTTPSuccess + yield response + else + error_message = JSON.parse(response.body)['error'] + exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message) + context = { + code: response.code, + message: response.message, + headers: response.to_hash, + body: error_message + } + ErrorNotifier.report(exception, context: context) + end + end + end + end +end diff --git a/app/adapters/signal_adapter/outbound/file.rb b/app/adapters/signal_adapter/outbound/file.rb index 610b4c610..516db7f0c 100644 --- a/app/adapters/signal_adapter/outbound/file.rb +++ b/app/adapters/signal_adapter/outbound/file.rb @@ -9,16 +9,17 @@ class File < ApplicationJob def perform(message:) @message = message - url = URI.parse("#{Setting.signal_cli_rest_api_endpoint}/v2/send") - request = Net::HTTP::Post.new(url.to_s, { + uri = URI.parse("#{Setting.signal_cli_rest_api_endpoint}/v2/send") + request = Net::HTTP::Post.new(uri, { Accept: 'application/json', 'Content-Type': 'application/json' }) request.body = data.to_json - response = Net::HTTP.start(url.host, url.port) do |http| - http.request(request) + SignalAdapter::Api.perform_request(request) do + # TODO: Do something on success. For example, mark the message as delivered? + # Or should we use deliver receipts as the source of truth. + Rails.logger.debug 'Great!' end - handle_response(response) end def data @@ -32,25 +33,6 @@ def data base64_attachments: base64_files } end - - def handle_response(response) - case response.code.to_i - when 200 - # TODO: Do something on success. For example, mark the message as delivered? - # Or should we use deliver receipts as the source of truth. - Rails.logger.debug 'Great!' - when 400..599 - error_message = JSON.parse(response.body)['error'] - exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message) - context = { - code: response.code, - message: response.message, - headers: response.to_hash, - body: error_message - } - ErrorNotifier.report(exception, context: context) - end - end end end end diff --git a/app/adapters/signal_adapter/outbound/text.rb b/app/adapters/signal_adapter/outbound/text.rb index 6a994b902..95eb238a8 100644 --- a/app/adapters/signal_adapter/outbound/text.rb +++ b/app/adapters/signal_adapter/outbound/text.rb @@ -10,16 +10,17 @@ class Text < ApplicationJob def perform(recipient:, text:) @recipient = recipient @text = text - url = URI.parse("#{Setting.signal_cli_rest_api_endpoint}/v2/send") - request = Net::HTTP::Post.new(url.to_s, { + uri = URI.parse("#{Setting.signal_cli_rest_api_endpoint}/v2/send") + request = Net::HTTP::Post.new(uri, { Accept: 'application/json', 'Content-Type': 'application/json' }) request.body = data.to_json - response = Net::HTTP.start(url.host, url.port) do |http| - http.request(request) + SignalAdapter::Api.perform_request(request) do + # TODO: Do something on success. For example, mark the message as delivered? + # Or should we use deliver receipts as the source of truth. + Rails.logger.debug 'Great!' end - handle_response(response) end def data @@ -29,25 +30,6 @@ def data message: text } end - - def handle_response(response) - case response.code.to_i - when 200 - # TODO: Do something on success. For example, mark the message as delivered? - # Or should we use deliver receipts as the source of truth. - Rails.logger.debug 'Great!' - when 400..599 - error_message = JSON.parse(response.body)['error'] - exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message) - context = { - code: response.code, - message: response.message, - headers: response.to_hash, - body: error_message - } - ErrorNotifier.report(exception, context: context) - end - end end end end