Class: Puppet::HTTP::ResponseNetHTTP

Inherits:
Response show all
Defined in:
lib/puppet/http/response_net_http.rb

Overview

Adapts Net::HTTPResponse to Puppet::HTTP::Response

Instance Attribute Summary

Attributes inherited from Response

#url

Instance Method Summary collapse

Methods inherited from Response

#code, #drain, #reason

Constructor Details

#initialize(url, nethttp) ⇒ ResponseNetHTTP

Create a response associated with the URL.

Parameters:

  • url (URI)
  • nethttp (Net::HTTPResponse)

    The response


10
11
12
13
14
# File 'lib/puppet/http/response_net_http.rb', line 10

def initialize(url, nethttp)
  super(url, nethttp.code.to_i, nethttp.message)

  @nethttp = nethttp
end

Instance Method Details

#[](name) ⇒ String

Get a header case-insensitively.

Parameters:

  • name (String)

    The header name

Returns:

  • (String)

    The header value


34
35
36
# File 'lib/puppet/http/response_net_http.rb', line 34

def [](name)
  @nethttp[name]
end

#bodyString

Returns the entire response body. Can be used instead of

`Puppet::HTTP::Response.read_body`, but both methods cannot be used for the
same response.

Returns:

  • (String)

    Response body for the request


17
18
19
# File 'lib/puppet/http/response_net_http.rb', line 17

def body
  @nethttp.body
end

#each_header {|header, header| ... } ⇒ Object

Yield each header name and value. Returns an enumerator if no block is given.

Yield Parameters:

  • header (String)

    name

  • header (String)

    value


39
40
41
# File 'lib/puppet/http/response_net_http.rb', line 39

def each_header(&block)
  @nethttp.each_header(&block)
end

#read_body {|String| ... } ⇒ Object

Streams the response body to the caller in chunks. Can be used instead of

`Puppet::HTTP::Response.body`, but both methods cannot be used for the same
response.

Yields:

  • (String)

    Streams the response body in chunks

Raises:

  • (ArgumentError)

    raise if a block is not given


22
23
24
25
26
# File 'lib/puppet/http/response_net_http.rb', line 22

def read_body(&block)
  raise ArgumentError, "A block is required" unless block_given?

  @nethttp.read_body(&block)
end

#success?Boolean

Check if the request received a response of success (HTTP 2xx).

Returns:

  • (Boolean)

    Returns true if the response indicates success


29
30
31
# File 'lib/puppet/http/response_net_http.rb', line 29

def success?
  @nethttp.is_a?(Net::HTTPSuccess)
end