Class: GenesisRuby::Network::Adapter::NetHttpAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/genesis_ruby/network/adapter/net_http_adapter.rb

Overview

Net-HTTP Adapter implementation

Instance Method Summary collapse

Instance Method Details

#error?Boolean

Whether the response is an error (HTTP Code != 200)

Returns:

  • (Boolean)


52
53
54
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 52

def error?
  !@response.is_a? Net::HTTPSuccess
end

#executeObject

Send the request

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 27

def execute # rubocop:disable Metrics/AbcSize
  raise NetworkError, 'Request is not initialized' unless @request

  safe_execute do
    case request_data.type
    when Api::Request::METHOD_POST then @response = @request.post path, request_data.body, headers
    when Api::Request::METHOD_PUT then @response = @request.put path, request_data.body, headers
    when Api::Request::METHOD_GET then @response = @request.get path, headers
    when Api::Request::METHOD_PATCH then @response = @request.patch path, request_data.body, headers
    else raise 'Invalid Request Type!'
    end
  end
end

#prepare_request(data) ⇒ Object

Prepare the request from the given data



19
20
21
22
23
24
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 19

def prepare_request(data)
  @request_data = data
  @uri          = parse_uri
  # Force connection re-creation
  @request      = Net::HTTP.start(@uri.hostname ||= '', ssl_options)
end

#response_bodyObject

Response body



42
43
44
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 42

def response_body
  @response_body ||= @response ? @response.body : ''
end

#response_headersObject

Response headers



47
48
49
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 47

def response_headers
  @response_headers ||= @response ? @response.each_header.to_h : {}
end

#server_messageObject

Response server message



57
58
59
60
61
62
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 57

def server_message
  message = status
  message += " #{@response.message}" if @response.message

  message
end

#statusObject

HTTP Response Status Code



14
15
16
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 14

def status
  @response&.code
end