-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
64 lines (52 loc) · 1.46 KB
/
main.rb
File metadata and controls
64 lines (52 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'sinatra'
require 'json'
require 'sinatra/json'
require 'sinatra/content_for'
require 'haml'
require_relative 'lib/hash_from_xml'
require_relative 'models'
set :haml, :format => :html5
set :json_encoder, :to_json
helpers do
def api_parameters(server, port, path)
{
'server' => server,
'port' => port,
'path' => path
}
end
end
get '/' do
haml :index
end
post '/api_through_orm' do
content_type :json
# make API URL
api_params = api_parameters(request.host, request.port, 'api_stub')
api_call_model = ApiCallProvider.new(params, api_params)
begin
# call stub handler
response = api_call_model.request
response[:status] = 'ok'
json response
rescue RequestValidationError => err
errs = []
api_call_model.errors.each_pair { |k, v| errs << { field: k, message: v } }
json status: 'error', errors: errs
rescue ApiError => err
json status: 'error', errors: [{ field: 'API', message: err.message }]
end
end
get '/api_stub' do
content_type 'text/xml'
xml = ['<?xml version="1.0" encoding="UTF-8" standalone="no" ?>',
'<p1:UserData xmlns:p1="urn:exampleuserdata" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:noNamespaceSchemaLocation="userdata.xsd">',
' <firstName>Maurits Cornelis</firstName>',
' <lastName>Escher</lastName>',
' <age>113</age>',
'</p1:UserData>'
].join("\n")
xml
end