From 833ac3f6d88f80cfe874c18dc36ee87c136efc39 Mon Sep 17 00:00:00 2001 From: Luca Ramundo Date: Thu, 23 Feb 2017 10:43:33 +0100 Subject: [PATCH] Small refactors --- lib/ruby_http_client.rb | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/lib/ruby_http_client.rb b/lib/ruby_http_client.rb index bdf60b0..18b97ba 100644 --- a/lib/ruby_http_client.rb +++ b/lib/ruby_http_client.rb @@ -32,9 +32,9 @@ class Client # def initialize(host: nil, request_headers: nil, version: nil, url_path: nil) @host = host - @request_headers = request_headers ? request_headers : {} + @request_headers = request_headers || {} @version = version - @url_path = url_path ? url_path : [] + @url_path = url_path || [] @methods = %w(delete get patch post put) @query_params = nil @request_body = nil @@ -46,7 +46,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil) # - +request_headers+ -> Hash of request header key/values # def update_headers(request_headers) - @request_headers = @request_headers.merge(request_headers) + @request_headers.merge!(request_headers) end # Build the final request headers @@ -70,9 +70,9 @@ def build_request_headers(request) # * *Returns* : # - The url string with the version pre-pended # - def add_version(url) - url.concat("/#{@version}") - url + def add_version(url = nil) + path = @version ? "/#{@version}" : '' + url.concat(path) end # Add query parameters to the url @@ -84,14 +84,8 @@ def add_version(url) # - The url string with the query parameters appended # def build_query_params(url, query_params) - url.concat('?') - count = 0 - query_params.each do |key, value| - url.concat('&') if count > 0 - url.concat("#{key}=#{value}") - count += 1 - end - url + params = query_params.map { |key, value| "#{key}=#{value}" }.join('&') + url.concat("?#{params}") end # Set the query params, request headers and request body @@ -122,11 +116,7 @@ def build_args(args) # - The final url string # def build_url(query_params: nil) - url = '' - url = add_version(url) if @version - @url_path.each do |x| - url.concat("/#{x}") - end + url = [add_version(''), *@url_path].join('/') url = build_query_params(url, query_params) if query_params URI.parse("#{@host}#{url}") end @@ -178,8 +168,7 @@ def make_request(http, request) # - HTTP::NET object # def add_ssl(http) - protocol = host.split(':')[0] - if protocol == 'https' + if host.start_with?('https') http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end