class Zenaton::Services::Http

Wrapper class around HTTParty that:

Constants

ALL_NET_HTTP_ERRORS

Net::HTTP errors translated into a Zenaton::ConnectionError

Public Instance Methods

get(url) click to toggle source

Makes a GET request and sets the correct headers

@param url [String] the url for the request @return [Hash] the parsed json response

# File lib/zenaton/services/http.rb, line 31
def get(url)
  parsed_url = parse_url(url)
  request = Net::HTTP::Get.new(parsed_url[:uri])
  set_body_and_headers(request, default_options)
  make_request(request, parsed_url)
end
post(url, body, headers = {}) click to toggle source

Makes a POST request with some data and sets the correct headers

@param url [String] the url for the request @param body [Hash] the payload to send with the request @param headers [Hash] additional headers to send with the request @return [Hash] the parsed json response

# File lib/zenaton/services/http.rb, line 44
def post(url, body, headers = {})
  parsed_url = parse_url(url)
  request = Net::HTTP::Post.new(parsed_url[:uri])
  set_body_and_headers(request, post_options(body, headers))
  make_request(request, parsed_url)
end
put(url, body, headers = {}) click to toggle source

Makes a PUT request with some data and sets the correct headers

@param url [String] the url for the request @param body [Hash] the payload to send with the request @param headers [Hash] additional headers to send with the request @return [Hash] the parsed json response

# File lib/zenaton/services/http.rb, line 57
def put(url, body, headers = {})
  parsed_url = parse_url(url)
  request = Net::HTTP::Put.new(parsed_url[:uri])
  set_body_and_headers(request, put_options(body, headers))
  make_request(request, parsed_url)
end

Private Instance Methods

default_options() click to toggle source
# File lib/zenaton/services/http.rb, line 105
def default_options
  {
    headers: { 'Accept' => 'application/json' }
  }
end
errors?(response) click to toggle source
# File lib/zenaton/services/http.rb, line 96
def errors?(response)
  response.code.to_i >= 400
end
format_error(response) click to toggle source
# File lib/zenaton/services/http.rb, line 100
def format_error(response)
  message = JSON.parse(response.read_body)['error']
  "#{response.code}: #{message}"
end
get_response(request, parsed_url) click to toggle source
# File lib/zenaton/services/http.rb, line 86
def get_response(request, parsed_url)
  Net::HTTP.start(
    parsed_url[:uri].hostname,
    parsed_url[:uri].port,
    use_ssl: parsed_url[:use_ssl]
  ) do |http|
    http.request(request)
  end
end
make_request(request, parsed_url) click to toggle source
# File lib/zenaton/services/http.rb, line 78
def make_request(request, parsed_url)
  res = get_response(request, parsed_url)
  raise Zenaton::InternalError, format_error(res) if errors?(res)
  JSON.parse(res.body)
rescue *ALL_NET_HTTP_ERRORS
  raise Zenaton::ConnectionError
end
parse_url(url) click to toggle source
# File lib/zenaton/services/http.rb, line 66
def parse_url(url)
  uri = URI.parse(url)
  { uri: uri, use_ssl: uri.scheme == 'https' }
end
post_options(body, headers) click to toggle source
# File lib/zenaton/services/http.rb, line 111
def post_options(body, headers)
  default_post_headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
  }
  {
    body: body,
    headers: default_post_headers.merge(headers)
  }
end
Also aliased as: put_options
put_options(body, headers)
Alias for: post_options
set_body_and_headers(request, options) click to toggle source
# File lib/zenaton/services/http.rb, line 71
def set_body_and_headers(request, options)
  options[:headers].each do |key, value|
    request[key] = value
  end
  request.body = options[:body].to_json if options[:body]
end