Skip to content

Commit a93e87b

Browse files
committed
Some micro optimizations (literal strings mostly).
1 parent d7718f0 commit a93e87b

File tree

16 files changed

+80
-55
lines changed

16 files changed

+80
-55
lines changed

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Grape
3939
autoload :Cookies
4040
autoload :Validations
4141
autoload :Request, 'grape/http/request'
42+
autoload :Env, 'grape/util/env'
4243
end
4344

4445
module Http

lib/grape/dsl/inside_route.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def declared(params, options = {}, declared_params = nil)
5656

5757
# The API version as specified in the URL.
5858
def version
59-
env['api.version']
59+
env[Grape::Env::API_VERSION]
6060
end
6161

6262
# End the request and display an error to the
@@ -241,7 +241,7 @@ def present(*args)
241241
if key
242242
representation = (@body || {}).merge(key => representation)
243243
elsif entity_class.present? && @body
244-
fail ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?('merge')
244+
fail ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
245245
representation = @body.merge(representation)
246246
end
247247

@@ -293,7 +293,7 @@ def entity_class_for_obj(object, options)
293293
# the given entity_class.
294294
def entity_representation_for(entity_class, object, options)
295295
embeds = { env: env }
296-
embeds[:version] = env['api.version'] if env['api.version']
296+
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
297297
entity_class.represent(object, embeds.merge(options))
298298
end
299299
end

lib/grape/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def call(env)
193193
def call!(env)
194194
extend helpers
195195

196-
env['api.endpoint'] = self
196+
env[Grape::Env::API_ENDPOINT] = self
197197
if options[:app]
198198
options[:app].call(env)
199199
else

lib/grape/error_formatter/base.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ def present(message, env)
3333
present_options = {}
3434
present_options[:with] = message.delete(:with) if message.is_a?(Hash)
3535

36-
presenter = env['api.endpoint'].entity_class_for_obj(message, present_options)
36+
presenter = env[Grape::Env::API_ENDPOINT].entity_class_for_obj(message, present_options)
3737

38-
unless presenter || env['rack.routing_args'].nil?
38+
unless presenter || env[Grape::Env::RACK_ROUTING_ARGS].nil?
3939
# env['api.endpoint'].route does not work when the error occurs within a middleware
4040
# the Endpoint does not have a valid env at this moment
41-
http_codes = env['rack.routing_args'][:route_info].route_http_codes || []
41+
http_codes = env[Grape::Env::RACK_ROUTING_ARGS][:route_info].route_http_codes || []
4242
found_code = http_codes.find do |http_code|
43-
(http_code[0].to_i == env['api.endpoint'].status) && http_code[2].respond_to?(:represent)
44-
end if env['api.endpoint'].request
43+
(http_code[0].to_i == env[Grape::Env::API_ENDPOINT].status) && http_code[2].respond_to?(:represent)
44+
end if env[Grape::Env::API_ENDPOINT].request
4545

4646
presenter = found_code[2] if found_code
4747
end
4848

4949
if presenter
5050
embeds = { env: env }
51-
embeds[:version] = env['api.version'] if env['api.version']
51+
embeds[:version] = env[Grape::Env::API_VERSION] if env[Grape::Env::API_VERSION]
5252
message = presenter.represent(message, embeds).serializable_hash
5353
end
5454

lib/grape/exceptions/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Grape
22
module Exceptions
33
class Base < StandardError
4-
BASE_MESSAGES_KEY = 'grape.errors.messages'
5-
BASE_ATTRIBUTES_KEY = 'grape.errors.attributes'
4+
BASE_MESSAGES_KEY = 'grape.errors.messages'.freeze
5+
BASE_ATTRIBUTES_KEY = 'grape.errors.attributes'.freeze
66
FALLBACK_LOCALE = :en
77

88
attr_reader :status, :message, :headers

lib/grape/http/request.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
module Grape
22
class Request < Rack::Request
3-
ROUTING_ARGS = 'rack.routing_args'
4-
HTTP_PREFIX = 'HTTP_'
5-
UNDERSCORE = '_'
6-
MINUS = '-'
3+
HTTP_PREFIX = 'HTTP_'.freeze
4+
UNDERSCORE = '_'.freeze
5+
MINUS = '-'.freeze
76

87
def params
98
@params ||= begin
109
params = Hashie::Mash.new(super)
11-
if env[ROUTING_ARGS]
12-
args = env[ROUTING_ARGS].dup
10+
if env[Grape::Env::RACK_ROUTING_ARGS]
11+
args = env[Grape::Env::RACK_ROUTING_ARGS].dup
1312
# preserve version from query string parameters
1413
args.delete(:version)
1514
args.delete(:route_info)
@@ -20,15 +19,14 @@ def params
2019
end
2120

2221
def headers
23-
@headers ||= env.dup.inject({}) do |h, (k, v)|
24-
if k.to_s.start_with? HTTP_PREFIX
25-
k = k[5..-1]
26-
k.tr!(UNDERSCORE, MINUS)
27-
k.downcase!
28-
k.gsub!(/^.|[-_\s]./, &:upcase!)
29-
h[k] = v
30-
end
31-
h
22+
@headers ||= env.each_with_object({}) do |(k, v), h|
23+
next unless k.to_s.start_with? HTTP_PREFIX
24+
25+
k = k[5..-1]
26+
k.tr!(UNDERSCORE, MINUS)
27+
k.downcase!
28+
k.gsub!(/^.|[-\s]./, &:upcase!)
29+
h[k] = v
3230
end
3331
end
3432
end

lib/grape/middleware/auth/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(app, options = {})
1212
end
1313

1414
def context
15-
env['api.endpoint']
15+
env[Grape::Env::API_ENDPOINT]
1616
end
1717

1818
def call(env)

lib/grape/middleware/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Grape
22
module Middleware
33
class Base
44
attr_reader :app, :env, :options
5+
TEXT_HTML = 'text/html'.freeze
56

67
# @param [Rack Application] app The standard argument for a Rack middleware.
78
# @param [Hash] options A hash of options, simply stored for use by subclasses.
@@ -50,7 +51,7 @@ def content_types
5051
end
5152

5253
def content_type
53-
content_type_for(env['api.format'] || options[:format]) || 'text/html'
54+
content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || TEXT_HTML
5455
end
5556

5657
def mime_types

lib/grape/middleware/error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def rack_response(message, status = options[:default_status], headers = { Grape:
8282
end
8383

8484
def format_message(message, backtrace)
85-
format = env['api.format'] || options[:format]
85+
format = env[Grape::Env::API_FORMAT] || options[:format]
8686
formatter = Grape::ErrorFormatter::Base.formatter_for(format, options)
8787
throw :error, status: 406, message: "The requested format '#{format}' is not supported." unless formatter
8888
formatter.call(message, backtrace, options, env)

lib/grape/middleware/formatter.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Grape
44
module Middleware
55
class Formatter < Base
6+
CHUNKED = 'chunked'.freeze
7+
68
def default_options
79
{
810
default_format: :txt,
@@ -28,7 +30,7 @@ def after
2830
end
2931
else
3032
# Allow content-type to be explicitly overwritten
31-
api_format = mime_types[headers[Grape::Http::Headers::CONTENT_TYPE]] || env['api.format']
33+
api_format = mime_types[headers[Grape::Http::Headers::CONTENT_TYPE]] || env[Grape::Env::API_FORMAT]
3234
formatter = Grape::Formatter::Base.formatter_for(api_format, options)
3335

3436
begin
@@ -57,7 +59,7 @@ def ensure_content_type(headers)
5759
if headers[Grape::Http::Headers::CONTENT_TYPE]
5860
headers
5961
else
60-
headers.merge(Grape::Http::Headers::CONTENT_TYPE => content_type_for(env['api.format']))
62+
headers.merge(Grape::Http::Headers::CONTENT_TYPE => content_type_for(env[Grape::Env::API_FORMAT]))
6163
end
6264
end
6365

@@ -70,11 +72,11 @@ def read_body_input
7072
if (request.post? || request.put? || request.patch? || request.delete?) &&
7173
(!request.form_data? || !request.media_type) &&
7274
(!request.parseable_data?) &&
73-
(request.content_length.to_i > 0 || request.env[Grape::Http::Headers::HTTP_TRANSFER_ENCODING] == 'chunked')
75+
(request.content_length.to_i > 0 || request.env[Grape::Http::Headers::HTTP_TRANSFER_ENCODING] == CHUNKED)
7476

75-
if (input = env['rack.input'])
77+
if (input = env[Grape::Env::RACK_INPUT])
7678
input.rewind
77-
body = env['api.request.input'] = input.read
79+
body = env[Grape::Env::API_REQUEST_INPUT] = input.read
7880
begin
7981
read_rack_input(body) if body && body.length > 0
8082
ensure
@@ -92,22 +94,22 @@ def read_rack_input(body)
9294
parser = Grape::Parser::Base.parser_for fmt, options
9395
if parser
9496
begin
95-
body = (env['api.request.body'] = parser.call(body, env))
97+
body = (env[Grape::Env::API_REQUEST_BODY] = parser.call(body, env))
9698
if body.is_a?(Hash)
97-
if env['rack.request.form_hash']
98-
env['rack.request.form_hash'] = env['rack.request.form_hash'].merge(body)
99+
if env[Grape::Env::RACK_REQUEST_FORM_HASH]
100+
env[Grape::Env::RACK_REQUEST_FORM_HASH] = env[Grape::Env::RACK_REQUEST_FORM_HASH].merge(body)
99101
else
100-
env['rack.request.form_hash'] = body
102+
env[Grape::Env::RACK_REQUEST_FORM_HASH] = body
101103
end
102-
env['rack.request.form_input'] = env['rack.input']
104+
env[Grape::Env::RACK_REQUEST_FORM_INPUT] = env[Grape::Env::RACK_INPUT]
103105
end
104106
rescue Grape::Exceptions::Base => e
105107
raise e
106108
rescue StandardError => e
107109
throw :error, status: 400, message: e.message
108110
end
109111
else
110-
env['api.request.body'] = body
112+
env[Grape::Env::API_REQUEST_BODY] = body
111113
end
112114
else
113115
throw :error, status: 406, message: "The requested content-type '#{request.media_type}' is not supported."
@@ -117,7 +119,7 @@ def read_rack_input(body)
117119
def negotiate_content_type
118120
fmt = format_from_extension || format_from_params || options[:format] || format_from_header || options[:default_format]
119121
if content_type_for(fmt)
120-
env['api.format'] = fmt
122+
env[Grape::Env::API_FORMAT] = fmt
121123
else
122124
throw :error, status: 406, message: "The requested format '#{fmt}' is not supported."
123125
end

0 commit comments

Comments
 (0)