Skip to content

Commit

Permalink
extract handle_response
Browse files Browse the repository at this point in the history
  • Loading branch information
SViccari committed Dec 20, 2018
1 parent f645752 commit 9d519e7
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def get_json(path, opts)
url = generate_url(path, opts)
response = get(url, format: :json)
log_request_and_response url, response
raise(Hubspot::RequestError.new(response)) unless response.success?
response.parsed_response
handle_response(response)
end

def post_json(path, opts)
Expand All @@ -22,12 +21,18 @@ def post_json(path, opts)
no_parse ? response : response.parsed_response
end

def put_json(path, opts)
url = generate_url(path, opts[:params])
response = put(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json)
log_request_and_response url, response, opts[:body]
raise(Hubspot::RequestError.new(response)) unless response.success?
response.parsed_response
def put_json(path, options)
url = generate_url(path, options[:params])

response = put(
url,
body: options[:body].to_json,
headers: { "Content-Type" => "application/json" },
format: :json
)

log_request_and_response(url, response, options[:body])
handle_response(response)
end

def delete_json(path, opts)
Expand All @@ -40,6 +45,14 @@ def delete_json(path, opts)

protected

def handle_response(response)
if response.success?
response.parsed_response
else
raise(Hubspot::RequestError.new(response))
end
end

def log_request_and_response(uri, response, body=nil)
Hubspot::Config.logger.info(<<~MSG)
Hubspot: #{uri}.
Expand Down

0 comments on commit 9d519e7

Please sign in to comment.