From 687371a4211f795ef0e6b8ba4b019be5c66d2c93 Mon Sep 17 00:00:00 2001 From: Tony Ta Date: Wed, 11 May 2016 19:36:39 -0700 Subject: [PATCH 1/2] faster and more concise string manipulation --- lib/http/content_type.rb | 8 ++++---- lib/http/response/status.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/http/content_type.rb b/lib/http/content_type.rb index 20e44571..056ca4b9 100644 --- a/lib/http/content_type.rb +++ b/lib/http/content_type.rb @@ -13,14 +13,14 @@ def parse(str) # :nodoc: def mime_type(str) - md = str.to_s.match MIME_TYPE_RE - md && md[1].to_s.strip.downcase + m = str.to_s[MIME_TYPE_RE, 1] + m && m.strip.downcase end # :nodoc: def charset(str) - md = str.to_s.match CHARSET_RE - md && md[1].to_s.strip.gsub(/^"|"$/, "") + m = str.to_s[CHARSET_RE, 1] + m && m.strip.delete('"') end end end diff --git a/lib/http/response/status.rb b/lib/http/response/status.rb index 73fb3444..821746bf 100644 --- a/lib/http/response/status.rb +++ b/lib/http/response/status.rb @@ -43,7 +43,7 @@ def coerce(object) # @param [#to_s] str # @return [Symbol] def symbolize(str) - str.to_s.downcase.tr("-", " ").gsub(/[^a-z ]/, "").gsub(/\s+/, "_").to_sym + str.to_s.downcase.tr("- ", "_").delete("^a-z_").to_sym end end From 7007005f6802dcf923ac1518ca3a9ad27773264a Mon Sep 17 00:00:00 2001 From: Tony Ta Date: Wed, 11 May 2016 19:41:30 -0700 Subject: [PATCH 2/2] more conventional to compare variable to constant, rather than other way around --- lib/http/redirector.rb | 6 +++--- lib/http/response/status.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/http/redirector.rb b/lib/http/redirector.rb index 783273ea..f2cda14d 100644 --- a/lib/http/redirector.rb +++ b/lib/http/redirector.rb @@ -68,13 +68,13 @@ def perform(request, response) # Check if we reached max amount of redirect hops # @return [Boolean] def too_many_hops? - 1 <= @max_hops && @max_hops < @visited.count + @max_hops > 0 && @visited.count > @max_hops end # Check if we got into an endless loop # @return [Boolean] def endless_loop? - 2 <= @visited.count(@visited.last) + @visited.count(@visited.last) > 1 end # Redirect policy for follow @@ -90,7 +90,7 @@ def redirect_to(uri) verb = :get end - verb = :get if !SEE_OTHER_ALLOWED_VERBS.include?(verb) && 303 == code + verb = :get if !SEE_OTHER_ALLOWED_VERBS.include?(verb) && code == 303 @request.redirect(uri, verb) end diff --git a/lib/http/response/status.rb b/lib/http/response/status.rb index 821746bf..2b2e15a4 100644 --- a/lib/http/response/status.rb +++ b/lib/http/response/status.rb @@ -132,7 +132,7 @@ def inspect SYMBOLS.each do |code, symbol| class_eval <<-RUBY, __FILE__, __LINE__ def #{symbol}? # def bad_request? - #{code} == code # 400 == code + code == #{code} # code == 400 end # end RUBY end