Module: Apipie::DSL::Common

Included in:
Concern, Controller, ResourceDescriptionDsl
Defined in:
lib/apipie/dsl_definition.rb

Instance Method Summary collapse

Instance Method Details

#_apipie_define_validators(description) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/apipie/dsl_definition.rb', line 230

def _apipie_define_validators(description)

  # [re]define method only if validation is turned on
  if description && (Apipie.configuration.validate == true ||
                     Apipie.configuration.validate == :implicitly ||
                     Apipie.configuration.validate == :explicitly)

    _apipie_save_method_params(description.method, description.params)

    unless instance_methods.include?(:apipie_validations)
      define_method(:apipie_validations) do
        method_params = self.class._apipie_get_method_params(action_name)

        if Apipie.configuration.validate_presence?
          method_params.each do |_, param|
            # check if required parameters are present
            raise ParamMissing.new(param) if param.required && !params.key?(param.name)
          end
        end

        if Apipie.configuration.validate_value?
          method_params.each do |_, param|
            # params validations
            param.validate(params[:"#{param.name}"]) if params.key?(param.name)
          end
        end

        # Only allow params passed in that are defined keys in the api
        # Auto skip the default params (format, controller, action)
        if Apipie.configuration.validate_key?
          params.reject{|k,_| %w[format controller action].include?(k.to_s) }.each_pair do |param, _|
            # params allowed
            if method_params.none? {|_,p| p.name.to_s == param.to_s}
              self.class._apipie_handle_validate_key_error params, param
            end
          end
        end

        if Apipie.configuration.process_value?
          @api_params ||= {}
          method_params.each do |_, param|
            # params processing
            @api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.key?(param.name)
          end
        end
      end
    end

    if Apipie.configuration.validate == :implicitly || Apipie.configuration.validate == true
      old_method = instance_method(description.method)

      define_method(description.method) do |*args|
        apipie_validations

        # run the original method code
        old_method.bind(self).call(*args)
      end
    end

  end
end

#_apipie_get_method_params(method) ⇒ Object



307
308
309
# File 'lib/apipie/dsl_definition.rb', line 307

def _apipie_get_method_params(method)
  @method_params[method]
end

#_apipie_handle_validate_key_error(params, param) ⇒ Object



292
293
294
295
296
297
298
299
300
# File 'lib/apipie/dsl_definition.rb', line 292

def _apipie_handle_validate_key_error params, param
  case Apipie.configuration.action_on_non_validated_keys
  when :raise
    raise UnknownParam, param
  when :skip
    params.delete(param)
    Rails.logger.warn(UnknownParam.new(param).to_s)
  end
end

#_apipie_save_method_params(method, params) ⇒ Object



302
303
304
305
# File 'lib/apipie/dsl_definition.rb', line 302

def _apipie_save_method_params(method, params)
  @method_params ||= {}
  @method_params[method] = params
end

#api_versions(*versions) ⇒ Object Also known as: api_version



157
158
159
# File 'lib/apipie/dsl_definition.rb', line 157

def api_versions(*versions)
  _apipie_dsl_data[:api_versions].concat(versions)
end

#desc(description) ⇒ Object Also known as: description, full_description

Describe the next method.

Example:

desc "print hello world"
def hello_world
  puts "hello world"
end


170
171
172
173
174
175
176
# File 'lib/apipie/dsl_definition.rb', line 170

def desc(description) #:doc:
  return unless Apipie.active_dsl?
  if _apipie_dsl_data[:description]
    raise "Double method description."
  end
  _apipie_dsl_data[:description] = description
end

#document(path) ⇒ Object

describe next method with document in given path in convension, these doc located under “#Rails.root/doc” Example: document “hello_world.md” def hello_world

puts "hello world"

end



187
188
189
190
# File 'lib/apipie/dsl_definition.rb', line 187

def document path
  content = File.open(File.join(Rails.root,  Apipie.configuration.doc_path, path)).read
  desc content
end

#error(code_or_options, desc = nil, options = {}) ⇒ Object

Describe possible errors

Example:

error :desc => "speaker is sleeping", :code => 500, :meta => [:some, :more, :data]
error 500, "speaker is sleeping"
def hello_world
  return 500 if self.speaker.sleeping?
  puts "hello world"
end


218
219
220
221
# File 'lib/apipie/dsl_definition.rb', line 218

def error(code_or_options, desc = nil, options = {}) #:doc:
  return unless Apipie.active_dsl?
  _apipie_dsl_data[:errors] << [code_or_options, desc, options]
end

#formats(formats) ⇒ Object

Describe available request/response formats

formats ['json', 'jsonp', 'xml']


195
196
197
198
# File 'lib/apipie/dsl_definition.rb', line 195

def formats(formats) #:doc:
  return unless Apipie.active_dsl?
  _apipie_dsl_data[:formats] = formats
end

#header(header_name, description, options = {}) ⇒ Object

Describe request header.

Headers can't be validated with config.validate_presence = true

Example:

header 'ClientId', "client-id"
def show
  render :text => headers['HTTP_CLIENT_ID']
end


320
321
322
323
324
325
326
327
# File 'lib/apipie/dsl_definition.rb', line 320

def header(header_name, description, options = {}) #:doc
  return unless Apipie.active_dsl?
  _apipie_dsl_data[:headers] << {
    name: header_name,
    description: description,
    options: options
  }
end

#meta(meta) ⇒ Object

Describe additional metadata

meta :author => { :name => 'John', :surname => 'Doe' }


203
204
205
# File 'lib/apipie/dsl_definition.rb', line 203

def meta(meta) #:doc:
  _apipie_dsl_data[:meta] = meta
end

#tags(*args) ⇒ Object

Add tags to resources and actions group operations together.



224
225
226
227
228
# File 'lib/apipie/dsl_definition.rb', line 224

def tags(*args)
  return unless Apipie.active_dsl?
  tags = args.length == 1 ? args.first : args
  _apipie_dsl_data[:tag_list] += tags
end