Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::Trades::ItemPut

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Pantheios, Util::ConversionUtil, RazorRisk::Core::Diagnostics::Logger, Razor::Connectivity::EntityConnectors::Exceptions, Razor::Connectivity::Razor3, Razor::Connectivity::Razor3::EntityConnectors, Xqsr3::Conversion
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/trades/item_put.rb

Overview

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

Constant Summary collapse

HTTP_VERB =
:put
HTTP_ACCEPTS =
%w{
    application/xml
    application/json
    text/xml
}
ROUTE_VARIABLES =
%w{
    id
}
QUERY_PARAMETERS =
%w{}

Instance Method Summary collapse

Instance Method Details

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



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
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
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
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/trades/item_put.rb', line 66

def handle env, params, request, response

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

    id  =   params['id']

    # read request body (XML)

    #

    # NOTE: For this handler, the input is required to be XML, and it

    # is required that the document has a single top-level node 'portfolios'


    request.body.rewind

    body_text   =   request.body.read
    rq_body     =   nil

    case request.content_type
    when 'application/xml', 'text/xml'

        rq_body =   ::Nokogiri.XML body_text
    else

        log :violation, "unexpected content-type '#{request.content_type}'"
    end

    # get credentials


    cr  =   get_required_credentials

    # get requester


    rr  =   settings.razor_requester

    # get connector


    ec  =   TradesConnector.new(rr, credentials: cr)

    # issue request


    begin

        qr  =   ec.update_or_insert_trade(
            id,
            rq_body,
            indicate_result_by: :qualified_result
        )
    rescue InvalidRequestContentException => x

        log :failure, "exception (#{x.class}): #{x}"

        headers =   {

            'Content-Type'  =>  'text/plain',
        }

        halt 422, headers, x.message
    rescue RazorRequester::InvalidCredentialsException => x

        log :failure, "exception (#{x.class}): #{x}"

        headers =   {

            'Content-Type'  =>  'text/plain',
        }

        halt 401, headers, 'Invalid credentials'
    rescue RazorRequester::Exception => x

        log :failure, "exception (#{x.class}): #{x}"

        headers =   {

            'Content-Type'  =>  'text/plain',
        }

        halt 500, headers, x.message
    end

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

    unless qr.succeeded?

        if qr.result.nil?

            if r = qr.failure_qualifier.reasons.lookup?('RZ0010')

                log :debug1, 'failed to update trade: duplicate record'

                halt 422, {}, "#{r.message}: #{r.details}"
            elsif r = qr.failure_qualifier.reasons.lookup?('RZ0014')

                log :debug1, 'failed to update trade: invalid data'

                halt 422, {}, "#{r.message}: #{r.details}"
            elsif r = qr.failure_qualifier.reasons.lookup?('RZ0022')

                log :debug1, 'failed to update trade: invalid trade'

                halt 422, {}, "#{r.message}: #{r.details}"
            elsif r = qr.failure_qualifier.reasons.lookup?('RZ0027')

                log :debug1, 'failed to update trade: invalid netting agreement'

                halt 422, {}, "#{r.message}: #{r.details}"
            elsif r = qr.failure_qualifier.reasons.lookup?('RZ0028')

                log :debug1, 'failed to update trade: invalid What-if batch number'

                halt 422, {}, "#{r.message}: #{r.details}"
            else

                log :warning, 'failed to update trade'

                halt 500, {}, qr.failure_qualifier.reasons.join("\n")
            end
        else

            log :warning, 'trade updated with mixed success'

            resp_body    =   ::Nokogiri.XML(qr.result)

            ::Nokogiri::XML::Builder.new({}, resp_body.root) do |xml|
                xml.reasons do
                    qr.failure_qualifier.reasons.each do |reason|
                        xml.reason do
                            details =   reason.details
                            details &&= details.split("\n")
                                .map { |detail| detail.strip }
                                .reject { |detail| detail.empty? }
                                .join("\n")
                            xml.code reason.code if reason.code
                            xml.message reason.message if reason.message
                            xml.details details if details
                        end
                    end
                end
            end

            if request.accept? 'application/xml'
                headers = { 'Content-Type' => 'application/xml' }
                body = resp_body.to_xml
            elsif request.accept? 'text/xml'
                headers = { 'Content-Type' => 'text/xml' }
                body = resp_body.to_xml
            elsif request.accept? 'application/json'
                headers = { 'Content-Type' => 'application/json' }
                body = convert_XML_to_JSON resp_body, { scheme: :gdata }
            else
                log(:violation) { "unexpected failure to match given 'Accept' header '#{request.accept}'" }
                headers = { 'Content-Type' => 'text/xml' }
                body = resp_body.to_xml
            end

            halt 422, headers, body
        end
    end

    case qr.result
    when :update

        status 204
    when :insert

        status 201
    else

        log :violation, "Unkown trade action '#{qr.result}'"
        status 500
    end
end