Skip to content

Instantly share code, notes, and snippets.

@gusk
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusk/36871a95a07d6f74ac86 to your computer and use it in GitHub Desktop.
Save gusk/36871a95a07d6f74ac86 to your computer and use it in GitHub Desktop.
http_parse
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
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