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



29
30
31
32
33
34
# File 'lib/zenaton/services/http.rb', line 29

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) ⇒ 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

Returns:

  • (Hash)

    the parsed json response



41
42
43
44
45
46
# File 'lib/zenaton/services/http.rb', line 41

def post(url, body)
  parsed_url = parse_url(url)
  request = Net::HTTP::Post.new(parsed_url[:uri])
  set_body_and_headers(request, post_options(body))
  make_request(request, parsed_url)
end

#put(url, body) ⇒ 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

Returns:

  • (Hash)

    the parsed json response



53
54
55
56
57
58
# File 'lib/zenaton/services/http.rb', line 53

def put(url, body)
  parsed_url = parse_url(url)
  request = Net::HTTP::Put.new(parsed_url[:uri])
  set_body_and_headers(request, put_options(body))
  make_request(request, parsed_url)
end