This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor request/response classes into appropriate folders
- Loading branch information
Showing
10 changed files
with
95 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Reel | ||
class Request | ||
# Represents the bodies of Requests | ||
class Body | ||
include Enumerable | ||
|
||
def initialize(request) | ||
@request = request | ||
@streaming = nil | ||
@contents = nil | ||
end | ||
|
||
# Read exactly the given amount of data | ||
def read(length) | ||
stream! | ||
@request.read(length) | ||
end | ||
|
||
# Read up to length bytes, but return any data that's available | ||
def readpartial(length = nil) | ||
stream! | ||
@request.readpartial(length) | ||
end | ||
|
||
# Iterate over the body, allowing it to be enumerable | ||
def each | ||
while chunk = readpartial | ||
yield chunk | ||
end | ||
end | ||
|
||
# Eagerly consume the entire body as a string | ||
def to_s | ||
return @contents if @contents | ||
raise StateError, "body is being streamed" unless @streaming.nil? | ||
|
||
begin | ||
@streaming = false | ||
@contents = "" | ||
while chunk = @request.readpartial | ||
@contents << chunk | ||
end | ||
rescue | ||
@contents = nil | ||
raise | ||
end | ||
|
||
@contents | ||
end | ||
|
||
# Easier to interpret string inspect | ||
def inspect | ||
"#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" | ||
end | ||
|
||
# Assert that the body is actively being streamed | ||
def stream! | ||
raise StateError, "body has already been consumed" if @streaming == false | ||
@streaming = true | ||
end | ||
private :stream! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Reel | ||
class Request | ||
class Info | ||
attr_reader :http_method, :url, :http_version, :headers | ||
|
||
def initialize(http_method, url, http_version, headers) | ||
@http_method = http_method | ||
@url = url | ||
@http_version = http_version | ||
@headers = headers | ||
end | ||
|
||
UPGRADE = 'Upgrade'.freeze | ||
WEBSOCKET = 'websocket'.freeze | ||
|
||
def websocket_request? | ||
headers[UPGRADE] && headers[UPGRADE].downcase == WEBSOCKET | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'reel/response/writer' | ||
|
||
module Reel | ||
class Response | ||
include HTTP::Header | ||
|
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
spec/reel/response_writer_spec.rb → spec/reel/response/writer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters