Module: Apipie::DSL::Controller

Includes:
Action, Base, Common, Param
Defined in:
lib/apipie/dsl_definition.rb

Instance Attribute Summary

Attributes included from Base

#api_params, #apipie_resource_descriptions

Instance Method Summary collapse

Methods included from Param

#_default_param_group_scope, #param, #param_group, #property, #returns

Methods included from Action

#api, #api!, #def_param_group, #example, #resource_description, #see, #show

Methods included from Common

#_apipie_define_validators, #_apipie_get_method_params, #_apipie_handle_validate_key_error, #_apipie_save_method_params, #api_versions, #desc, #document, #error, #formats, #header, #meta, #tags

Methods included from Base

#_apipie_eval_dsl

Instance Method Details

#_apipie_concern_substObject



529
530
531
532
533
534
# File 'lib/apipie/dsl_definition.rb', line 529

def _apipie_concern_subst
  @_apipie_concern_subst ||= {
    controller_path: self.controller_path,
    resource_id: Apipie.get_resource_id(self)
  }
end

#_apipie_perform_concern_subst(string) ⇒ Object



536
537
538
539
540
# File 'lib/apipie/dsl_definition.rb', line 536

def _apipie_perform_concern_subst(string)
  _apipie_concern_subst.reduce(string) do |ret, (key, val)|
    ret.gsub(":#{key}", val)
  end
end

#_apipie_update_meta(method_desc, dsl_data) ⇒ Object



508
509
510
511
512
513
# File 'lib/apipie/dsl_definition.rb', line 508

def _apipie_update_meta(method_desc, dsl_data)
  return unless dsl_data[:meta].is_a?(Hash)

  method_desc. ||= {}
  method_desc..merge!(dsl_data[:meta])
end

#_apipie_update_params(method_desc, dsl_data) ⇒ Object

Allows to update existing params after definition was made (usually needed when extending the API form plugins).

UsersController.apipie_update_params([:create, :update]) do
  param :user, Hash do
    param :oauth, String
  end
 end

The block is evaluated in scope of the controller. Ohe can pass some additional objects via additional arguments like this:

UsersController.apipie_update_params([:create, :update], [:name, :secret]) do |param_names|
  param :user, Hash do
    param_names.each { |p| param p, String }
  end
 end


501
502
503
504
505
506
# File 'lib/apipie/dsl_definition.rb', line 501

def _apipie_update_params(method_desc, dsl_data)
  params_ordered = dsl_data[:params].map do |args|
    Apipie::ParamDescription.from_dsl_data(method_desc, args)
  end
  ParamDescription.merge(method_desc.params_ordered_self, params_ordered)
end

#apipie_concern?Boolean

Returns:

  • (Boolean)


542
543
544
# File 'lib/apipie/dsl_definition.rb', line 542

def apipie_concern?
  false
end

#apipie_concern_subst(subst_hash) ⇒ Object

defines the substitutions to be made in the API paths deifned in concerns included. For example:

There is this method defined in concern:

api GET ':controller_path/:id'
def show
  # ...
end

If you include the concern into some controller, you can specify the value for :controller_path like this:

apipie_concern_subst(:controller_path => '/users')
include ::Concerns::SampleController

The resulting path will be ‘/users/:id’.

It has to be specified before the concern is included.

If not specified, the default predefined substitions are

{:conroller_path => controller.controller_path,
 :resource_id  => `resource_id_from_apipie` }


480
481
482
# File 'lib/apipie/dsl_definition.rb', line 480

def apipie_concern_subst(subst_hash)
  _apipie_concern_subst.merge!(subst_hash)
end

#apipie_update_methods(methods, *args, &block) ⇒ Object Also known as: apipie_update_params



515
516
517
518
519
520
521
522
523
524
525
# File 'lib/apipie/dsl_definition.rb', line 515

def apipie_update_methods(methods, *args, &block)
  methods.each do |method|
    method_desc = Apipie.get_method_description(self, method)
    unless method_desc
      raise "Could not find method description for #{self}##{method}. Was the method defined?"
    end
    dsl_data = _apipie_eval_dsl(*args, &block)
    _apipie_update_params(method_desc, dsl_data)
    _apipie_update_meta(method_desc, dsl_data)
  end
end

#method_added(method_name) ⇒ Object

create method api and redefine newly added method



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/apipie/dsl_definition.rb', line 547

def method_added(method_name) #:doc:
  super
  return if !Apipie.active_dsl? || !_apipie_dsl_data[:api]

  return if _apipie_dsl_data[:api_args].blank? && _apipie_dsl_data[:api_from_routes].blank?

  # remove method description if exists and create new one
  Apipie.remove_method_description(self, _apipie_dsl_data[:api_versions], method_name)
  description = Apipie.define_method_description(self, method_name, _apipie_dsl_data)

  _apipie_dsl_data_clear
  _apipie_define_validators(description)
ensure
  _apipie_dsl_data_clear
end