Class: Apiwork::Contract::Action
- Inherits:
-
Object
- Object
- Apiwork::Contract::Action
- Defined in:
- lib/apiwork/contract/action.rb,
lib/apiwork/contract/action/request.rb,
lib/apiwork/contract/action/response.rb
Overview
Defined Under Namespace
Instance Attribute Summary collapse
- #contract_class ⇒ Object readonly
- #name ⇒ Object readonly
Instance Method Summary collapse
-
#deprecated! ⇒ void
Marks this action as deprecated.
-
#deprecated? ⇒ Boolean
Whether this action is deprecated.
-
#description(value = nil) ⇒ String?
The description for this action.
-
#initialize(contract_class, name, replace: false) ⇒ Action
constructor
A new instance of Action.
-
#operation_id(value = nil) ⇒ String?
The operation ID for this action.
-
#raises(*error_code_keys) ⇒ void
Declares the raised error codes for this action.
-
#request(replace: false) {|request| ... } ⇒ Action::Request
Defines the request structure for this action.
- #resets_request? ⇒ Boolean
- #resets_response? ⇒ Boolean
-
#response(replace: false) {|response| ... } ⇒ Action::Response
Defines the response structure for this action.
-
#summary(value = nil) ⇒ String?
The summary for this action.
-
#tags(*tags) ⇒ Array<Symbol>?
The tags for this action.
Constructor Details
#initialize(contract_class, name, replace: false) ⇒ Action
Returns a new instance of Action.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/apiwork/contract/action.rb', line 13 def initialize(contract_class, name, replace: false) @name = name @contract_class = contract_class @reset_request = replace @reset_response = replace @request = nil @response = nil @raises = [] @summary = nil @description = nil @tags = nil @deprecated = nil @operation_id = nil end |
Instance Attribute Details
#contract_class ⇒ Object (readonly)
10 11 12 |
# File 'lib/apiwork/contract/action.rb', line 10 def contract_class @contract_class end |
#name ⇒ Object (readonly)
10 11 12 |
# File 'lib/apiwork/contract/action.rb', line 10 def name @name end |
Instance Method Details
#deprecated! ⇒ void
This method returns an undefined value.
Marks this action as deprecated.
94 95 96 |
# File 'lib/apiwork/contract/action.rb', line 94 def deprecated! @deprecated = true end |
#deprecated? ⇒ Boolean
Whether this action is deprecated.
102 103 104 |
# File 'lib/apiwork/contract/action.rb', line 102 def deprecated? @deprecated == true end |
#description(value = nil) ⇒ String?
The description for this action.
Used in generated specs as the operation description. Supports Markdown formatting.
61 62 63 64 65 |
# File 'lib/apiwork/contract/action.rb', line 61 def description(value = nil) return @description if value.nil? @description = value end |
#operation_id(value = nil) ⇒ String?
The operation ID for this action.
117 118 119 120 121 |
# File 'lib/apiwork/contract/action.rb', line 117 def operation_id(value = nil) return @operation_id if value.nil? @operation_id = value end |
#raises(*error_code_keys) ⇒ void
This method returns an undefined value.
Declares the raised error codes for this action.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/apiwork/contract/action.rb', line 139 def raises(*error_code_keys) error_code_keys = error_code_keys.flatten error_code_keys.each do |error_code_key| unless error_code_key.is_a?(Symbol) hint = error_code_key.is_a?(Integer) ? " Use :#{ErrorCode.key_for_status(error_code_key)} instead." : '' raise ConfigurationError, "raises must be symbols, got #{error_code_key.class}: #{error_code_key}.#{hint}" end next if ErrorCode.exists?(error_code_key) raise ConfigurationError, "Unknown error code :#{error_code_key}. Register it with: " \ "Apiwork::ErrorCode.register :#{error_code_key}, status: <status>" end @raises |= error_code_keys end |
#request(replace: false) {|request| ... } ⇒ Action::Request
Defines the request structure for this action.
Use the block to define query parameters and request body.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/apiwork/contract/action.rb', line 190 def request(replace: false, &block) @reset_request = replace if replace @request ||= Request.new(contract_class, name) if block block.arity.positive? ? yield(@request) : @request.instance_eval(&block) end @request end |
#resets_request? ⇒ Boolean
249 250 251 |
# File 'lib/apiwork/contract/action.rb', line 249 def resets_request? @reset_request end |
#resets_response? ⇒ Boolean
253 254 255 |
# File 'lib/apiwork/contract/action.rb', line 253 def resets_response? @reset_response end |
#response(replace: false) {|response| ... } ⇒ Action::Response
Defines the response structure for this action.
Use the block to define response body or declare no_content.
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/apiwork/contract/action.rb', line 237 def response(replace: false, &block) @reset_response = replace if replace @response ||= Response.new(contract_class, name) if block block.arity.positive? ? yield(@response) : @response.instance_eval(&block) end @response end |
#summary(value = nil) ⇒ String?
The summary for this action.
Used in generated specs as the operation summary.
41 42 43 44 45 |
# File 'lib/apiwork/contract/action.rb', line 41 def summary(value = nil) return @summary if value.nil? @summary = value end |
#tags(*tags) ⇒ Array<Symbol>?
The tags for this action.
Tags help organize actions in generated documentation.
80 81 82 83 |
# File 'lib/apiwork/contract/action.rb', line 80 def (*) @tags = .flatten if .any? @tags end |