diff --git a/lib/ruby_http_client.rb b/lib/ruby_http_client.rb index 4059a7d..904e6c9 100644 --- a/lib/ruby_http_client.rb +++ b/lib/ruby_http_client.rb @@ -141,29 +141,10 @@ def build_url(query_params: nil) # def build_request(name, args) build_args(args) if args - uri = build_url(query_params: @query_params) - @http = build_http(uri.host, uri.port) - net_http = Kernel.const_get('Net::HTTP::' + name.to_s.capitalize) - @request = build_request_headers(net_http.new(uri.request_uri)) - if @request_body && - (!@request_headers.key?('Content-Type') || - @request_headers['Content-Type'] == 'application/json') - - # If body is a hash, encode it; else leave it alone - @request.body = if @request_body.class == Hash - @request_body.to_json - else - @request_body - end - @request['Content-Type'] = 'application/json' - elsif !@request_body && (name.to_s == 'post') - @request['Content-Type'] = '' - else - @request.body = @request_body - end - @http_options.each do |attribute, value| - @http.send("#{attribute}=", value) - end + # build the request & http object + build_http_request(name) + # set the content type & request body + update_content_type(name) make_request(@http, @request) end @@ -245,6 +226,37 @@ def method_missing(name, *args, &_block) # Add a segment to the URL _(name) end + + private + + def build_http_request(http_method) + uri = build_url(query_params: @query_params) + net_http = Kernel.const_get('Net::HTTP::' + http_method.to_s.capitalize) + + @http = build_http(uri.host, uri.port) + @request = build_request_headers(net_http.new(uri.request_uri)) + end + + def update_content_type(http_method) + if @request_body && content_type_json? + # If body is a hash, encode it; else leave it alone + @request.body = if @request_body.class == Hash + @request_body.to_json + else + @request_body + end + @request['Content-Type'] = 'application/json' + elsif !@request_body && http_method.to_s == 'post' + @request['Content-Type'] = '' + else + @request.body = @request_body + end + end + + def content_type_json? + !@request_headers.key?('Content-Type') || + @request_headers['Content-Type'] == 'application/json' + end # rubocop:enable Style/MethodMissingSuper # rubocop:enable Style/MissingRespondToMissing end