Permalink
Browse files

Move code responsible for reading the response into the request class.

  • Loading branch information...
1 parent 6617639 commit c2faaa2e72462893293282e589d55d4bf29ddb8c @Manfred committed Apr 19, 2012
Showing with 19 additions and 19 deletions.
  1. +17 −5 lib/shl/request.rb
  2. +2 −14 lib/shl/response.rb
View
@@ -1,5 +1,7 @@
module SHL
class Request
+ BUFFER_LENGTH = 4096
+
attr_accessor :verb, :url, :headers, :body
include AttributeInitializer
@@ -12,8 +14,8 @@ def path
@url.path == '' ? '/' : @url.path
end
- def connection
- @connection ||= TCPSocket.new(@url.host, @url.port)
+ def socket
+ @socket ||= TCPSocket.new(@url.host, @url.port)
end
def request_line
@@ -37,10 +39,20 @@ def serialized_body
@body.to_s + NEWLINE
end
+ def raw_response
+ if @raw_response.nil?
+ @raw_response = ''
+ while(data = socket.read(BUFFER_LENGTH))
+ @raw_response << data
+ end
+ socket.close
+ end; @raw_response
+ end
+
def run
- connection.write([request_line,serialized_headers,serialized_body].join(NEWLINE))
- connection.flush
- Response.new(:io=>connection)
+ socket.write([request_line,serialized_headers,serialized_body].join(NEWLINE))
+ socket.flush
+ Response.new(:raw => raw_response)
end
end
end
View
@@ -1,23 +1,11 @@
module SHL
class Response
- BUFFER_LENGTH = 4096
-
- attr_accessor :io
+ attr_accessor :raw
include AttributeInitializer
- def parsed
- if @parsed.nil?
- buffer = ''
- while(data = io.read(BUFFER_LENGTH))
- buffer << data
- end
- @parsed = buffer.split(NEWLINE*2)
- end; @parsed
- end
-
def body
- parsed[1]
+ raw.split(NEWLINE*2)[1]
end
end
end

0 comments on commit c2faaa2

Please sign in to comment.