Class: RmsApiRuby::Chain::HttpClient
- Inherits:
-
RmsApiRuby::Chain
- Object
- RmsApiRuby::Chain
- RmsApiRuby::Chain::HttpClient
- Includes:
- HashKeysUnderscorable
- Defined in:
- lib/rms_api_ruby/chain/http_client.rb
Constant Summary collapse
- SERVER_ERROR =
500
Instance Method Summary collapse
- #call ⇒ Object
- #execute_request ⇒ Object
-
#initialize(method:, url:, headers: nil, params: nil, return_method:) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #set_body(params) ⇒ Object
- #set_headers(headers) ⇒ Object
Methods included from HashKeysUnderscorable
Constructor Details
#initialize(method:, url:, headers: nil, params: nil, return_method:) ⇒ HttpClient
Returns a new instance of HttpClient.
14 15 16 17 18 19 20 21 |
# File 'lib/rms_api_ruby/chain/http_client.rb', line 14 def initialize(method:, url:, headers: nil, params: nil, return_method:) @uri = URI.parse url_factory(url, method, params) @request = request_class(method).new request_uri(method, @uri) @return_method = return_method set_headers(headers) if headers.present? set_body(params) if method != :get && params.present? end |
Instance Method Details
#call ⇒ Object
37 38 39 40 41 42 |
# File 'lib/rms_api_ruby/chain/http_client.rb', line 37 def call chain { execute_request } when_truthy { status_code >= SERVER_ERROR }. dam { handle_http_error } chain(:response) { parse_to_mash } end |
#execute_request ⇒ Object
31 32 33 34 35 |
# File 'lib/rms_api_ruby/chain/http_client.rb', line 31 def execute_request @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: true) do |http| http.request @request end end |
#set_body(params) ⇒ Object
27 28 29 |
# File 'lib/rms_api_ruby/chain/http_client.rb', line 27 def set_body(params) @request.body = params end |
#set_headers(headers) ⇒ Object
23 24 25 |
# File 'lib/rms_api_ruby/chain/http_client.rb', line 23 def set_headers(headers) headers.each { |k, v| @request[k] = v } end |