Sha256: 2b47e40fd8fc889040eba3a4f104276365e0abe89dcb3d032f128902cc0b5c04

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'ixtlan/babel/factory'
module CubaApi
  module InputFilter

    module ClassMethods
      def factory
        @_factory ||= Ixtlan::Babel::Factory.new
      end
    end

    def new_instance( clazz, context = nil )
      clazz.new( params( clazz, context ) )
    end
    
    def params( clazz = nil, context = nil )
      filter_params_and_keeps( clazz, context )
      @_data[ 0 ] || {}
    end

    def keeps( clazz = nil, context = nil )
      filter_params_and_keeps( clazz, context )
      @_data[ 1 ] || {}
    end

    def filter_params_and_keeps( clazz, context )
      if clazz
        @_data ||= 
          begin
            filter = self.class.factory.new_filter( clazz ).use( context )
            filter.filter_it( parse_request_body )
          end
      end
      @_data ||= {}
    end
    private :filter_params_and_keeps

    def parse_request_body
      if env[ 'CONTENT_TYPE' ] == 'application/json'
        JSON.parse( req.body.read )
      else
        {}
      end
    end
    protected :parse_request_body
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cuba-api-0.3.0 lib/cuba_api/input_filter.rb
cuba-api-0.2.0 lib/cuba_api/input_filter.rb