Skip to content

Commit

Permalink
Delegate all request methods to the request
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hunt committed Dec 29, 2013
1 parent ed865f1 commit 4e7e7db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
47 changes: 32 additions & 15 deletions lib/hi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

module Hi
class Request
extend Forwardable

ATTRIBUTES = [
:host,
:ip,
:port,
:request_method,
:scheme,
:url,
:query_string,
:body,
:content_length,
:media_type,
:referer,
:user_agent,
:xhr?
]

def_delegators :request, *ATTRIBUTES
attr_reader :env, :request

def initialize(env)
Expand All @@ -13,23 +32,21 @@ def headers
env.select { |key| key.start_with? 'HTTP_' }
end

def body_string
body.string if body
end

def to_h
{
host: request.host,
ip: request.ip,
port: request.port,
request_method: request.request_method,
scheme: request.scheme,
url: request.url,
query_string: request.query_string,
body: (request.body.string if request.body),
content_length: request.content_length,
media_type: request.media_type,
referer: request.referer,
user_agent: request.user_agent,
xhr: request.xhr?,
request_hash.merge({
body: body_string,
headers: headers,
}
})
end

private

def request_hash
ATTRIBUTES.inject({}) { |hash, attr| hash[attr] = send(attr); hash }
end
end
end
10 changes: 10 additions & 0 deletions spec/hi/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@
expect(request.headers).to eq headers
end
end

describe '#host' do
it 'returns the host passed in the environment' do
host = 'amazing.dev'

request = described_class.new('HTTP_HOST' => host)

expect(request.host).to eq host
end
end
end

0 comments on commit 4e7e7db

Please sign in to comment.