class FriendlyShipping::HttpClient

Attributes

error_handler[R]

Public Class Methods

new(error_handler: method(:wrap_in_failure)) click to toggle source

@param [Proc] error_handler Called to handle an error if one occurs

# File lib/friendly_shipping/http_client.rb, line 14
def initialize(error_handler: method(:wrap_in_failure))
  @error_handler = error_handler
end

Public Instance Methods

get(request) click to toggle source
# File lib/friendly_shipping/http_client.rb, line 18
def get(request)
  http_response = ::RestClient.get(
    request.url, request.headers
  )

  Success(convert_to_friendly_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end
post(request) click to toggle source
# File lib/friendly_shipping/http_client.rb, line 28
def post(request)
  http_response = ::RestClient.post(
    request.url,
    request.body,
    request.headers
  )

  Success(convert_to_friendly_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end
put(request) click to toggle source
# File lib/friendly_shipping/http_client.rb, line 40
def put(request)
  http_response = ::RestClient.put(
    request.url,
    request.body,
    request.headers
  )

  Success(convert_to_friendly_response(http_response))
rescue ::RestClient::Exception => e
  error_handler.call(e, original_request: request, original_response: e.response)
end

Private Instance Methods

convert_to_friendly_response(http_response) click to toggle source
# File lib/friendly_shipping/http_client.rb, line 64
def convert_to_friendly_response(http_response)
  FriendlyShipping::Response.new(
    status: http_response.code,
    body: http_response.body,
    headers: http_response.headers
  )
end
wrap_in_failure(error, original_request: nil, original_response: nil) click to toggle source
# File lib/friendly_shipping/http_client.rb, line 54
def wrap_in_failure(error, original_request: nil, original_response: nil)
  Failure(
    ApiFailure.new(
      error,
      original_request: original_request,
      original_response: original_response
    )
  )
end