@@ -141,29 +141,10 @@ def build_url(query_params: nil)
141
141
#
142
142
def build_request ( name , args )
143
143
build_args ( args ) if args
144
- uri = build_url ( query_params : @query_params )
145
- @http = build_http ( uri . host , uri . port )
146
- net_http = Kernel . const_get ( 'Net::HTTP::' + name . to_s . capitalize )
147
- @request = build_request_headers ( net_http . new ( uri . request_uri ) )
148
- if @request_body &&
149
- ( !@request_headers . key? ( 'Content-Type' ) ||
150
- @request_headers [ 'Content-Type' ] == 'application/json' )
151
-
152
- # If body is a hash, encode it; else leave it alone
153
- @request . body = if @request_body . class == Hash
154
- @request_body . to_json
155
- else
156
- @request_body
157
- end
158
- @request [ 'Content-Type' ] = 'application/json'
159
- elsif !@request_body && ( name . to_s == 'post' )
160
- @request [ 'Content-Type' ] = ''
161
- else
162
- @request . body = @request_body
163
- end
164
- @http_options . each do |attribute , value |
165
- @http . send ( "#{ attribute } =" , value )
166
- end
144
+ # build the request & http object
145
+ build_http_request ( name )
146
+ # set the content type & request body
147
+ update_content_type ( name )
167
148
make_request ( @http , @request )
168
149
end
169
150
@@ -245,6 +226,37 @@ def method_missing(name, *args, &_block)
245
226
# Add a segment to the URL
246
227
_ ( name )
247
228
end
229
+
230
+ private
231
+
232
+ def build_http_request ( http_method )
233
+ uri = build_url ( query_params : @query_params )
234
+ net_http = Kernel . const_get ( 'Net::HTTP::' + http_method . to_s . capitalize )
235
+
236
+ @http = build_http ( uri . host , uri . port )
237
+ @request = build_request_headers ( net_http . new ( uri . request_uri ) )
238
+ end
239
+
240
+ def update_content_type ( http_method )
241
+ if @request_body && content_type_json?
242
+ # If body is a hash, encode it; else leave it alone
243
+ @request . body = if @request_body . class == Hash
244
+ @request_body . to_json
245
+ else
246
+ @request_body
247
+ end
248
+ @request [ 'Content-Type' ] = 'application/json'
249
+ elsif !@request_body && http_method . to_s == 'post'
250
+ @request [ 'Content-Type' ] = ''
251
+ else
252
+ @request . body = @request_body
253
+ end
254
+ end
255
+
256
+ def content_type_json?
257
+ !@request_headers . key? ( 'Content-Type' ) ||
258
+ @request_headers [ 'Content-Type' ] == 'application/json'
259
+ end
248
260
# rubocop:enable Style/MethodMissingSuper
249
261
# rubocop:enable Style/MissingRespondToMissing
250
262
end
0 commit comments