Module: RazorRisk::Cassini::Applications::RESTFramework::RouteVerbDispatch

Includes:
Pantheios, Xqsr3::Quality::ParameterChecking
Defined in:
lib/razor_risk/cassini/applications/rest_framework/route_verb_dispatcher.rb

Overview

########################################################################## modules

Instance Method Summary collapse

Instance Method Details

#dispatch(supplier, *args, **options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/razor_risk/cassini/applications/rest_framework/route_verb_dispatcher.rb', line 39

def dispatch supplier, *args, **options

    trace ParamNames[ :supplier, :args, :options ], supplier, args, options

    if ::Class === supplier

        supplier_class  =   supplier
        supplier        =   supplier_class.new(self, self.class, *args, **options)
    else

        supplier_class  =   supplier.class
    end

    if $PARAMETER_CHECKING

        check_parameter supplier, 'supplier', responds_to: :handle
    end

    verb    =   (options[:verb] || request.request_method).to_s.downcase.to_sym

    # validate 'Accept'

    validate_accept(request, supplier_class::HTTP_ACCEPTS) if supplier_class.const_defined?(:HTTP_ACCEPTS)

    # validate 'Content-Type'

    validate_content_type(request, supplier_class::HTTP_CONTENT_TYPES) if supplier_class.const_defined?(:HTTP_CONTENT_TYPES)

    # validate query parameters

    validated_params = validate_query_parameters(params, supplier_class.const_defined?(:QUERY_PARAMETERS) ?  supplier_class::QUERY_PARAMETERS : nil, supplier_class.const_defined?(:ROUTE_VARIABLES) ?  supplier_class::ROUTE_VARIABLES : nil)

    # handle base-64 for all params

    validated_params = validated_params.merge(validated_params) do |k, o, n|

        case n
        when ::String

            s   =   n.strip

            if /^===(.+)===$/ =~ s

                e   =   $1

                begin

                    Base64.strict_decode64(e).chomp
                rescue ::ArgumentError => x

                    halt(*[ 422, {}, "value for query parameter '#{k}' is malformed Base-64: a valid Base-64 string must be prefixed and suffixed by '==='" ])
                end
            else

                n
            end
        else

            n
        end
    end

    # execute handler
    resp = supplier.handle(env, validated_params, request, response)

    # TODO: post-process result, in terms of converting to 'Accept' type
    # and calling halt / status appropriately

    resp
end