Class: Zenaton::Services::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/zenaton/services/http.rb

Overview

Wrapper class around HTTParty that:

  • handles http calls

  • sets appropriate headers for each request type

  • translates exceptions into Zenaton specific ones

Constant Summary collapse

ALL_NET_HTTP_ERRORS =

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

[
  Timeout::Error,
  Errno::EINVAL,
  Errno::ECONNRESET,
  EOFError,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError
].freeze

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Hash

Makes a GET request and sets the correct headers

Parameters:

  • url (String)

    the url for the request

Returns:

  • (Hash)

    the parsed json response



31
32
33
34
35
36
# 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 = {}) ⇒ Hash

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

Parameters:

  • url (String)

    the url for the request

  • body (Hash)

    the payload to send with the request

  • headers (Hash) (defaults to: {})

    additional headers to send with the request

Returns:

  • (Hash)

    the parsed json response



44
45
46
47
48
49
# 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 = {}) ⇒ Hash

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

Parameters:

  • url (String)

    the url for the request

  • body (Hash)

    the payload to send with the request

  • headers (Hash) (defaults to: {})

    additional headers to send with the request

Returns:

  • (Hash)

    the parsed json response



57
58
59
60
61
62
# 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