Class: Tinker::Http::HttpClient

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

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



10
11
12
# File 'lib/tinker/http/http_client.rb', line 10

def initialize
  @timeout = 30
end

Instance Method Details

#post(url, headers: {}, body: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tinker/http/http_client.rb', line 14

def post(url, headers: {}, body: nil)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.read_timeout = @timeout
  http.open_timeout = @timeout

  request = Net::HTTP::Post.new(uri.path)
  headers.each { |key, value| request[key] = value }
  request.body = body if body

  response = http.request(request)
  Response.new(response.code.to_i, response.body, response.to_hash)
rescue StandardError => e
  raise Exception::NetworkException.new("Network error: #{e.message}", Exception::ExceptionCode::NETWORK_ERROR, e)
end