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)


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

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
40
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 27

def execute # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  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
    when Api::Request::METHOD_DELETE then @response = execute_delete
    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



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

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

#response_headersObject

Response headers



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

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

#server_messageObject

Response server message



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

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