class ObjectFramework::HTTP::Parameters
Public Class Methods
Source
# File lib/object_framework/http/protocol.rb, line 161 def initialize(&declaration) @fields = [] instance_eval(&declaration) if declaration @fields.freeze freeze end
Public Instance Methods
Source
# File lib/object_framework/http/protocol.rb, line 168 def field(name, source: :body, key: name, type: String, required: true) raise ConfigurationError, "Invalid parameter source" unless %i[path query body].include?(source) raise ConfigurationError, "Duplicate parameter" if @fields.any? { |field| field.name == name } @fields << Parameter.new(name:, source:, key:, type:, required:) end
Source
# File lib/object_framework/http/protocol.rb, line 174 def normalize(input) @fields.each_with_object({}) do |field, result| source = input.public_send(field.source == :path ? :params : field.source) key = field.source == :path ? field.key.to_sym : field.key.to_s unless source.key?(key) raise InvalidInput, "Missing required parameter" if field.required next end value = source.fetch(key) if field.source != :body value = Integer(value, 10) if field.type == Integer value = Float(value) if field.type == Float end raise InvalidInput, "Invalid parameter type" unless value.is_a?(field.type) result[field.name] = value end rescue ArgumentError, TypeError raise InvalidInput, "Invalid parameter value" end