Class: FriendlyShipping::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_handler: FriendlyShipping::ApiErrorHandler.new) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • error_handler (Proc) (defaults to: FriendlyShipping::ApiErrorHandler.new)

    Called to handle an error if one occurs



16
17
18
# File 'lib/friendly_shipping/http_client.rb', line 16

def initialize(error_handler: FriendlyShipping::ApiErrorHandler.new)
  @error_handler = error_handler
end

Instance Attribute Details

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



13
14
15
# File 'lib/friendly_shipping/http_client.rb', line 13

def error_handler
  @error_handler
end

Instance Method Details

#get(request) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/friendly_shipping/http_client.rb', line 20

def get(request)
  http_response = ::RestClient.get(
    request.url, request.headers
  )

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

#post(request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/friendly_shipping/http_client.rb', line 30

def post(request)
  http_response = ::RestClient.post(
    request.url,
    request.body,
    request.headers
  )

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

#put(request) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/friendly_shipping/http_client.rb', line 42

def put(request)
  http_response = ::RestClient.put(
    request.url,
    request.body,
    request.headers
  )

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