Class: Apipie::Params::Descriptor::Hash

Inherits:
JsonSchema show all
Defined in:
lib/apipie/params/descriptor.rb

Defined Under Namespace

Classes: DSL

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JsonSchema

inherited, #validate!

Methods inherited from Base

find, inherited, #to_json

Constructor Details

#initialize(block, options) ⇒ Hash

Returns a new instance of Hash.



188
189
190
191
# File 'lib/apipie/params/descriptor.rb', line 188

def initialize(block, options)
  super(options)
  @dsl_data = DSL.new(&block)._apipie_params_dsl_data
end

Class Method Details

.build(argument, options, block) ⇒ Object



182
183
184
185
186
# File 'lib/apipie/params/descriptor.rb', line 182

def self.build(argument, options, block)
  if block.is_a?(::Proc) && block.arity <= 0 && argument == ::Hash
    self.new(block, options)
  end
end

Instance Method Details

#descriptionObject



204
205
206
# File 'lib/apipie/params/descriptor.rb', line 204

def description
  "Must be a Hash"
end

#fragment_descriptor(param_description, fragment) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/apipie/params/descriptor.rb', line 226

def fragment_descriptor(param_description, fragment)
  keys_path = fragment.sub(/\A#\/root\//,'').split('/')
  keys_path.delete_if { |a| a =~ /\A\d+\Z/ }
  keys_path.reduce(param_description) do |description, key|
    description.param(key)
  end
end

#invalid_param_error(param_description, error_value, errors) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/apipie/params/descriptor.rb', line 216

def invalid_param_error(param_description, error_value, errors)
  descriptions = errors.map do |error|
    fragment_descriptor(param_description, error[:fragment])
  end
  # TODO: handle multiple errors at the same time
  invalid_param = descriptions.first
  description = invalid_param.descriptor.description
  Params::Errors::Invalid.new(invalid_param, error_value, description)
end

#json_schemaObject



208
209
210
211
212
213
214
# File 'lib/apipie/params/descriptor.rb', line 208

def json_schema
  properties = params.reduce({}) do |hash, description|
    hash.update(description.name.to_s => description.descriptor.json_schema)
  end
  super.merge('type' => 'object',
              'properties' => properties)
end

#param(param_name) ⇒ Object



200
201
202
# File 'lib/apipie/params/descriptor.rb', line 200

def param(param_name)
  params.find { |param| param.name.to_s == param_name.to_s }
end

#paramsObject



193
194
195
196
197
198
# File 'lib/apipie/params/descriptor.rb', line 193

def params
  @params ||= @dsl_data.map do |name, arg, options, block|
    Description.new(name, arg, options, &block)
  end
  return @params
end