Class: SimpleHttp::SimpleHttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/funky-simplehttp.rb

Constant Summary collapse

SEP =
SimpleHttp::SEP

Instance Method Summary collapse

Constructor Details

#initialize(response_text) ⇒ SimpleHttpResponse

Returns a new instance of SimpleHttpResponse.



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/funky-simplehttp.rb', line 199

def initialize(response_text)
  @response = {}
  @headers = {}
  if response_text.empty?
    @response["header"] = nil
  elsif response_text.include?(SEP + SEP)
    @response["header"], @response["body"] = response_text.split(SEP + SEP, 2)
  else
    @response["header"] = response_text
  end
  parse_header
  self
end

Instance Method Details

#[](key) ⇒ Object



213
# File 'lib/funky-simplehttp.rb', line 213

def [](key); @response[key]; end

#[]=(key, value) ⇒ Object



214
# File 'lib/funky-simplehttp.rb', line 214

def []=(key, value);  @response[key] = value; end

#bodyObject



218
# File 'lib/funky-simplehttp.rb', line 218

def body; @response['body']; end

#codeObject



220
# File 'lib/funky-simplehttp.rb', line 220

def code; @response['code']; end

#content_lengthObject



223
# File 'lib/funky-simplehttp.rb', line 223

def content_length; @response['content-length']; end

#content_typeObject



222
# File 'lib/funky-simplehttp.rb', line 222

def content_type; @response['content-type']; end

#dateObject



221
# File 'lib/funky-simplehttp.rb', line 221

def date; @response['date']; end

#each(&block) ⇒ Object



225
226
227
228
229
# File 'lib/funky-simplehttp.rb', line 225

def each(&block)
  if block
    @response.each do |k,v| block.call(k,v) end
  end
end

#each_name(&block) ⇒ Object



230
231
232
233
234
# File 'lib/funky-simplehttp.rb', line 230

def each_name(&block)
  if block
    @response.each do |k,v| block.call(k) end
  end
end

#headerObject



216
# File 'lib/funky-simplehttp.rb', line 216

def header; @response['header']; end

#headersObject



217
# File 'lib/funky-simplehttp.rb', line 217

def headers; @headers; end

#parse_headerObject

private



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/funky-simplehttp.rb', line 237

def parse_header
  return unless @response["header"]
  h = @response["header"].split(SEP)
  if h[0].include?("HTTP/1")
    @response["status"] = h[0].split(" ", 2).last
    @response["code"]   = h[0].split(" ", 3)[1].to_i
  end
  h.each do |line|
    if line.include?(": ")
      k,v = line.split(": ")
      @response[k.downcase] = v
      @headers[k.downcase] = v
    end
  end
end

#statusObject



219
# File 'lib/funky-simplehttp.rb', line 219

def status; @response['status']; end