Skip to content

Commit

Permalink
refactor: DRY handle_response
Browse files Browse the repository at this point in the history
I use the same pattern from this PR here: #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.
  • Loading branch information
roschaefer committed Sep 15, 2023
1 parent 60dc5fc commit 43e2833
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
28 changes: 28 additions & 0 deletions app/adapters/signal_adapter/api.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 6 additions & 24 deletions app/adapters/signal_adapter/outbound/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
30 changes: 6 additions & 24 deletions app/adapters/signal_adapter/outbound/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 43e2833

Please sign in to comment.