Skip to content

Commit

Permalink
Refactor MovuTransceiver, MovuJsonBuilder, HttpRequester, ResponseLogger
Browse files Browse the repository at this point in the history
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
khalilgharbaoui committed Sep 29, 2018
1 parent c759924 commit 8a7ee24
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 133 deletions.
2 changes: 1 addition & 1 deletion app/jobs/inquiry_delivery_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def work(msg)

def deliver_inquiry(msg)
inquiry = inquiry(msg)
MovuTransceiver.new(inquiry).preform
MovuTransceiver.transceive(inquiry)
end

def deliver_client_email(msg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# frozen_string_literal: true

# This PORO simply creates an HTTP POST request to a URL,
# while carrying a JSON payload in the request body.
# Its outcome is an HTTP response object with headers, response_body etc.
class HttpPostRequester
# This PORO simply has 1 class method post that make an HTTP POST request
# to a API endpoint URL,While carrying a JSON payload in the request body.
# Its outcome is an HTTP response object with headers, response_body etc...
class HttpRequester
def initialize(json, url)
@json = json
@uri = URI.parse(url)
end

def call
create_http_response
def self.post(json, url)
new(json, url).send(:post)
end

private

def create_http_response
def post
response = http_object.request(http_post_object) # make the post request
warn "💡 #{response.code} => #{response.message}"
return response
end

def http_object
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.request(post_request_object) # make the post request
http
end

def post_request_object
def http_post_object
request = Net::HTTP::Post.new(@uri.request_uri)
request['accept'] = 'application/json'
request['content-type'] = 'application/json'
Expand Down
78 changes: 0 additions & 78 deletions app/services/http_status_code_logger.rb

This file was deleted.

10 changes: 6 additions & 4 deletions app/services/movu_json_builder.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
class MovuJsonBuilder
attr_reader :inquiry

def initialize(inquiry)
@inquiry = inquiry
end

def call
build_json
def self.build(inquiry)
new(inquiry).send(:build)
end

private

def build_json
@inquiry.as_json(except: exclusions).merge(inclusions).to_json
def build
inquiry.as_json(except: exclusions).merge(inclusions).to_json
end

def inclusions
Expand Down
30 changes: 19 additions & 11 deletions app/services/movu_transceiver.rb
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
54 changes: 29 additions & 25 deletions app/services/response_logger.rb
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
7 changes: 2 additions & 5 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

Expand Down Expand Up @@ -93,6 +88,8 @@
# mailcatcher configuration
config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }

config.action_mailer.perform_caching = false
config.action_mailer.logger = nil
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true
Expand Down

0 comments on commit 8a7ee24

Please sign in to comment.