Class: NineOneOne::Http

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

Constant Summary collapse

NET_HTTP_EXCEPTIONS =

rubocop:disable Style/MutableConstant

[
  EOFError,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  SocketError,
  Zlib::GzipFile::Error
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_host, scheme = 'https') ⇒ Http

Returns a new instance of Http.



37
38
39
40
41
42
43
# File 'lib/nine_one_one/http.rb', line 37

def initialize(base_host, scheme = 'https')
  port, use_ssl = scheme == 'https' ? [443, true] : [80, false]

  @net_http = Net::HTTP.new(base_host, port)

  @net_http.use_ssl = use_ssl
end

Instance Attribute Details

#net_httpObject (readonly)

Returns the value of attribute net_http.



35
36
37
# File 'lib/nine_one_one/http.rb', line 35

def net_http
  @net_http
end

Instance Method Details

#post(path, body, headers) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nine_one_one/http.rb', line 45

def post(path, body, headers)
  post!(path, body, headers)
rescue *NET_HTTP_EXCEPTIONS => err
  err_class = if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
                SSLError
              else
                ConnectionFailedError
              end

  raise err_class, err
rescue Timeout::Error, Errno::ETIMEDOUT => err
  raise TimeoutError, err
end