diff --git a/lib/hubspot/connection.rb b/lib/hubspot/connection.rb index 81f8e08c..f233fa37 100644 --- a/lib/hubspot/connection.rb +++ b/lib/hubspot/connection.rb @@ -10,49 +10,33 @@ def get_json(path, opts) handle_response(response).parsed_response end - def post_json(path, opts) - no_parse = opts[:params].delete(:no_parse) { false } - - url = generate_url(path, opts[:params]) - response = post( - url, - body: opts[:body].to_json, - headers: { 'Content-Type' => 'application/json' }, - format: :json, - read_timeout: read_timeout(opts), - open_timeout: open_timeout(opts) - ) - - log_request_and_response url, response, opts[:body] - handle_response(response).yield_self do |r| - no_parse ? r : r.parsed_response - end + def post_json(path, options) + modification_query(:post, path, options) end def put_json(path, options) - no_parse = options[:params].delete(:no_parse) { false } - url = generate_url(path, options[:params]) + modification_query(:put, path, options) + end - response = put( - url, - body: options[:body].to_json, - headers: { "Content-Type" => "application/json" }, - format: :json, - read_timeout: read_timeout(options), - open_timeout: open_timeout(options), - ) + def patch_json(path, options) + modification_query(:patch, path, options) + end - log_request_and_response(url, response, options[:body]) - handle_response(response).yield_self do |r| - no_parse ? r : r.parsed_response - end + def delete_json(path, opts) + url = generate_url(path, opts) + response = delete(url, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts)) + log_request_and_response url, response, opts[:body] + handle_response(response) end - def patch_json(path, options) + protected + + def modification_query(verb, path, options) no_parse = options[:params].delete(:no_parse) { false } url = generate_url(path, options[:params]) - response = patch( + response = public_send( + verb, url, body: options[:body].to_json, headers: { "Content-Type" => "application/json" }, @@ -67,15 +51,6 @@ def patch_json(path, options) end end - def delete_json(path, opts) - url = generate_url(path, opts) - response = delete(url, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts)) - log_request_and_response url, response, opts[:body] - handle_response(response) - end - - protected - def read_timeout(opts = {}) opts.delete(:read_timeout) || Hubspot::Config.read_timeout end