Class: Apiwork::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/response.rb

Overview

Immutable value object representing a response.

Encapsulates body parameters. Transformations return new instances, preserving immutability.

Examples:

Creating a response

response = Response.new(body: { id: 1, title: "Hello" })
response.body # => { id: 1, title: "Hello" }

Transforming keys

response.transform { |data| camelize(data) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:) ⇒ Response

Creates a new response context.

Parameters:

  • body (Hash)

    The body parameters.



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

def initialize(body:)
  @body = body
end

Instance Attribute Details

#bodyHash (readonly)

The body for this response.

Returns:

  • (Hash)


21
22
23
# File 'lib/apiwork/response.rb', line 21

def body
  @body
end

Instance Method Details

#transform {|Hash| ... } ⇒ Response

Transforms the body parameters.

Examples:

response.transform { |data| camelize(data) }

Yields:

  • (Hash)

    the body parameters

Returns:



40
41
42
# File 'lib/apiwork/response.rb', line 40

def transform
  self.class.new(body: yield(body))
end

#transform_body {|Hash| ... } ⇒ Response

Transforms the body parameters.

Examples:

response.transform_body { |data| camelize(data) }

Yields:

  • (Hash)

    the body parameters

Returns:



52
53
54
# File 'lib/apiwork/response.rb', line 52

def transform_body
  self.class.new(body: yield(body))
end