Skip to content

Commit d4c05b5

Browse files
committed
refactored @base_uri instantiation to have same pattern as
@routing_tokens
1 parent 33c52f4 commit d4c05b5

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

lib/webmachine/request.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ module Webmachine
88
# should be instantiated by {Adapters} when a request is received
99
class Request
1010
HTTP_HEADERS_MATCH = /^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i.freeze
11+
ROUTING_PATH_MATCH = /^\/(.*)/.freeze
1112

1213
extend Forwardable
1314

14-
attr_reader :method, :uri, :headers, :body, :routing_tokens
15+
attr_reader :method, :uri, :headers, :body, :routing_tokens, :base_uri
1516
attr_accessor :disp_path, :path_info, :path_tokens
1617

1718
# @param [String] method the HTTP request method
@@ -21,9 +22,13 @@ class Request
2122
# @param [String,#to_s,#each,nil] body the entity included in the
2223
# request, if present
2324
def initialize(method, uri, headers, body, routing_tokens=nil, base_uri=nil)
24-
@method, @headers, @body, @base_uri = method, headers, body, base_uri
25+
@method, @headers, @body = method, headers, body
2526
@uri = build_uri(uri, headers)
2627
@routing_tokens = routing_tokens || @uri.path.match(ROUTING_PATH_MATCH)[1].split(SLASH)
28+
@base_uri = base_uri || @uri.dup.tap do |u|
29+
u.path = SLASH
30+
u.query = nil
31+
end
2732
end
2833

2934
def_delegators :headers, :[]
@@ -54,16 +59,6 @@ def has_body?
5459
!(body.nil? || body.empty?)
5560
end
5661

57-
# The root URI for the request, ignoring path and query. This is
58-
# useful for calculating relative paths to resources.
59-
# @return [URI]
60-
def base_uri
61-
@base_uri ||= uri.dup.tap do |u|
62-
u.path = SLASH
63-
u.query = nil
64-
end
65-
end
66-
6762
# Returns a hash of query parameters (they come after the ? in the
6863
# URI). Note that this does NOT work in the same way as Rails,
6964
# i.e. it does not support nested arrays and hashes.
@@ -165,8 +160,6 @@ def options?
165160
method == OPTIONS_METHOD
166161
end
167162

168-
ROUTING_PATH_MATCH = /^\/(.*)/.freeze
169-
170163
private
171164

172165
IPV6_MATCH = /\A\[(?<address> .* )\]:(?<port> \d+ )\z/x.freeze # string like "[::1]:80"

0 commit comments

Comments
 (0)