class VpsbClient::HttpClient

Public Class Methods

new(curl_wrapper, protocol, hostname) click to toggle source
# File lib/vpsb_client/http_client.rb, line 3
def initialize(curl_wrapper, protocol, hostname)
  @protocol = protocol
  @hostname = hostname
  @curl_wrapper = curl_wrapper
end

Public Instance Methods

get(request) click to toggle source
# File lib/vpsb_client/http_client.rb, line 9
def get(request)
  @curl_wrapper.get(url(request))
end
post(request) click to toggle source
# File lib/vpsb_client/http_client.rb, line 13
def post(request)
  post_params = post_params(request, request.content_type)
  @curl_wrapper.post(url(request), post_params, request.content_type)
end
put(request) click to toggle source
# File lib/vpsb_client/http_client.rb, line 18
def put(request)
  put_params = put_params(request, request.content_type)
  @curl_wrapper.put(url(request), put_params, request.content_type)
end

Private Instance Methods

post_params(request, content_type) click to toggle source
# File lib/vpsb_client/http_client.rb, line 38
def post_params(request, content_type)
  post_params = request.post_params
  if request.content_type == 'application/json'
    JSON.generate(post_params) # curl doesn't do the json encoding by itself
  else
    post_params # but curl does the www form encoding
  end
end
Also aliased as: put_params
put_params(request, content_type)
Alias for: post_params
query_sep(query_string) click to toggle source
# File lib/vpsb_client/http_client.rb, line 30
def query_sep(query_string)
  query_string.empty? ? '' : '?'
end
suffix(request) click to toggle source
# File lib/vpsb_client/http_client.rb, line 34
def suffix(request)
  '.json' if request.accept == 'application/json'
end
url(request) click to toggle source
# File lib/vpsb_client/http_client.rb, line 25
def url(request)
  query_string = url_encode(request.query_params)
  "#{@protocol}://#{@hostname}#{request.url_path}#{suffix(request)}#{query_sep(query_string)}#{query_string}"
end
url_encode(params) click to toggle source
# File lib/vpsb_client/http_client.rb, line 48
def url_encode(params)
  return '' if params.empty?
  s = ''
  i = 0
  params.each do |k,v|
    s << '&' unless i==0
    s << "#{ERB::Util.url_encode(k)}=#{ERB::Util.url_encode(v)}"
    i += 1
  end
  s
end