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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/apipie/dsl_definition.rb', line 170

def _apipie_define_validators(description)
  # redefine method only if validation is turned on
  if description && Apipie.configuration.validate == true

    old_method = instance_method(description.method)

    define_method(description.method) do |*args|

      if Apipie.configuration.validate_presence?
        description.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?
        description.params.each do |_, param|
          # params validations
          param.validate(params[:"#{param.name}"]) if params.has_key?(param.name)
        end
      end

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

  end

end

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



124
125
126
# File 'lib/apipie/dsl_definition.rb', line 124

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


137
138
139
140
141
142
143
# File 'lib/apipie/dsl_definition.rb', line 137

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

#error(*args) ⇒ Object

Describe possible errors

Example:

error :desc => "speaker is sleeping", :code => 500
error 500, "speaker is sleeping"
def hello_world
  return 500 if self.speaker.sleeping?
  puts "hello world"
end


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

def error(*args) #:doc:
  return unless Apipie.active_dsl?
  _apipie_dsl_data[:errors] << args
end

#formats(formats) ⇒ Object

Describe available request/response formats

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


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

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