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
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
|
# File 'lib/cocainum/api.rb', line 53
def execute(request, response)
df = request.read
df.callback do |rq|
EM.synchrony do
begin
url = rq.url.to_s
url_items = url.split('/')
url_items.shift if url_items[0].empty?
api_req = ApiRequest.new
api_req.method = rq.method
url_items[0].match(/^v[0-9.]+/) { |m|
api_req.version = m[0]
url_items.shift
}
format = url.match(/\.([a-z]+)$/)
if format && %w(json xml).include?(format[1])
api_req.accept = format[1].to_sym
elsif rq..include?('Accept')
= Hash[rq.['Accept'].split(',').map { |row|
params = row.split(';')
ver = ''
row.match(/format=(v[0-9.]+)/) { |m| ver = m[1]}
[params[0], {:version => ver}]
}]
if .key?('application/json')
api_req.accept = :json
api_req.version = ['application/json'][:version]
elsif .key?('application/xml')
api_req.accept = :xml
api_req.version = ['application/xml'][:version]
end
end
api_req.content_type = rq..key?('Content-Type') ? rq.['Content-Type'] : 'text/plain'
api_req.request = case api_req.content_type
when 'application/json'
JSON.parse(rq.body, {symbolize_names:true})
when 'application/xml'
Nokogiri::XML(rq.body).to_hash
else
{}
end
api_req.action = url_items[0]
api_resp = if @handlers[api_req.method] && @handlers[api_req.method][api_req.action]
@handlers[api_req.method][api_req.action].call(api_req)
else
{:code => 400, :response => {:error_details => 'method unknown ' + api_req.method + ': ' + api_req.action + ' (' + @handlers.inspect + ')'}}
end
case api_req.accept
when :json
api_resp[:response] = prepare_hash_for_json(api_resp[:response])
msg = JSON.generate(api_resp[:response])
content_type = 'application/json'
when :xml
msg = api_resp[:response].to_xml
content_type = 'application/xml'
else
content_type = 'plain/html'
msg = 'error'
end
=begin
msg = 'Message: ' + (rq.query.key?('message') ? rq.query['message'][0] : '') + '<br/>Body: ' + rq.body + '<br/>Headers: ' + rq.headers.inspect + '<br/>Test: '
service = Cocaine::Synchrony::Service.new 'DummyService'
ch = service.enqueue('write_dummy', 'sent this message123')
msg1 = ch.collect(1)
msg += '<br/>URL: ' + url
msg += '<br/>Method: ' + api_req.method
msg += '<br/>Version: ' + api_req.version
msg += '<br/>Accept: ' + api_req.accept.to_s
msg += '<br/>Content-type: ' + api_req.content_type
msg += '<br/>Query: ' + api_req.request.inspect
msg += '<br/>' + msg1.inspect
=end
= [['Content-Type', content_type + '; charset=UTF-8'],
['Content-Length', msg.size.to_s],
['Expires', 'Thu, 19 Nov 1981 08:52:00 GMT'],
['Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0']
]
api_resp[:headers].each {|k, v|
<< [k, v]
}
rescue Exception=> e
msg = 'Message: ' + e.message + '<br/>'
msg += 'Stacktrace:<br/>' + e.backtrace.join('<br/>')
end
response.(api_resp[:code], )
response.body = msg
response.close
end
end
end
|