Skip to content

Commit

Permalink
dry connection post, put and patch methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Yves committed Nov 7, 2024
1 parent 1671c96 commit 18e2143
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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
Expand Down

0 comments on commit 18e2143

Please sign in to comment.