Skip to content

Commit a00381d

Browse files
committed
[rubocop] apply --auto-correct
1 parent 67a8402 commit a00381d

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

Gemfile

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
ruby '~> 2.3.1'
6+
gem 'nokogiri', '1.8.5'
7+
gem 'puma'
48
gem 'rack', '2.0.5'
9+
gem 'rack-cors', require: 'rack/cors'
510
gem 'sinatra'
6-
gem 'puma'
7-
gem 'nokogiri', '1.8.5'
8-
gem 'rack-cors', :require => 'rack/cors'
911

1012
gem 'json'
1113

1214
# Opentracing
13-
gem 'rack-tracer', '0.8.0'
15+
gem 'jaeger-client', '0.6.1'
1416
gem 'opentracing', '0.4.1'
17+
gem 'rack-tracer', '0.8.0'
1518
gem 'spanmanager', '= 0.3.0'
16-
gem 'jaeger-client', '0.6.1'
1719

1820
group :development do
1921
gem 'shotgun'

config.ru

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# frozen_string_literal: true
2+
13
require './echo_api'
24
run Sinatra::Application

echo_api.rb

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#shotgun app.rb -p 9294
1+
# frozen_string_literal: true
2+
3+
# shotgun app.rb -p 9294
24

35
require 'sinatra'
46
require 'json'
@@ -8,8 +10,6 @@
810
require 'base64'
911
require 'rack/cors'
1012

11-
@@random = Random.new
12-
1313
enable :logging
1414
set :protection, except: [:json_csrf]
1515

@@ -19,15 +19,15 @@
1919
enable :static
2020
end
2121

22-
if ENV['OPENTRACING_TRACER'] == "jaeger"
22+
if ENV['OPENTRACING_TRACER'] == 'jaeger'
2323
require 'opentracing'
2424
require 'jaeger/client'
2525
require 'spanmanager'
2626
require 'rack/tracer'
2727

28-
jaeger_agent_host = ENV['JAEGER_AGENT_HOST'] || "127.0.0.1"
28+
jaeger_agent_host = ENV['JAEGER_AGENT_HOST'] || '127.0.0.1'
2929
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'
3131
jaeger_client = Jaeger::Client.build(host: jaeger_agent_host, port: jaeger_agent_port, service_name: jaeger_service_name, flush_interval: 1)
3232
OpenTracing.global_tracer = SpanManager::Tracer.new(jaeger_client)
3333
use ::Rack::Tracer
@@ -37,8 +37,8 @@
3737
use Rack::Cors do
3838
allow do
3939
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
4242
]
4343
end
4444
end
@@ -54,19 +54,19 @@ def all_methods(path, opts = {}, &block)
5454
end
5555

5656
def get_headers
57-
env.select {|k, v| k.start_with? 'HTTP_'}
57+
env.select { |k, _v| k.start_with? 'HTTP_' }
5858
end
5959

6060
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_' }
6262
end
6363

6464
def echo_response
6565
body = request.body.read
6666
hash = Digest::SHA1.base64digest(body)
6767
# B64 encode if contains obviously non-printable character(s).
6868
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:]]/
7070
body = Base64.encode64(body)
7171
end
7272

@@ -75,10 +75,10 @@ def echo_response
7575
# ECHO_<baz> as a response header <baz>:
7676
get_echoable_headers.each do |(header, value)|
7777
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('-')
8282

8383
headers[response_header] = value
8484
end
@@ -88,13 +88,13 @@ def echo_response
8888
path: request.path,
8989
args: request.query_string,
9090
body: body,
91-
headers: get_headers(),
91+
headers: get_headers,
9292
uuid: SecureRandom.uuid
9393
}
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
9898

9999
# Prefer JSON if possible, including cases of unrecognised/erroneous types.
100100
if request.accept?('application/xml') && !request.accept?('application/json')
@@ -107,33 +107,33 @@ def echo_response
107107
end
108108

109109
def build_xml_response(method:, path:, uuid:, body:, bodySha1: nil,
110-
bodyLength:0, headers:, args: nil)
110+
bodyLength: 0, headers:, args: nil)
111111

112112
builder = Nokogiri::XML::Builder.new do |xml|
113-
xml.echoResponse {
113+
xml.echoResponse do
114114
xml.method_ method
115115
xml.path path
116116
xml.uuid uuid
117117
xml.bodySha1 bodySha1 if bodySha1
118118
xml.bodyLength bodyLength if bodyLength > 0
119119
xml.body body if bodyLength > 0
120-
xml.headers { |headers_|
120+
xml.headers do |headers_|
121121
headers.each_pair do |key, value|
122-
headers_.header { |header|
122+
headers_.header do |header|
123123
header.key key.split('HTTP_')[1]
124124
header.value value
125-
}
125+
end
126126
end
127-
}
128-
xml.args { |args|
127+
end
128+
xml.args do |args|
129129
request.env['rack.request.query_hash'].each_pair do |key, value|
130-
args.arg { |arg|
130+
args.arg do |arg|
131131
arg.key key
132132
arg.value value
133-
}
133+
end
134134
end
135-
}
136-
}
135+
end
136+
end
137137
end
138138
builder.to_xml
139139
end
@@ -145,6 +145,6 @@ def build_xml_response(method:, path:, uuid:, body:, bodySha1: nil,
145145
get '/favicon.ico' do # Avoid bumping counter on favicon
146146
end
147147

148-
all_methods "/**" do
148+
all_methods '/**' do
149149
echo_response
150150
end

0 commit comments

Comments
 (0)