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



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

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



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

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



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

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



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

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



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

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



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

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