From 30216f9268a670df0981587a60c3fa2206ec7f0a Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:33:44 +0530 Subject: [PATCH 1/7] drop `entries` variable and use `map!` instead of `map` on new array to save new object creation --- lib/sinatra/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 2078056a..bc38eeec 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -22,8 +22,8 @@ module Sinatra # Returns an array of acceptable media types for the response def accept @env['sinatra.accept'] ||= begin - entries = @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS) - entries.map { |e| AcceptEntry.new(e) }.sort + @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS) + .map! { |e| AcceptEntry.new(e) }.sort end end From f607ffd81efd72307d1d0a8eab84d79676db1b24 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:41:25 +0530 Subject: [PATCH 2/7] use `map!` instead of `map` on new array to save new object creation also change "1.0" (string) to `1.0`(float) since we save conversion from `to_f` applied on it --- lib/sinatra/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index bc38eeec..ba99242a 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -70,7 +70,7 @@ module Sinatra attr_accessor :params def initialize(entry) - params = entry.scan(HEADER_PARAM).map do |s| + params = entry.scan(HEADER_PARAM).map! do |s| key, value = s.strip.split('=', 2) value = value[1..-2].gsub(/\\(.)/, '\1') if value.start_with?('"') [key, value] @@ -79,7 +79,7 @@ module Sinatra @entry = entry @type = entry[/[^;]+/].delete(' ') @params = Hash[params] - @q = @params.delete('q') { "1.0" }.to_f + @q = @params.delete('q') { 1.0 }.to_f end def <=>(other) From fef89bd28c459919ac92bf0c162bbc60f0163b70 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:43:09 +0530 Subject: [PATCH 3/7] Extract `DROP_BODY_RESPONSES` to a constant --- lib/sinatra/base.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index ba99242a..beb6aea4 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -114,6 +114,7 @@ module Sinatra # http://rack.rubyforge.org/doc/classes/Rack/Response.html # http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html class Response < Rack::Response + DROP_BODY_RESPONSES = [204, 205, 304] def initialize(*) super headers['Content-Type'] ||= 'text/html' @@ -161,7 +162,7 @@ module Sinatra end def drop_body? - [204, 205, 304].include?(status.to_i) + DROP_BODY_RESPONSES.include?(status.to_i) end end From 91127334a93c4f1a6d6f6a3c4cf70e615cd74457 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:44:33 +0530 Subject: [PATCH 4/7] avoid creating an array an then doing a join by use of string interpolation --- lib/sinatra/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index beb6aea4..416f2226 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -454,7 +454,7 @@ module Sinatra hash.each do |key, value| key = key.to_s.tr('_', '-') value = value.to_i if key == "max-age" - values << [key, value].join('=') + values << "#{key}=#{value}" end response['Cache-Control'] = values.join(', ') if values.any? From 55d1de945ebdcdc4132375786bcbfe33443770d5 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:47:09 +0530 Subject: [PATCH 5/7] extract `ETAG_KINDS` to a constant also use string interpolation instead of addition --- lib/sinatra/base.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 416f2226..75eadba0 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -513,6 +513,7 @@ module Sinatra rescue ArgumentError end + ETAG_KINDS = [:strong, :weak] # Set the response entity tag (HTTP 'ETag' header) and halt if conditional # GET matches. The +value+ argument is an identifier that uniquely # identifies the current version of the resource. The +kind+ argument @@ -528,12 +529,12 @@ module Sinatra kind = options[:kind] || :strong new_resource = options.fetch(:new_resource) { request.post? } - unless [:strong, :weak].include?(kind) + unless ETAG_KINDS.include?(kind) raise ArgumentError, ":strong or :weak expected" end value = '"%s"' % value - value = 'W/' + value if kind == :weak + value = "W/#{value}" if kind == :weak response['ETag'] = value if success? or status == 304 From 93480f138cd71ce0992b9902cffab687ee875838 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 01:49:15 +0530 Subject: [PATCH 6/7] use destructive merge operations on `options` to save new hash creations --- lib/sinatra/base.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 75eadba0..e200121a 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -772,7 +772,7 @@ module Sinatra def render(engine, data, options = {}, locals = {}, &block) # merge app-level options engine_options = settings.respond_to?(engine) ? settings.send(engine) : {} - options = engine_options.merge(options) + options.merge!(engine_options) { |key, v1, v2| v1 } # extract generic options locals = options.delete(:locals) || locals || {} @@ -804,8 +804,8 @@ module Sinatra # render layout if layout - options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope) - options.merge! layout_options + options.merge!(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope) + .merge!(layout_options) catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } } end From 0d56f1f90f29340b75cc399c5c080d7c9973294a Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 21 Jul 2013 03:16:32 +0530 Subject: [PATCH 7/7] change `.` indentation to fix jruby syntax error --- lib/sinatra/base.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index e200121a..69058781 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -22,8 +22,8 @@ module Sinatra # Returns an array of acceptable media types for the response def accept @env['sinatra.accept'] ||= begin - @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS) - .map! { |e| AcceptEntry.new(e) }.sort + @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS). + map! { |e| AcceptEntry.new(e) }.sort end end @@ -804,8 +804,8 @@ module Sinatra # render layout if layout - options.merge!(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope) - .merge!(layout_options) + options.merge!(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope). + merge!(layout_options) catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } } end