Class: Octarine::Request
- Inherits:
-
Object
- Object
- Octarine::Request
- Defined in:
- lib/octarine/request.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
The Rack enviroment hash.
-
#host ⇒ Object
readonly
The host name the request was made to.
-
#input ⇒ Object
readonly
The request POST/PUT body.
-
#method ⇒ Object
readonly
The request method, e.g.
-
#path ⇒ Object
readonly
An Octarine::Path representing the path request was made to.
-
#port ⇒ Object
readonly
The port the request was made to.
Instance Method Summary collapse
-
#[](key) ⇒ Object
:call-seq: request -> header_value.
-
#header ⇒ Object
(also: #headers)
:call-seq: request.header -> hash request.headers -> hash.
-
#initialize(env) ⇒ Request
constructor
:call-seq: Request.new(env) -> request.
-
#to(client, to_path = path, to_input = input) ⇒ Object
:call-seq: request.to(host) -> response request.to(host, path) -> response request.to(host, path, input) -> response.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(env) ⇒ Request
:call-seq: Request.new(env) -> request
Create a Request instance with a Rack enviroment hash.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/octarine/request.rb', line 24 def initialize(env) @env = env env.delete("router") env.delete("router.params") template = env.delete("router.route") @method = env["REQUEST_METHOD"] @host = env["SERVER_NAME"] @port = env["SERVER_PORT"] path = env["SCRIPT_NAME"] || "" path << env["PATH_INFO"] unless env["PATH_INFO"].empty? full_path = path.dup full_path << "?" << env["QUERY_STRING"] unless env["QUERY_STRING"].empty? @path = Path.new(template || path, full_path) @input = env["rack.input"] end |
Instance Attribute Details
#env ⇒ Object (readonly)
The Rack enviroment hash
8 9 10 |
# File 'lib/octarine/request.rb', line 8 def env @env end |
#host ⇒ Object (readonly)
The host name the request was made to
12 13 14 |
# File 'lib/octarine/request.rb', line 12 def host @host end |
#input ⇒ Object (readonly)
The request POST/PUT body
18 19 20 |
# File 'lib/octarine/request.rb', line 18 def input @input end |
#method ⇒ Object (readonly)
The request method, e.g. “GET”, “POST”
10 11 12 |
# File 'lib/octarine/request.rb', line 10 def method @method end |
#path ⇒ Object (readonly)
An Octarine::Path representing the path request was made to
16 17 18 |
# File 'lib/octarine/request.rb', line 16 def path @path end |
#port ⇒ Object (readonly)
The port the request was made to
14 15 16 |
# File 'lib/octarine/request.rb', line 14 def port @port end |
Instance Method Details
#[](key) ⇒ Object
:call-seq: request -> header_value
Retrieve header.
request["Content-Length"] #=> "123"
request["Content-Type"] #=> "application/json"
46 47 48 49 50 51 52 |
# File 'lib/octarine/request.rb', line 46 def [](key) upper_key = key.to_s.tr("a-z-", "A-Z_") unless upper_key == "CONTENT_LENGTH" || upper_key == "CONTENT_TYPE" upper_key[0,0] = "HTTP_" end @env[upper_key] end |
#header ⇒ Object Also known as: headers
:call-seq: request.header -> hash request.headers -> hash
Get header as a hash.
request.header
#=> {"content-length" => "123", "content-type" => "application/json"}
61 62 63 64 65 66 67 |
# File 'lib/octarine/request.rb', line 61 def header Hash[@env.select do |k,v| k =~ /^HTTP_[^(VERSION)]/ || %W{CONTENT_LENGTH CONTENT_TYPE}.include?(k) end.map do |key, value| [key.sub(/HTTP_/, "").tr("_", "-").downcase, value] end] end |
#to(client, to_path = path, to_input = input) ⇒ Object
:call-seq: request.to(host) -> response request.to(host, path) -> response request.to(host, path, input) -> response
Re-issue request to new host/path.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/octarine/request.rb', line 76 def to(client, to_path=path, to_input=input) client = SimpleHTTP.new(client.to_str) if client.respond_to?(:to_str) res = if method == "OPTIONS" && client.respond_to?(:run_request) client.run_request(:options, to_path, nil, header_for_rerequest) elsif %W{POST PUT}.include?(method) client.__send__(method.downcase, to_path, to_input, header_for_rerequest) else client.__send__(method.downcase, to_path, header_for_rerequest) end response_headers = res.headers response_headers.delete("transfer-encoding") response_headers.delete("content-length") Octarine::Response.new(res.body, response_headers, res.status) end |
#to_s ⇒ Object
:nodoc:
91 92 93 94 95 96 97 |
# File 'lib/octarine/request.rb', line 91 def to_s # :nodoc: version = " " + @env["HTTP_VERSION"] if @env.key?("HTTP_VERSION") "#{method} #{path}#{version}\r\n" << header.map do |key, value| key = key.split(/-/).map(&:capitalize).join("-") "#{key}: #{value}" end.join("\r\n") << "\r\n\r\n" end |