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



202
203
204
205
206
207
208
209
210
211
212
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
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/apipie/dsl_definition.rb', line 202

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.name) if param.required && !params.has_key?(param.name)
          end
        end

        if Apipie.configuration.validate_value?
          method_params.each do |_, param|
            # params validations
            param.validate(params[:"#{param.name}"]) if params.has_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_key do |param|
            # params allowed
            raise UnknownParam.new(param) if method_params.select {|_,p| p.name.to_s == param.to_s}.empty?
          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.has_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



267
268
269
# File 'lib/apipie/dsl_definition.rb', line 267

def _apipie_get_method_params(method)
  @method_params[method]
end

#_apipie_save_method_params(method, params) ⇒ Object



262
263
264
265
# File 'lib/apipie/dsl_definition.rb', line 262

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

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



136
137
138
# File 'lib/apipie/dsl_definition.rb', line 136

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


149
150
151
152
153
154
155
# File 'lib/apipie/dsl_definition.rb', line 149

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



166
167
168
169
# File 'lib/apipie/dsl_definition.rb', line 166

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


197
198
199
200
# File 'lib/apipie/dsl_definition.rb', line 197

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']


174
175
176
177
# File 'lib/apipie/dsl_definition.rb', line 174

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


280
281
282
283
284
285
286
287
# File 'lib/apipie/dsl_definition.rb', line 280

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' }


182
183
184
# File 'lib/apipie/dsl_definition.rb', line 182

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