-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MovuTransceiver, MovuJsonBuilder, HttpRequester, ResponseLogger
In this refactoring sprint i added attr_readers and a main class method to Each service object, things look better now. Discoved a Bug: It seems some jobs get rejected when not using the ResponseLogger in MovuTransceiver. Need to figure out why this is happening. My gut tells me its NET::HTTP, i tried adding a delay instead but notthing works only the logger. The logger calls the NET::HTTP response object. which causes a delay I guess... will try other http requesters soon: http://www.rubyguides.com/2018/08/ruby-http-request/
- Loading branch information
1 parent
c759924
commit 8a7ee24
Showing
7 changed files
with
72 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 15 additions & 9 deletions
24
app/services/http_post_requester.rb → app/services/http_requester.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
class MovuTransceiver | ||
attr_reader :inquiry, :inquiry_json, :api_url | ||
attr_accessor :response | ||
|
||
def initialize(inquiry) | ||
@inquiry = inquiry | ||
@response = nil | ||
@inquiry_json = MovuJsonBuilder.build(inquiry) | ||
@api_url = Cre.dig(:partner_url) + Cre.dig(:partner_api_path) | ||
end | ||
|
||
def preform | ||
send_request | ||
def self.transceive(inquiry) | ||
new(inquiry).send(:send_and_receive) | ||
end | ||
|
||
private | ||
|
||
def send_request | ||
inquiry_json = MovuJsonBuilder.new(@inquiry).call | ||
@response = HttpPostRequester.new(inquiry_json, @api_url).call | ||
ResponseLogger.new(@response, @inquiry).call | ||
@response.code == "200" ? receive_response : TelegramNotifier.new(HttpStatusCodeLogger.new(@response.code).call) | ||
def send_and_receive | ||
self.response = HttpRequester.post(inquiry_json, api_url) | ||
# NOTE: BUG when not using this log function some jobs get "rejected"! | ||
# I think it has todo with NET::HTTP. Will be trying others soon! | ||
log(response) # keep this here for now! | ||
response.code == "200" ? create_received : TelegramNotifier.new(log(response)) | ||
end | ||
|
||
def receive_response | ||
def create_received | ||
ReceivedInquiryResponse.create!( | ||
:"#{@inquiry.class.name.underscore}_id" => @inquiry.id, | ||
:response_body => @response.body | ||
:"#{inquiry.class.name.underscore}_id" => inquiry.id, | ||
:response_body => response.body | ||
) | ||
end | ||
|
||
def log(response) | ||
ResponseLogger.log(response) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class ResponseLogger | ||
def initialize(response, inquiry) | ||
attr_reader :response | ||
|
||
def initialize(response) | ||
@response = response | ||
@inquiry = inquiry | ||
end | ||
|
||
def call | ||
def self.log(response) | ||
new(response).send(:log) | ||
end | ||
|
||
private | ||
|
||
def log | ||
status | ||
response_body | ||
end | ||
|
||
private | ||
attr_reader :response, :inquiry | ||
|
||
def status | ||
warn 'STATUS:' | ||
warn "CODE: #{response.code}" if response.code # => '200' | ||
warn "MESSAGE: #{response.message}" if response.message # => 'OK' | ||
warn "CLASS NAME: #{response.class.name}" if response.class.name # => 'HTTPOK' | ||
end | ||
|
||
# def headers | ||
# # warn "Headers: " | ||
# # warn JSON::PrettyPrint.prettify(response.to_json) if response | ||
# end | ||
|
||
def response_body | ||
warn 'RESPONSE BODY: ' | ||
warn "#{JSON::PrettyPrint.prettify(response.body)}" if response.body | ||
warn '⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️' | ||
warn "LEAD ID: #{inquiry.id} !!" | ||
warn "LEAD ID: #{inquiry.class.name} !!" | ||
end | ||
def status | ||
warn 'STATUS:' | ||
warn "CODE: #{response.code}" if response.code # => '200' | ||
warn "MESSAGE: #{response.message}" if response.message # => 'OK' | ||
warn "CLASS NAME: #{response.class.name}" if response.class.name # => 'HTTPOK' | ||
end | ||
|
||
# def headers | ||
# # warn "Headers: " | ||
# # warn JSON::PrettyPrint.prettify(response.to_json) if response | ||
# end | ||
|
||
def response_body | ||
warn 'RESPONSE BODY: ' | ||
warn JSON::PrettyPrint.prettify(response.body).to_s if response.body | ||
warn '⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️ ⚠️' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters