Class: Rooibos::Message::HttpResponse

Inherits:
Data
  • Object
show all
Includes:
Predicates
Defined in:
lib/rooibos/message/http_response.rb

Overview

Response from an HTTP command.

HTTP requests return status codes, bodies, and headers. Errors may occur. Without structured responses, handling success vs. error requires manual checks on raw data.

This response includes predicates for common checks and deconstructs for pattern matching. Include Predicates for safe predicate calls on any message.

Use it to handle Command.http completions.

Example

case msg
in { type: :http, envelope: :users, status:, body: }
  model.with(users: JSON.parse(body))
in { type: :http, envelope: :users, error: }
  model.with(error:)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Predicates

#==, #method_missing, #respond_to_missing?, #to_sym

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rooibos::Message::Predicates

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



30
31
32
# File 'lib/rooibos/message/http_response.rb', line 30

def body
  @body
end

#envelopeObject (readonly)

Returns the value of attribute envelope

Returns:

  • (Object)

    the current value of envelope



30
31
32
# File 'lib/rooibos/message/http_response.rb', line 30

def envelope
  @envelope
end

#errorObject (readonly)

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



30
31
32
# File 'lib/rooibos/message/http_response.rb', line 30

def error
  @error
end

#headersObject (readonly)

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



30
31
32
# File 'lib/rooibos/message/http_response.rb', line 30

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



30
31
32
# File 'lib/rooibos/message/http_response.rb', line 30

def status
  @status
end

Instance Method Details

#deconstruct_keys(_keys) ⇒ Object

Deconstructs for pattern matching.

Returns a hash with :type, :envelope, and either :error or :status, :body, :headers.



52
53
54
55
56
57
58
# File 'lib/rooibos/message/http_response.rb', line 52

def deconstruct_keys(_keys)
  if error
    { type: :http, envelope:, error: }
  else
    { type: :http, envelope:, status:, body:, headers: }
  end
end

#error?Boolean

Returns true if an error occurred.

Returns:

  • (Boolean)


44
45
46
# File 'lib/rooibos/message/http_response.rb', line 44

def error?
  !!error
end

#http?Boolean

Returns true for HTTP responses.

Returns:

  • (Boolean)


34
35
36
# File 'lib/rooibos/message/http_response.rb', line 34

def http?
  true
end

#success?Boolean

Returns true if status is 2xx.

Returns:

  • (Boolean)


39
40
41
# File 'lib/rooibos/message/http_response.rb', line 39

def success?
  status&.between?(200, 299)
end