Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::Trades::ItemPost

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Pantheios, 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_post.rb

Overview

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

Constant Summary collapse

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

    result-form
}

Instance Method Summary collapse

Instance Method Details

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



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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/trades/item_post.rb', line 65

def handle env, params, request, response

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

    rf  =   case rf = (params['result-form'] || '').to_s
            when '', 'id'
                :id
            when 'body'
                :body
            else
                log(:warning) { "unrecognised result-form '#{rf}'" }
                :id
            end

    # 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.insert_trade rq_body, indicate_result_by: :qualified_result, result_form: rf
    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 insert trade: duplicate record'

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

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

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

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

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

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

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

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

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

                log :warning, 'failed to insert trade'

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

            log :warning, 'trade inserted with mixed success'

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

            builder =   ::Nokogiri::XML::Builder.new({}, 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

            halt 422, { 'Content-Type' => 'application/xml' }, body.to_xml
        end
    end

    status 201

    r   =   qr.result.to_s

    if rf == :id

        r
    else

        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'

            resp_hash   =   Hash.from_xml r

            resp_json   =   resp_hash

            JSON.generate resp_json
        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
end