Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respond with a response object? #1

Merged
merged 2 commits into from
Oct 12, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
.rvmrc
2 changes: 2 additions & 0 deletions lib/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'http/client'
require 'http/mime_type'
require 'http/parameters'
require 'http/response'


# THIS IS ENTIRELY TEMPORARY, I ASSURE YOU
require 'net/https'
Expand Down
3 changes: 2 additions & 1 deletion lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ def request(verb, options = {})

case options[:response]
when :parsed_body
parse_response response
response.body = parse_response(response)
else
response.body
end
Http::Response.new(response.body, response.code)
end

# Parse the response body according to its content type
Expand Down
11 changes: 11 additions & 0 deletions lib/http/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Http
class Response
attr_accessor :body
attr_accessor :status

def initialize(body, status)
@body = body
@status = status.to_i
end
end
end
6 changes: 3 additions & 3 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
it "should be easy" do
# Fuck it, we'll do it live! (Testing against WEBRick or something coming soon)
response = Http.get "http://www.google.com"
response.should match(/<!doctype html>/)
response.body.should match(/<!doctype html>/)
end

context "with headers" do
it "should be easy" do
response = Http.accept(:json).get("https://github.com/tarcieri/http/commit/HEAD")

# Congratulations first committer, your prize is to break the build!
response['commit']['author']['name'].should == "Tony Arcieri"
response.body['commit']['author']['name'].should == "Tony Arcieri"
end
end
end
Expand All @@ -24,7 +24,7 @@
fragment = "<!doctype html><html><head><title>example</title></head></html>"
response = Http.post "http://validator.w3.org/check", :form => {:fragment => fragment}

response.should match(/HTML5/)
response.body.should match(/HTML5/)
end
end
end