Module: ClientApiBuilder::NetHTTP::Request

Defined in:
lib/client_api_builder/net_http_request.rb

Constant Summary collapse

METHOD_TO_NET_HTTP_CLASS =
{
  copy: Net::HTTP::Copy,
  delete: Net::HTTP::Delete,
  get: Net::HTTP::Get,
  head: Net::HTTP::Head,
  lock: Net::HTTP::Lock,
  mkcol: Net::HTTP::Mkcol,
  move: Net::HTTP::Move,
  options: Net::HTTP::Options,
  patch: Net::HTTP::Patch,
  post: Net::HTTP::Post,
  propfind: Net::HTTP::Propfind,
  proppatch: Net::HTTP::Proppatch,
  put: Net::HTTP::Put,
  trace: Net::HTTP::Trace,
  unlock: Net::HTTP::Unlock
}

Instance Method Summary collapse

Instance Method Details

#request(method:, uri:, body:, headers:, connection_options:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/client_api_builder/net_http_request.rb', line 26

def request(method:, uri:, body:, headers:, connection_options:)
  request = METHOD_TO_NET_HTTP_CLASS[method].new(uri.request_uri, headers)
  request.body = body if body

  Net::HTTP.start(uri.hostname, uri.port, connection_options.merge(use_ssl: uri.scheme == 'https')) do |http|
    http.request(request) do |response|
      yield response if block_given?
    end
  end
end

#stream(method:, uri:, body:, headers:, connection_options:) ⇒ Object



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

def stream(method:, uri:, body:, headers:, connection_options:)
  request(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options) do |response|
    response.read_body do |chunk|
      yield response, chunk
    end
  end
end

#stream_to_file(method:, uri:, body:, headers:, connection_options:, file:) ⇒ Object



51
52
53
54
55
56
# File 'lib/client_api_builder/net_http_request.rb', line 51

def stream_to_file(method:, uri:, body:, headers:, connection_options:, file:)
  mode = connection_options.delete(:file_mode) || 'wb'
  File.open(file, mode) do |io|
    stream_to_io(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options, io: io)
  end
end

#stream_to_io(method:, uri:, body:, headers:, connection_options:, io:) ⇒ Object



45
46
47
48
49
# File 'lib/client_api_builder/net_http_request.rb', line 45

def stream_to_io(method:, uri:, body:, headers:, connection_options:, io:)
  stream(method: method, uri: uri, body: body, headers: headers, connection_options: connection_options) do |_, chunk|
    io.write chunk
  end
end