class Boechat::Core::Service::Request
Class responsible for make the request to one service
Constants
- BASIC_HEADER
- HTTP_UNPROCESSABLE_ENTITY
Attributes
body[R]
headers[R]
parameters[R]
request[R]
response[R]
result[R]
service_uri[R]
verb[R]
Public Class Methods
new(service_uri, verb: :get, parameters: nil, body: nil, headers: nil)
click to toggle source
# File lib/boechat/core/service/request.rb, line 20 def initialize(service_uri, verb: :get, parameters: nil, body: nil, headers: nil) @service_uri = service_uri @verb = verb @parameters = parameters @body = body @headers = headers @request = Typhoeus::Request.new(@service_uri, method: @verb, params: @parameters, body: @body, headers: http_header) end
Public Instance Methods
call()
click to toggle source
# File lib/boechat/core/service/request.rb, line 30 def call handle_request end
Private Instance Methods
handle_request()
click to toggle source
# File lib/boechat/core/service/request.rb, line 41 def handle_request @request.on_complete do |res| @response = res @result = Result.new(valid_response(res)) end @request.run end
http_header()
click to toggle source
# File lib/boechat/core/service/request.rb, line 36 def http_header return BASIC_HEADER.merge(@headers) if @headers BASIC_HEADER end
response_error(response)
click to toggle source
# File lib/boechat/core/service/request.rb, line 57 def response_error(response) code = response.code case code when 404 Oj.load({ error: 'Not Found', code: code }.to_json, symbol_keys: true) when 500 Oj.load({ error: 'Service Internal Error', code: code }.to_json, symbol_keys: true) else Oj.load({ error: 'Service Unexpected Error', code: code }.to_json, symbol_keys: true) end end
response_invalid_format()
click to toggle source
# File lib/boechat/core/service/request.rb, line 70 def response_invalid_format Oj.load({ error: 'Invalid JSON format', status: HTTP_UNPROCESSABLE_ENTITY }.to_json) end
valid_response(response)
click to toggle source
# File lib/boechat/core/service/request.rb, line 49 def valid_response(response) body = response.body return response_error(response) if body.empty? return response_invalid_format unless body.valid_json? Oj.load(body, symbol_keys: true).merge(status: response.code) end