Class: HaveAPI::Action
- Includes:
- Hookable
- Defined in:
- lib/haveapi/action.rb
Direct Known Subclasses
HaveAPI::Actions::Default::Create, HaveAPI::Actions::Default::Delete, HaveAPI::Actions::Default::Index, HaveAPI::Actions::Default::Show, HaveAPI::Actions::Default::Update, Resources::ActionState::Cancel, Resources::ActionState::Poll
Constant Summary collapse
- PATH_PARAM_PATTERN =
/\{([a-zA-Z0-9\-_]+)\}/
Class Attribute Summary collapse
- .action_name ⇒ Object
-
.authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
.examples ⇒ Object
readonly
Returns the value of attribute examples.
-
.resource ⇒ Object
Returns the value of attribute resource.
Instance Attribute Summary collapse
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .add_pre_authorize_blocks(authorization, context) ⇒ Object
- .authorize ⇒ Object
- .build_route(prefix) ⇒ Object
- .delayed_inherited(subclass) ⇒ Object
- .describe(context) ⇒ Object
- .example(title = '') ⇒ Object
- .from_context(c) ⇒ Object
-
.inherit_attrs_from_resource(action, r, attrs) ⇒ Object
Inherit attributes from resource action is defined in.
- .inherited(subclass) ⇒ Object
-
.initialize ⇒ Object
rubocop:disable Lint/MissingSuper.
- .input(layout = nil, namespace: nil, &block) ⇒ Object
- .meta(type = :object, &block) ⇒ Object
- .model_adapter(layout) ⇒ Object
- .output(layout = nil, namespace: nil, &block) ⇒ Object
- .parameter_metadata_i18n_items(context) ⇒ Object
- .path_param_names(path) ⇒ Object
- .path_params(path, args) ⇒ Object
- .resolve_path_params(object) ⇒ Object
- .validate_build(path: nil) ⇒ Object
Instance Method Summary collapse
- #authorized?(user) ⇒ Boolean
-
#exec ⇒ Object
This method must be reimplemented in every action.
-
#initialize(request, version, params, input_params, context) ⇒ Action
constructor
A new instance of Action.
- #input ⇒ Object
- #meta ⇒ Object
- #path_params ⇒ Object
- #pre_exec ⇒ Object
-
#prepare ⇒ Object
Prepare object, set instance variables from URL parameters.
-
#safe_exec ⇒ Object
Calls exec while catching all exceptions and restricting output only to what user can see.
- #safe_output(ret) ⇒ Object
- #set_meta(hash) ⇒ Object
- #v?(v) ⇒ Boolean
- #validate! ⇒ Object
Methods included from Hookable
Methods inherited from Common
check_build, has_attr, inherit_attrs
Constructor Details
#initialize(request, version, params, input_params, context) ⇒ Action
Returns a new instance of Action.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/haveapi/action.rb', line 324 def initialize(request, version, params, input_params, context) super() @request = request @version = version @route_params = params.dup @raw_input = input_params || {} @context = context @context.action = self.class @context.action_instance = self @metadata = {} @reply_meta = { object: {}, global: {} } @flags = {} class_auth = self.class. @authorization = if class_auth class_auth.clone else Authorization.new {} end self.class.(@authorization, @context) end |
Class Attribute Details
.action_name ⇒ Object
197 198 199 |
# File 'lib/haveapi/action.rb', line 197 def action_name (@action_name ? @action_name.to_s : to_s).demodulize end |
.authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
46 47 48 |
# File 'lib/haveapi/action.rb', line 46 def @authorization end |
.examples ⇒ Object (readonly)
Returns the value of attribute examples.
46 47 48 |
# File 'lib/haveapi/action.rb', line 46 def examples @examples end |
.resource ⇒ Object
Returns the value of attribute resource.
45 46 47 |
# File 'lib/haveapi/action.rb', line 45 def resource @resource end |
Instance Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
41 42 43 |
# File 'lib/haveapi/action.rb', line 41 def current_user @current_user end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
41 42 43 |
# File 'lib/haveapi/action.rb', line 41 def errors @errors end |
#flags ⇒ Object
Returns the value of attribute flags.
42 43 44 |
# File 'lib/haveapi/action.rb', line 42 def flags @flags end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
41 42 43 |
# File 'lib/haveapi/action.rb', line 41 def @message end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
41 42 43 |
# File 'lib/haveapi/action.rb', line 41 def request @request end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
41 42 43 |
# File 'lib/haveapi/action.rb', line 41 def version @version end |
Class Method Details
.add_pre_authorize_blocks(authorization, context) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/haveapi/action.rb', line 311 def (, context) ret = Action.call_hooks( :pre_authorize, args: [context], initial: { blocks: [] } ) ret[:blocks].reverse_each do |block| .prepend_block(block) end end |
.authorize ⇒ Object
186 187 188 |
# File 'lib/haveapi/action.rb', line 186 def (&) @authorization = Authorization.new(&) end |
.build_route(prefix) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/haveapi/action.rb', line 203 def build_route(prefix) route = @route || action_name.underscore if !route.is_a?(String) && route.respond_to?(:call) route = route.call(resource) end prefix + format(route, resource: resource.resource_name.underscore) end |
.delayed_inherited(subclass) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/haveapi/action.rb', line 59 def delayed_inherited(subclass) resource = subclass.resource || Kernel.const_get(subclass.to_s.deconstantize) inherit_attrs(subclass) inherit_attrs_from_resource(subclass, resource, [:auth]) i = @input.clone i.action = subclass o = @output.clone o.action = subclass m = {} @meta.each do |k, v| m[k] = v && v.clone next unless v m[k].action = subclass end subclass.instance_variable_set(:@input, i) subclass.instance_variable_set(:@output, o) subclass.instance_variable_set(:@meta, m) begin subclass.instance_variable_set(:@resource, resource) subclass.instance_variable_set(:@model, resource.model) resource.action_defined(subclass) rescue NoMethodError nil end end |
.describe(context) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/haveapi/action.rb', line 213 def describe(context) = (@authorization && @authorization.clone) || Authorization.new {} (, context) if (context.endpoint || context.current_user) \ && !.(context.current_user, context.path_params_from_args) return false end route_method = context.action.http_method.to_s.upcase context. = if context.endpoint context.action_instance = context.action.from_context(context) ret = catch(:return) do context.action_prepare = context.action_instance.prepare end return false if ret == false end { auth: @auth, description: HaveAPI.localize(@desc), aliases: @aliases, blocking: @blocking ? true : false, input: @input ? @input.describe(context) : { parameters: {} }, output: @output ? @output.describe(context) : { parameters: {} }, meta: @meta ? @meta.to_h { |type, v| [type, v && v.describe(context, type:)] } : nil, examples: @examples ? @examples.describe(context) : [], scope: context.action_scope, path: context.resolved_path, method: route_method, help: "#{context.path}?method=#{route_method}" } end |
.example(title = '') ⇒ Object
190 191 192 193 194 195 |
# File 'lib/haveapi/action.rb', line 190 def example(title = '', &) @examples ||= ExampleList.new e = Example.new(title) e.instance_eval(&) @examples << e end |
.from_context(c) ⇒ Object
274 275 276 277 278 279 280 281 282 283 |
# File 'lib/haveapi/action.rb', line 274 def from_context(c) ret = new(nil, c.version, c.path_params || c.path_params_from_args, c.input || c.params || {}, c) ret.instance_exec do @safe_input = self.class.input && input_from_params(raw_input_params(self.class.input)) @authorization = c. @current_user = c.current_user end ret end |
.inherit_attrs_from_resource(action, r, attrs) ⇒ Object
Inherit attributes from resource action is defined in.
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/haveapi/action.rb', line 262 def inherit_attrs_from_resource(action, r, attrs) begin return unless r.obj_type == :resource rescue NoMethodError return end attrs.each do |attr| action.method(attr).call(r.method(attr).call) end end |
.inherited(subclass) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/haveapi/action.rb', line 48 def inherited(subclass) # puts "Action.inherited called #{subclass} from #{to_s}" super subclass.instance_variable_set(:@obj_type, obj_type) return unless subclass.name # not an anonymouse class delayed_inherited(subclass) end |
.initialize ⇒ Object
rubocop:disable Lint/MissingSuper
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/haveapi/action.rb', line 93 def initialize # rubocop:disable Lint/MissingSuper return if @initialized check_build("#{self}.input") do input.exec model_adapter(input.layout).load_validators(model, input) if model end check_build("#{self}.output") do output.exec end model_adapter(input.layout).used_by(:input, self) model_adapter(output.layout).used_by(:output, self) if blocking (:global) do output do integer :action_state_id, label: HaveAPI.('haveapi.parameters.action_state_id.label'), desc: HaveAPI.('haveapi.parameters.action_state_id.description') end end end if @meta @meta.each_value do |m| next unless m check_build("#{self}.meta.input") do m.input && m.input.exec end check_build("#{self}.meta.output") do m.output && m.output.exec end end end @initialized = true end |
.input(layout = nil, namespace: nil, &block) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/haveapi/action.rb', line 153 def input(layout = nil, namespace: nil, &block) if block @input ||= Params.new(:input, self) @input.layout = layout @input.namespace = namespace @input.add_block(block) else @input end end |
.meta(type = :object, &block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/haveapi/action.rb', line 175 def (type = :object, &block) if block @meta ||= { object: nil, global: nil } @meta[type] ||= Metadata::ActionMetadata.new @meta[type].action = self @meta[type].instance_exec(&block) else @meta[type] end end |
.model_adapter(layout) ⇒ Object
149 150 151 |
# File 'lib/haveapi/action.rb', line 149 def model_adapter(layout) ModelAdapter.for(layout, resource.model) end |
.output(layout = nil, namespace: nil, &block) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/haveapi/action.rb', line 164 def output(layout = nil, namespace: nil, &block) if block @output ||= Params.new(:output, self) @output.layout = layout @output.namespace = namespace @output.add_block(block) else @output end end |
.parameter_metadata_i18n_items(context) ⇒ Object
251 252 253 254 255 256 257 258 259 |
# File 'lib/haveapi/action.rb', line 251 def (context) [ *@input&.(context), *@output&.(context), *(@meta&.flat_map do |type, | &.(context, type:) end) ].compact end |
.path_param_names(path) ⇒ Object
294 295 296 |
# File 'lib/haveapi/action.rb', line 294 def path_param_names(path) path.scan(PATH_PARAM_PATTERN).map(&:first) end |
.path_params(path, args) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/haveapi/action.rb', line 298 def path_params(path, args) values = args.is_a?(Array) ? args.dup : [args] params = {} path_param_names(path).each do |name| value = values.shift.to_s params[name] = value params[name.to_sym] = value end params end |
.resolve_path_params(object) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/haveapi/action.rb', line 285 def resolve_path_params(object) if resolve resolve.call(object) else object.respond_to?(:id) ? object.id : nil end end |
.validate_build(path: nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/haveapi/action.rb', line 135 def validate_build(path: nil) check_build("#{self}.input") do input.validate_build end check_build("#{self}.output") do output.validate_build end check_build("#{self}.examples") do @examples&.validate_build(self, path:) end end |
Instance Method Details
#authorized?(user) ⇒ Boolean
358 359 360 361 |
# File 'lib/haveapi/action.rb', line 358 def (user) @current_user = user @authorization.(user, path_params) end |
#exec ⇒ Object
This method must be reimplemented in every action. It must not be invoked directly, only via safe_exec, which restricts output.
394 395 396 |
# File 'lib/haveapi/action.rb', line 394 def exec ['not implemented'] end |
#input ⇒ Object
367 368 369 370 371 |
# File 'lib/haveapi/action.rb', line 367 def input return unless self.class.input @safe_input end |
#meta ⇒ Object
373 374 375 |
# File 'lib/haveapi/action.rb', line 373 def @metadata end |
#path_params ⇒ Object
363 364 365 |
# File 'lib/haveapi/action.rb', line 363 def path_params @path_params ||= extract_path_params end |
#pre_exec ⇒ Object
390 |
# File 'lib/haveapi/action.rb', line 390 def pre_exec; end |
#prepare ⇒ Object
Prepare object, set instance variables from URL parameters. This method should return queried object. If the method is not implemented or returns nil, action description will not contain link to an associated resource.
FIXME: is this correct behaviour? ++
388 |
# File 'lib/haveapi/action.rb', line 388 def prepare; end |
#safe_exec ⇒ Object
Calls exec while catching all exceptions and restricting output only
to what user can see.
Return array [status, data|error, errors]
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/haveapi/action.rb', line 401 def safe_exec exec_ret = catch(:return) do validate! prepare pre_exec exec rescue Exception => e # rubocop:disable Lint/RescueException tmp = call_class_hooks_as_for(Action, :exec_exception, args: [@context, e]) if tmp.empty? p e. puts e.backtrace error!(HaveAPI.('haveapi.errors.server_error'), {}, http_status: 500) end unless tmp[:status] error!(tmp[:message], {}, http_status: tmp[:http_status] || 500) end end begin output_ret = safe_output(exec_ret) rescue Exception => e # rubocop:disable Lint/RescueException tmp = call_class_hooks_as_for(Action, :exec_exception, args: [@context, e]) p e. puts e.backtrace return [ tmp[:status] || false, tmp[:message] || HaveAPI.('haveapi.errors.server_error'), {}, tmp[:http_status] || 500 ] end output_ret end |
#safe_output(ret) ⇒ Object
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/haveapi/action.rb', line 444 def safe_output(ret) if ret output = self.class.output if output safe_ret = nil adapter = self.class.model_adapter(output.layout) out_params = self.class.output.params case output.layout when :object out = adapter.output(@context, ret) safe_ret = @authorization.filter_output( out_params, out, true ) @reply_meta[:global].update((out., safe_ret)) when :object_list safe_ret = [] ret.each do |obj| out = adapter.output(@context, obj) safe_ret << @authorization.filter_output( out_params, out, true ) next if [:no] safe_ret.last.update({ Metadata.namespace => (out., safe_ret.last) }) end when :hash safe_ret = @authorization.filter_output( out_params, adapter.output(@context, ret), true ) when :hash_list safe_ret = ret.map do |hash| @authorization.filter_output( out_params, adapter.output(@context, hash), true ) end else safe_ret = ret end if self.class.blocking @reply_meta[:global][:action_state_id] = state_id end ns = { output.namespace => safe_ret } ns[Metadata.namespace] = unless [:no] [true, ns] else [true, {}] end else [false, @message, @errors, @http_status] end end |
#set_meta(hash) ⇒ Object
377 378 379 |
# File 'lib/haveapi/action.rb', line 377 def (hash) @reply_meta[:global].update(hash) end |
#validate! ⇒ Object
348 349 350 351 352 353 354 355 356 |
# File 'lib/haveapi/action.rb', line 348 def validate! validate rescue ValidationError => e opts = {} status = @context.server.validation_error_http_status opts[:http_status] = status if status error!(e., e.to_hash, opts) end |