Class: Seahorse::Client::Response

Inherits:
Delegator
  • Object
show all
Defined in:
lib/seahorse/client/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :context (RequestContext) — default: nil
  • :status_code (Integer) — default: nil
  • :headers (Http::Headers) — default: Http::Headers.new
  • :body (String) — default: ''


12
13
14
15
16
17
18
19
20
21
# File 'lib/seahorse/client/response.rb', line 12

def initialize(options = {})
  @context = options[:context] || RequestContext.new
  @data = options[:data]
  @error = options[:error]
  @http_request = @context.http_request
  @http_response = @context.http_response
  @http_response.on_error do |error|
    @error = error
  end
end

Instance Attribute Details

#contextRequestContext (readonly)

Returns:



24
25
26
# File 'lib/seahorse/client/response.rb', line 24

def context
  @context
end

#dataObject

Returns The response data. This may be ‘nil` if the response contains an #error.

Returns:

  • The response data. This may be ‘nil` if the response contains an #error.



28
29
30
# File 'lib/seahorse/client/response.rb', line 28

def data
  @data
end

#errorStandardError?

Returns:

  • (StandardError, nil)


31
32
33
# File 'lib/seahorse/client/response.rb', line 31

def error
  @error
end

Instance Method Details

#__getobj__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Necessary to define as a subclass of Delegator



77
78
79
# File 'lib/seahorse/client/response.rb', line 77

def __getobj__
  @data
end

#__setobj__(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Necessary to define as a subclass of Delegator



83
84
85
# File 'lib/seahorse/client/response.rb', line 83

def __setobj__(obj)
  @data = obj
end

#checksum_validatedString?

Returns the algorithm used to validate the response checksum. Returns nil if no verification was done.

Returns:

  • (String, nil)

    returns the algorithm used to validate the response checksum. Returns nil if no verification was done.



35
36
37
# File 'lib/seahorse/client/response.rb', line 35

def checksum_validated
  context[:http_checksum][:validated] if context[:http_checksum]
end

#on(status_code, &block) ⇒ self #on(status_code_range, &block) ⇒ self

Overloads:

  • #on(status_code, &block) ⇒ self

    Parameters:

    • status_code (Integer)

      The block will be triggered only for responses with the given status code.

  • #on(status_code_range, &block) ⇒ self

    Parameters:

    • status_code_range (Range<Integer>)

      The block will be triggered only for responses with a status code that falls witin the given range.

Returns:

  • (self)


49
50
51
52
53
54
55
# File 'lib/seahorse/client/response.rb', line 49

def on(range, &_block)
  response = self
  @context.http_response.on_success(range) do
    yield response
  end
  self
end

#on_complete(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
# File 'lib/seahorse/client/response.rb', line 70

def on_complete(&block)
  @context.http_response.on_done(&block)
  self
end

#on_success(&block) ⇒ self

Yields to the block if the response has a 200 level status code.

Returns:

  • (self)


59
60
61
# File 'lib/seahorse/client/response.rb', line 59

def on_success(&block)
  on(200..299, &block)
end

#successful?Boolean

Returns ‘true` if the response is complete with a ~ 200 level http status code.

Returns:

  • (Boolean)

    Returns ‘true` if the response is complete with a ~ 200 level http status code.



65
66
67
# File 'lib/seahorse/client/response.rb', line 65

def successful?
  (200..299).cover?(@context.http_response.status_code) && @error.nil?
end