Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::Portfolios::HierarchyGet

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Pantheios, Utilities::CollectionGetHelper, Utilities::Portfolios, Util::ConversionUtil, RazorRisk::Core::Diagnostics::Exceptions, RazorRisk::Core::Diagnostics::Logger, Razor::Connectivity::EntityConnectors::Exceptions, Razor::Connectivity::Razor3, Razor::Connectivity::Razor3::EntityConnectors
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/portfolios/hierarchy_get.rb

Overview

########################################################################## classes

Constant Summary collapse

HTTP_VERB =
:get
HTTP_ACCEPTS =
%w{ application/xml application/json text/xml }
QUERY_PARAMETERS =
%w{

    filter-name

    page-base
    page-extent

    key-search
}

Instance Method Summary collapse

Methods included from Utilities::Portfolios

#validate_request_body

Instance Method Details

#handle(env, params, request, response) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/portfolios/hierarchy_get.rb', line 72

def handle env, params, request, response

    trace ParamNames[ :env, :params, :request, :response ], env, params, request, response
    # params


    filter_name = params['filter-name'].to_s.strip
    filter_name = nil if filter_name.empty?
    page_base   = ::Xqsr3::Conversion::IntegerParser.to_integer(
        params['page-base'] || 0
    ) do |x, arg|
        halt 422, {'Content-Type' => 'text/plain'}, "invalid page specifier: 'page-base'=#{arg}"
    end
    page_extent = ::Xqsr3::Conversion::IntegerParser.to_integer(
        params['page-extent'] || 1000
    ) do |x, arg|
        halt 422, {'Content-Type' => 'text/plain'}, "invalid page specifier: 'page-extent'=#{arg}"
    end
    key_search = params['key-search']
    key_search = nil if key_search.to_s.strip.empty?

    if page_base < 0 || page_extent < 1

        halt 416, {'Content-Type' => 'text/plain'}, "invalid page specifier: 'page-base'=#{page_base}; 'page-extent'=#{page_extent}"
    end

    if key_search
        begin
            key_search = MultiValueConjunctionParser.new.parse_conjunction_list key_search
        rescue MultiValueConjunctionParser::SyntaxError => x
            halt 422, {'Content-Type' => 'text/plain'}, x.message
        end
    end

    rq_opts = {
        :filter_name => filter_name,
        :page_base => page_base,
        :page_extent => page_extent,
        :key_search => key_search,
    }

    # get credentials


    cr = get_required_credentials

    # get requester


    rr = settings.razor_requester

    # get connector


    ec = PortfoliosConnector.new(rr, credentials: cr, **rq_opts)

    # issue request


    begin

        qr = ec.get_hierarchy indicate_result_by: :qualified_result
    rescue ::ArgumentError, ::NameError, ::NoMethodError, ::TypeError => x

        log(:violation) { "(#{x.class}): #{x.message}" }
        raise
    rescue RazorRequester::InvalidCredentialsException => x

        log(:failure) { "exception (#{x.class}): #{x}" }
        halt 401, { 'Content-Type'  =>  'text/plain' }, 'Invalid credentials'
    rescue DataNotFoundException => x

        log(:failure) { "exception (#{x.class}): #{x}" }
        halt 404, { 'Content-Type'  =>  'text/plain' }, x.message
    rescue RRCSBaseException => x

        log(:failure) { "exception (#{x.class}): #{x}" }
        halt 500, { 'Content-Type'  =>  'text/plain' }, x.message
    end

    log :debug1, "qr(#{qr.class})='#{qr}'"

    unless qr.succeeded?

        if :no_such_filter == qr.result

            log :debug1, "failed to retrieve portfolios: no such filter '#{filter_name}'"
            halt 422, { 'Content-Type'  =>  'text/plain' }, "no such filter '#{filter_name}'"
        elsif r = qr.failure_qualifier.reasons.lookup?('RZ0011')

            log :debug1, 'failed to retrieve portfolios'
            halt 404, { 'Content-Type'  =>  'text/plain' }, "#{r.message}: #{r.details}"
        else

            log :warning, 'failed to retrieve portfolios'
            halt 500, { 'Content-Type'  =>  'text/plain' }, qr.failure_qualifier.reasons.join("\n")
        end
    end

    r = qr.result.to_s

    # return result


    status 200

    if false
    elsif request.accept?('application/xml')

        log :debug1, 'application/xml'
        content_type 'application/xml'
        r
    elsif request.accept?('application/json')

        log :debug1, 'application/json'
        content_type 'application/json'
        convert_XML_to_JSON r, { scheme: :gdata }
    elsif request.accept?('text/xml')

        log :debug1, 'text/xml'
        content_type 'text/xml'
        r
    else

        log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
        status 500
    end
end