Class: Octarine::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/octarine/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#envObject (readonly)

The Rack enviroment hash



8
9
10
# File 'lib/octarine/request.rb', line 8

def env
  @env
end

#hostObject (readonly)

The host name the request was made to



12
13
14
# File 'lib/octarine/request.rb', line 12

def host
  @host
end

#inputObject (readonly)

The request POST/PUT body



18
19
20
# File 'lib/octarine/request.rb', line 18

def input
  @input
end

#methodObject (readonly)

The request method, e.g. “GET”, “POST”



10
11
12
# File 'lib/octarine/request.rb', line 10

def method
  @method
end

#pathObject (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

#portObject (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

#headerObject 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
# 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 %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_sObject

:nodoc:



89
90
91
92
93
94
95
# File 'lib/octarine/request.rb', line 89

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