1
- #shotgun app.rb -p 9294
1
+ # frozen_string_literal: true
2
+
3
+ # shotgun app.rb -p 9294
2
4
3
5
require 'sinatra'
4
6
require 'json'
8
10
require 'base64'
9
11
require 'rack/cors'
10
12
11
- @@random = Random . new
12
-
13
13
enable :logging
14
14
set :protection , except : [ :json_csrf ]
15
15
19
19
enable :static
20
20
end
21
21
22
- if ENV [ 'OPENTRACING_TRACER' ] == " jaeger"
22
+ if ENV [ 'OPENTRACING_TRACER' ] == ' jaeger'
23
23
require 'opentracing'
24
24
require 'jaeger/client'
25
25
require 'spanmanager'
26
26
require 'rack/tracer'
27
27
28
- jaeger_agent_host = ENV [ 'JAEGER_AGENT_HOST' ] || " 127.0.0.1"
28
+ jaeger_agent_host = ENV [ 'JAEGER_AGENT_HOST' ] || ' 127.0.0.1'
29
29
jaeger_agent_port = ENV [ 'JAEGER_AGENT_PORT' ] || 6831
30
- jaeger_service_name = ENV [ 'JAEGER_SERVICE_NAME' ] || " echo-api"
30
+ jaeger_service_name = ENV [ 'JAEGER_SERVICE_NAME' ] || ' echo-api'
31
31
jaeger_client = Jaeger ::Client . build ( host : jaeger_agent_host , port : jaeger_agent_port , service_name : jaeger_service_name , flush_interval : 1 )
32
32
OpenTracing . global_tracer = SpanManager ::Tracer . new ( jaeger_client )
33
33
use ::Rack ::Tracer
37
37
use Rack ::Cors do
38
38
allow do
39
39
origins '*'
40
- resource '*' , headers : :any , methods : [
41
- : head, : options, : get, : post, : patch, : put, : delete
40
+ resource '*' , headers : :any , methods : %i [
41
+ head options get post patch put delete
42
42
]
43
43
end
44
44
end
@@ -54,19 +54,19 @@ def all_methods(path, opts = {}, &block)
54
54
end
55
55
56
56
def get_headers
57
- env . select { |k , v | k . start_with? 'HTTP_' }
57
+ env . select { |k , _v | k . start_with? 'HTTP_' }
58
58
end
59
59
60
60
def get_echoable_headers
61
- get_headers ( ) . select { |k , v | k . start_with? 'HTTP_ECHO_' }
61
+ get_headers . select { |k , _v | k . start_with? 'HTTP_ECHO_' }
62
62
end
63
63
64
64
def echo_response
65
65
body = request . body . read
66
66
hash = Digest ::SHA1 . base64digest ( body )
67
67
# B64 encode if contains obviously non-printable character(s).
68
68
if request . media_type == 'application/octet-stream' ||
69
- request . media_type == 'multipart/form-data' || body =~ /[^[:print:]]/
69
+ request . media_type == 'multipart/form-data' || body =~ /[^[:print:]]/
70
70
body = Base64 . encode64 ( body )
71
71
end
72
72
@@ -75,10 +75,10 @@ def echo_response
75
75
# ECHO_<baz> as a response header <baz>:
76
76
get_echoable_headers . each do |( header , value ) |
77
77
response_header = header
78
- . gsub ( /HTTP_ECHO_/ , '' )
79
- . split ( '_' )
80
- . collect ( &:capitalize )
81
- . join ( '-' )
78
+ . gsub ( /HTTP_ECHO_/ , '' )
79
+ . split ( '_' )
80
+ . collect ( &:capitalize )
81
+ . join ( '-' )
82
82
83
83
headers [ response_header ] = value
84
84
end
@@ -88,13 +88,13 @@ def echo_response
88
88
path : request . path ,
89
89
args : request . query_string ,
90
90
body : body ,
91
- headers : get_headers ( ) ,
91
+ headers : get_headers ,
92
92
uuid : SecureRandom . uuid
93
93
}
94
- response_args . merge! (
95
- bodySha1 : hash ,
96
- bodyLength : body . length
97
- ) if body . length > 0
94
+ unless body . empty?
95
+ response_args [ :bodySha1 ] = hash
96
+ response_args [ :bodyLength ] = body . length
97
+ end
98
98
99
99
# Prefer JSON if possible, including cases of unrecognised/erroneous types.
100
100
if request . accept? ( 'application/xml' ) && !request . accept? ( 'application/json' )
@@ -107,33 +107,33 @@ def echo_response
107
107
end
108
108
109
109
def build_xml_response ( method :, path :, uuid :, body :, bodySha1 : nil ,
110
- bodyLength :0 , headers :, args : nil )
110
+ bodyLength : 0 , headers :, args : nil )
111
111
112
112
builder = Nokogiri ::XML ::Builder . new do |xml |
113
- xml . echoResponse {
113
+ xml . echoResponse do
114
114
xml . method_ method
115
115
xml . path path
116
116
xml . uuid uuid
117
117
xml . bodySha1 bodySha1 if bodySha1
118
118
xml . bodyLength bodyLength if bodyLength > 0
119
119
xml . body body if bodyLength > 0
120
- xml . headers { |headers_ |
120
+ xml . headers do |headers_ |
121
121
headers . each_pair do |key , value |
122
- headers_ . header { |header |
122
+ headers_ . header do |header |
123
123
header . key key . split ( 'HTTP_' ) [ 1 ]
124
124
header . value value
125
- }
125
+ end
126
126
end
127
- }
128
- xml . args { |args |
127
+ end
128
+ xml . args do |args |
129
129
request . env [ 'rack.request.query_hash' ] . each_pair do |key , value |
130
- args . arg { |arg |
130
+ args . arg do |arg |
131
131
arg . key key
132
132
arg . value value
133
- }
133
+ end
134
134
end
135
- }
136
- }
135
+ end
136
+ end
137
137
end
138
138
builder . to_xml
139
139
end
@@ -145,6 +145,6 @@ def build_xml_response(method:, path:, uuid:, body:, bodySha1: nil,
145
145
get '/favicon.ico' do # Avoid bumping counter on favicon
146
146
end
147
147
148
- all_methods " /**" do
148
+ all_methods ' /**' do
149
149
echo_response
150
150
end
0 commit comments