Skip to content

Small refactors and cosmetics #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions lib/ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Client
#
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil)
@host = host
@request_headers = request_headers ? request_headers : {}
@request_headers = request_headers || {}
@version = version
@url_path = url_path ? url_path : []
@url_path = url_path || []
@methods = %w(delete get patch post put)
@query_params = nil
@request_body = nil
Expand All @@ -46,7 +46,7 @@ def initialize(host: nil, request_headers: nil, version: nil, url_path: nil)
# - +request_headers+ -> Hash of request header key/values
#
def update_headers(request_headers)
@request_headers = @request_headers.merge(request_headers)
@request_headers.merge!(request_headers)
end

# Build the final request headers
Expand All @@ -70,9 +70,9 @@ def build_request_headers(request)
# * *Returns* :
# - The url string with the version pre-pended
#
def add_version(url)
url.concat("/#{@version}")
url
def add_version(url = nil)
path = @version ? "/#{@version}" : ''
url.concat(path)
end

# Add query parameters to the url
Expand All @@ -84,14 +84,8 @@ def add_version(url)
# - The url string with the query parameters appended
#
def build_query_params(url, query_params)
url.concat('?')
count = 0
query_params.each do |key, value|
url.concat('&') if count > 0
url.concat("#{key}=#{value}")
count += 1
end
url
params = query_params.map { |key, value| "#{key}=#{value}" }.join('&')
url.concat("?#{params}")
end

# Set the query params, request headers and request body
Expand Down Expand Up @@ -122,11 +116,7 @@ def build_args(args)
# - The final url string
#
def build_url(query_params: nil)
url = ''
url = add_version(url) if @version
@url_path.each do |x|
url.concat("/#{x}")
end
url = [add_version(''), *@url_path].join('/')
url = build_query_params(url, query_params) if query_params
URI.parse("#{@host}#{url}")
end
Expand Down Expand Up @@ -178,8 +168,7 @@ def make_request(http, request)
# - HTTP::NET object
#
def add_ssl(http)
protocol = host.split(':')[0]
if protocol == 'https'
if host.start_with?('https')
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
Expand Down