Last active
August 29, 2015 14:01
-
-
Save gusk/36871a95a07d6f74ac86 to your computer and use it in GitHub Desktop.
http_parse
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
class HttpParser | |
def initialize(response) | |
@response = response | |
end | |
def headers | |
bar = {} | |
rar = {} | |
array = @response.lines | |
foo = array[1..2] | |
first = foo[0].strip | |
value = first.split(": ") | |
bar[value[0]] = value[1] | |
second = foo[1].strip | |
value2 = second.split(": ") | |
rar[value2[0]] = value2[1] | |
bar.merge(rar) | |
end | |
def body | |
array = @response.split("\n\n") | |
array[1].strip | |
end | |
def status_code | |
array = @response.split("\n") | |
foo = array[0] | |
bar = foo.scan(/(\d{3})/).flatten | |
bar.join.to_i | |
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
class HttpParser | |
def initialize(response) | |
@response = response | |
end | |
def headers | |
headers = @response.lines | |
line_separator = headers.index("\n") | |
bar = {} | |
headers[1...line_separator].each do |line| | |
key, value = line.split(": ") | |
bar[key] = value.strip | |
end | |
bar | |
end | |
def body | |
array = @response.split("\n\n") | |
array[1].strip | |
end | |
def status_code | |
array = @response.split("\n") | |
first_line = array.first | |
first_line.match(/(\d{3})/)[1].to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment