|
Revision 8625, 0.6 kB
(checked in by nzkoz, 11 months ago)
|
Provide a nicer way to access headers. request.headersContent-Type? instead of request.headersHTTP_CONTENT_TYPE? [Koz]
|
| Line | |
|---|
| 1 |
module ActionController |
|---|
| 2 |
module Http |
|---|
| 3 |
class Headers < ::Hash |
|---|
| 4 |
|
|---|
| 5 |
def initialize(constructor = {}) |
|---|
| 6 |
if constructor.is_a?(Hash) |
|---|
| 7 |
super() |
|---|
| 8 |
update(constructor) |
|---|
| 9 |
else |
|---|
| 10 |
super(constructor) |
|---|
| 11 |
end |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
def [](header_name) |
|---|
| 15 |
if include?(header_name) |
|---|
| 16 |
super |
|---|
| 17 |
else |
|---|
| 18 |
super(normalize_header(header_name)) |
|---|
| 19 |
end |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
private |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
def normalize_header(header_name) |
|---|
| 27 |
"HTTP_#{header_name.upcase.gsub(/-/, '_')}" |
|---|
| 28 |
end |
|---|
| 29 |
end |
|---|
| 30 |
end |
|---|
| 31 |
end |
|---|