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, HaveAPI::Authentication::Token::Resources::Token::Renew, HaveAPI::Authentication::Token::Resources::Token::Request, HaveAPI::Authentication::Token::Resources::Token::Revoke, Resources::ActionState::Cancel, Resources::ActionState::Poll
Class Attribute Summary collapse
-
.authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
.examples ⇒ Object
readonly
Returns the value of attribute examples.
-
.resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Attribute Summary collapse
-
#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.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .authorize(&block) ⇒ Object
- .build_route(prefix) ⇒ Object
- .delayed_inherited(subclass) ⇒ Object
- .describe(context) ⇒ Object
- .example(title = '', &block) ⇒ 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
- .input(layout = nil, namespace: nil, &block) ⇒ Object
- .meta(type = :object, &block) ⇒ Object
- .model_adapter(layout) ⇒ Object
- .output(layout = nil, namespace: nil, &block) ⇒ Object
- .resolve_url_params(object) ⇒ Object
- .validate_build ⇒ Object
Instance Method Summary collapse
- #authorized?(user) ⇒ Boolean
- #current_user ⇒ Object
-
#exec ⇒ Object
This method must be reimplemented in every action.
-
#initialize(request, version, params, body, context) ⇒ Action
constructor
A new instance of Action.
- #input ⇒ Object
- #meta ⇒ Object
- #params ⇒ Object
- #pre_exec ⇒ Object
-
#prepare ⇒ Object
Prepare object, set instance variables from URL parameters.
- #request ⇒ Object
-
#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, body, context) ⇒ Action
Returns a new instance of Action.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/haveapi/action.rb', line 254 def initialize(request, version, params, body, context) @request = request @version = version @params = params @params.update(body) if body @context = context @context.action = self.class @context.action_instance = self = {} = {object: {}, global: {}} @flags = {} class_auth = self.class. if class_auth = class_auth.clone else = Authorization.new {} end end |
Class Attribute Details
.authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
30 31 32 |
# File 'lib/haveapi/action.rb', line 30 def end |
.examples ⇒ Object (readonly)
Returns the value of attribute examples.
30 31 32 |
# File 'lib/haveapi/action.rb', line 30 def examples @examples end |
.resource ⇒ Object (readonly)
Returns the value of attribute resource.
30 31 32 |
# File 'lib/haveapi/action.rb', line 30 def resource @resource end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
26 27 28 |
# File 'lib/haveapi/action.rb', line 26 def errors @errors end |
#flags ⇒ Object
Returns the value of attribute flags.
27 28 29 |
# File 'lib/haveapi/action.rb', line 27 def flags @flags end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
26 27 28 |
# File 'lib/haveapi/action.rb', line 26 def end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
26 27 28 |
# File 'lib/haveapi/action.rb', line 26 def version @version end |
Class Method Details
.authorize(&block) ⇒ Object
165 166 167 |
# File 'lib/haveapi/action.rb', line 165 def (&block) = Authorization.new(&block) end |
.build_route(prefix) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/haveapi/action.rb', line 176 def build_route(prefix) route = @route || to_s.demodulize.underscore if !route.is_a?(String) && route.respond_to?(:call) route = route.call(self.resource) end prefix + route % {resource: self.resource.to_s.demodulize.underscore} end |
.delayed_inherited(subclass) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/haveapi/action.rb', line 43 def delayed_inherited(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 = {} .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) rescue NoMethodError return end end |
.describe(context) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/haveapi/action.rb', line 186 def describe(context) = ( && .clone) || Authorization.new return false if (context.endpoint || context.current_user) && !.(context.current_user) 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: @desc, aliases: @aliases, blocking: @blocking ? true : false, input: @input ? @input.describe(context) : {parameters: {}}, output: @output ? @output.describe(context) : {parameters: {}}, meta: ? .merge() { |_, v| v && v.describe(context) } : nil, examples: @examples ? @examples.map { |e| e.describe } : [], url: context.resolved_url, method: route_method, help: "#{context.url}?method=#{route_method}" } end |
.example(title = '', &block) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/haveapi/action.rb', line 169 def example(title = '', &block) @examples ||= [] e = Example.new(title) e.instance_eval(&block) @examples << e end |
.from_context(c) ⇒ Object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/haveapi/action.rb', line 233 def from_context(c) ret = new(nil, c.version, c.params, nil, c) ret.instance_exec do @safe_params = @params.dup = 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.
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/haveapi/action.rb', line 220 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/haveapi/action.rb', line 32 def inherited(subclass) # puts "Action.inherited called #{subclass} from #{to_s}" subclass.instance_variable_set(:@obj_type, obj_type) if subclass.name # not an anonymouse class delayed_inherited(subclass) end end |
.initialize ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/haveapi/action.rb', line 75 def initialize 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: 'Action state ID', desc: 'ID of ActionState object for state querying. When null, the action '+ 'is not blocking for the current invocation.' end end end if .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
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/haveapi/action.rb', line 132 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
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/haveapi/action.rb', line 154 def (type = :object, &block) if block ||= {object: nil, global: nil} [type] ||= Metadata::ActionMetadata.new [type].action = self [type].instance_exec(&block) else [type] end end |
.model_adapter(layout) ⇒ Object
128 129 130 |
# File 'lib/haveapi/action.rb', line 128 def model_adapter(layout) ModelAdapter.for(layout, resource.model) end |
.output(layout = nil, namespace: nil, &block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/haveapi/action.rb', line 143 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 |
.resolve_url_params(object) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/haveapi/action.rb', line 244 def resolve_url_params(object) if self.resolve self.resolve.call(object) else object.respond_to?(:id) ? object.id : nil end end |
.validate_build ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/haveapi/action.rb', line 118 def validate_build check_build("#{self}.input") do input.validate_build end check_build("#{self}.output") do output.validate_build end end |
Instance Method Details
#authorized?(user) ⇒ Boolean
283 284 285 286 |
# File 'lib/haveapi/action.rb', line 283 def (user) @current_user = user .(user) end |
#current_user ⇒ Object
288 289 290 |
# File 'lib/haveapi/action.rb', line 288 def current_user @current_user end |
#exec ⇒ Object
This method must be reimplemented in every action. It must not be invoked directly, only via safe_exec, which restricts output.
329 330 331 |
# File 'lib/haveapi/action.rb', line 329 def exec ['not implemented'] end |
#input ⇒ Object
296 297 298 |
# File 'lib/haveapi/action.rb', line 296 def input @safe_params[ self.class.input.namespace ] if self.class.input end |
#meta ⇒ Object
304 305 306 |
# File 'lib/haveapi/action.rb', line 304 def end |
#params ⇒ Object
292 293 294 |
# File 'lib/haveapi/action.rb', line 292 def params @safe_params end |
#pre_exec ⇒ Object
323 324 325 |
# File 'lib/haveapi/action.rb', line 323 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? ++
319 320 321 |
# File 'lib/haveapi/action.rb', line 319 def prepare end |
#request ⇒ Object
300 301 302 |
# File 'lib/haveapi/action.rb', line 300 def request @request 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]
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/haveapi/action.rb', line 336 def safe_exec ret = catch(:return) do begin validate! prepare pre_exec exec rescue Exception => e tmp = call_class_hooks_as_for(Action, :exec_exception, args: [@context, e]) if tmp.empty? p e. puts e.backtrace error('Server error occurred') end unless tmp[:status] error(tmp[:message], {}, http_status: tmp[:http_status] || 500) end end end safe_output(ret) end |
#safe_output(ret) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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 |
# File 'lib/haveapi/action.rb', line 365 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 = .filter_output( out_params, out, true ) [:global].update(out.) when :object_list safe_ret = [] ret.each do |obj| out = adapter.output(@context, obj) safe_ret << .filter_output( out_params, out, true ) safe_ret.last.update({Metadata.namespace => out.}) unless [:no] end when :hash safe_ret = .filter_output( out_params, adapter.output(@context, ret), true ) when :hash_list safe_ret = ret safe_ret.map! do |hash| .filter_output( out_params, adapter.output(@context, hash), true ) end else safe_ret = ret end if self.class.blocking [:global][:action_state_id] = state_id end ns = {output.namespace => safe_ret} ns[Metadata.namespace] = [:global] unless [:no] [true, ns] else [true, {}] end else [false, , @errors, @http_status] end end |
#set_meta(hash) ⇒ Object
308 309 310 |
# File 'lib/haveapi/action.rb', line 308 def (hash) [:global].update(hash) end |
#validate! ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/haveapi/action.rb', line 275 def validate! begin @params = validate rescue ValidationError => e error(e., e.to_hash) end end |