Class: Apiwork::Contract::Action::Response

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

Overview

Defines body for a response.

Returns Object via ‘body`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract_class, action_name) ⇒ Response

Returns a new instance of Response.



14
15
16
17
18
19
20
# File 'lib/apiwork/contract/action/response.rb', line 14

def initialize(contract_class, action_name)
  @contract_class = contract_class
  @action_name = action_name
  @body = nil
  @description = nil
  @no_content = false
end

Instance Attribute Details

#action_nameObject (readonly)



11
12
13
# File 'lib/apiwork/contract/action/response.rb', line 11

def action_name
  @action_name
end

#contract_classObject (readonly)



11
12
13
# File 'lib/apiwork/contract/action/response.rb', line 11

def contract_class
  @contract_class
end

Instance Method Details

#body {|body| ... } ⇒ Contract::Object

Defines the response body for this response.

Examples:

instance_eval style

body do
  integer :id
  string :title
  decimal :amount
end

yield style

body do |body|
  body.integer :id
  body.string :title
  body.decimal :amount
end

Yields:

  • block for defining body params (instance_eval style)

Yield Parameters:

Returns:



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/apiwork/contract/action/response.rb', line 92

def body(&block)
  if block
    @body ||= Object.new(
      @contract_class,
      action_name: @action_name,
      wrapped: true,
    )
    block.arity.positive? ? yield(@body) : @body.instance_eval(&block)
  end
  @body
end

#description(value = nil) ⇒ String?

The description for this response.

Metadata included in exports.

Examples:

action :show do
  response do
    description 'Returns the invoice'
  end
end

Parameters:

  • value (String, nil) (defaults to: nil)

    (nil) The description.

Returns:

  • (String, nil)


37
38
39
40
41
# File 'lib/apiwork/contract/action/response.rb', line 37

def description(value = nil)
  return @description if value.nil?

  @description = value
end

#no_content!void

This method returns an undefined value.

Declares this response as 204 No Content.

Use for actions that don’t return a response body, like DELETE or actions that only perform side effects.

Examples:

action :destroy do
  response { no_content! }
end

Archive action

action :archive do
  response { no_content! }
end


68
69
70
# File 'lib/apiwork/contract/action/response.rb', line 68

def no_content!
  @no_content = true
end

#no_content?Boolean

Whether this response has no content.

Returns:

  • (Boolean)


47
48
49
# File 'lib/apiwork/contract/action/response.rb', line 47

def no_content?
  @no_content
end