Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::RiskView::ItemGet

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Cassini::Mixin::RazorResponseValidator, Cassini::Util::ConversionUtil, Pantheios, Razor::Connectivity::EntityConnectors::Exceptions, Razor::Connectivity::Razor3, Razor::Connectivity::Razor3::EntityConnectors
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/risk_view/item_get.rb

Overview

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

Defined Under Namespace

Modules: Constants

Constant Summary collapse

HTTP_VERB =
:get
HTTP_ACCEPTS =
[
    'application/xml',
    'application/json',
    'text/xml',
]
QUERY_PARAMETERS =
[
    'page-base',
    'page-extent',
    'group',
]
ROUTE_VARIABLES =
[
    'domain',
    'id',
]

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options) ⇒ ItemGet

Returns a new instance of ItemGet.



85
86
87
88
89
90
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/risk_view/item_get.rb', line 85

def initialize *args, **options

    @ptf_connector_class = options[:ptf_connector_class] || PortfoliosConnector
    @rpd_connector_class = options[:rpd_connector_class] || RiskPointData::DomainsConnector
    super
end

Instance Method Details

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

########################################################## Handler



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/risk_view/item_get.rb', line 193

def handle env, params, request, response

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

    domain = check_option(
        params,
        'domain',
        strip_str_whitespace: true,
        values: Constants::VALID_DOMAINS,
        nothrow: true,
    ) || error(HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY, 'Invalid domain')

    id = check_option(
        params,
        'id',
        strip_str_whitespace: true,
        reject_empty: true,
        nothrow: true,
    ) || error(HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY, 'Invalid ID')

    page_base = ::Xqsr3::Conversion::IntegerParser.to_integer(
        params['page-base'] || 0
    ) do |x, arg|
        error(
            HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY,
            "invalid page specifier: 'page-base'=#{arg}"
        )
    end
    page_extent = ::Xqsr3::Conversion::IntegerParser.to_integer(
        params['page-extent'] || 1000
    ) do |x, arg|
        error(
            HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY,
            "invalid page specifier: 'page-extent'=#{arg}"
        )
    end

    if page_base < 0 || page_extent < 1
        error(
            HTTP_STATUS_NAMES::RANGE_NOT_SATISFIABLE,
            "invalid page specifier: 'page-base'=#{page_base}; 'page-extent'=#{page_extent}"
        )
    end

    group = 0 == (
        /^t(rue)?$/i =~ check_option(
            params,
            'group',
            strip_str_whitespace: true,
            reject_empty: true,
            nothrow: true,
        )
    )

    if group
        leader_id = find_group_leader_(id)
        if leader_id
            id = leader_id
        else
            log :debug1, "Counterpary #{id} is not part of a group"
        end
    end

    limit_types = get_limit_types_

    # Handle Globals
    risk_view = { domain => id }
    if 'Counterparty' == domain
        if group
            risk_view['LimitType'] = limit_types[:counterparty_group]
        else
            risk_view['LimitType'] = limit_types[:counterparty]
        end
    elsif 'Country' == domain
        risk_view['LimitType'] = limit_types[:country]
    end

    rq_opts = {
        :page_base   => page_base,
        :page_extent => page_extent,
        :risk_view   => risk_view,
        :is_group    => group,
    }

    cr = get_required_credentials
    rr = settings.razor_requester
    ec = @ptf_connector_class.new(rr, credentials: cr, **rq_opts)

    # TODO: allow these exceptions to be handled by the frame work. This
    # will require significant change to the unit tests.
    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}" }
        error HTTP_STATUS_NAMES::UNAUTHORIZED, 'Invalid credentials'
    rescue DataNotFoundException => x
        log(:failure) { "exception (#{x.class}): #{x}" }
        error HTTP_STATUS_NAMES::NOT_FOUND, x.message
    rescue => x
        log(:failure) { "exception (#{x.class}): #{x}" }
        error HTTP_STATUS_NAMES::INTERNAL_SERVER_ERROR, x.message
    end

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

    validate_qualified_razor_response qr

    unless qr.succeeded?
        log :warning, "Failed to retrieve risk-view for '#{id}' for an unkown reason."
        error(
            HTTP_STATUS_NAMES::INTERNAL_SERVER_ERROR,
            'Oops! Something went wrong!'
        )
    end

    r = qr.result.to_s

    status HTTP_STATUS_NAMES::OK

    if request.accept?('application/xml')
        log :debug1, 'application/xml'
        content_type 'application/xml'
        r
    elsif request.accept?('text/xml')
        log :debug1, 'text/xml'
        content_type 'text/xml'
        r
    elsif request.accept?('application/json')
        log :debug1, 'application/json'
        content_type 'application/json'
        convert_XML_to_JSON r, { scheme: :gdata }
    else
        log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
        error(
            HTTP_STATUS_NAMES::INTERNAL_SERVER_ERROR,
            'Oops! Something went wrong!'
        )
    end
end