Class: GenesisRuby::Network::Adapter::NetHttpAdapter
- Inherits:
-
BaseAdapter
- Object
- BaseAdapter
- GenesisRuby::Network::Adapter::NetHttpAdapter
- Defined in:
- lib/genesis_ruby/network/adapter/net_http_adapter.rb
Overview
Net-HTTP Adapter implementation
Instance Method Summary collapse
-
#error? ⇒ Boolean
Whether the response is an error (HTTP Code != 200).
-
#execute ⇒ Object
Send the request.
-
#prepare_request(data) ⇒ Object
Prepare the request from the given data.
-
#response_body ⇒ Object
Response body.
-
#response_headers ⇒ Object
Response headers.
-
#server_message ⇒ Object
Response server message.
-
#status ⇒ Object
HTTP Response Status Code.
Instance Method Details
#error? ⇒ Boolean
Whether the response is an error (HTTP Code != 200)
52 53 54 |
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 52 def error? !@response.is_a? Net::HTTPSuccess end |
#execute ⇒ Object
Send the request
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 ||= '', ) end |
#response_body ⇒ Object
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_headers ⇒ Object
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_message ⇒ Object
Response server message
57 58 59 60 61 62 |
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 57 def = status += " #{@response.}" if @response. end |
#status ⇒ Object
HTTP Response Status Code
14 15 16 |
# File 'lib/genesis_ruby/network/adapter/net_http_adapter.rb', line 14 def status @response&.code end |