@@ -32,9 +32,9 @@ class Client
3232 #
3333 def initialize ( host : nil , request_headers : nil , version : nil , url_path : nil )
3434 @host = host
35- @request_headers = request_headers ? request_headers : { }
35+ @request_headers = request_headers || { }
3636 @version = version
37- @url_path = url_path ? url_path : [ ]
37+ @url_path = url_path || [ ]
3838 @methods = %w( delete get patch post put )
3939 @query_params = nil
4040 @request_body = nil
@@ -46,7 +46,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil)
4646 # - +request_headers+ -> Hash of request header key/values
4747 #
4848 def update_headers ( request_headers )
49- @request_headers = @request_headers . merge ( request_headers )
49+ @request_headers . merge! ( request_headers )
5050 end
5151
5252 # Build the final request headers
@@ -70,9 +70,9 @@ def build_request_headers(request)
7070 # * *Returns* :
7171 # - The url string with the version pre-pended
7272 #
73- def add_version ( url )
74- url . concat ( "/#{ @version } " )
75- url
73+ def add_version ( url = nil )
74+ path = @version ? "/#{ @version } " : ''
75+ url . concat ( path )
7676 end
7777
7878 # Add query parameters to the url
@@ -84,14 +84,8 @@ def add_version(url)
8484 # - The url string with the query parameters appended
8585 #
8686 def build_query_params ( url , query_params )
87- url . concat ( '?' )
88- count = 0
89- query_params . each do |key , value |
90- url . concat ( '&' ) if count > 0
91- url . concat ( "#{ key } =#{ value } " )
92- count += 1
93- end
94- url
87+ params = query_params . map { |key , value | "#{ key } =#{ value } " } . join ( '&' )
88+ url . concat ( "?#{ params } " )
9589 end
9690
9791 # Set the query params, request headers and request body
@@ -122,11 +116,7 @@ def build_args(args)
122116 # - The final url string
123117 #
124118 def build_url ( query_params : nil )
125- url = ''
126- url = add_version ( url ) if @version
127- @url_path . each do |x |
128- url . concat ( "/#{ x } " )
129- end
119+ url = [ add_version ( '' ) , *@url_path ] . join ( '/' )
130120 url = build_query_params ( url , query_params ) if query_params
131121 URI . parse ( "#{ @host } #{ url } " )
132122 end
@@ -178,8 +168,7 @@ def make_request(http, request)
178168 # - HTTP::NET object
179169 #
180170 def add_ssl ( http )
181- protocol = host . split ( ':' ) [ 0 ]
182- if protocol == 'https'
171+ if host . start_with? ( 'https' )
183172 http . use_ssl = true
184173 http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
185174 end
0 commit comments