Class: Stella::Data::HTTP::Response

Inherits:
Storable
  • Object
show all
Includes:
Gibbler::Complex
Defined in:
lib/stella/data/http/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data = nil) ⇒ Response

Returns a new instance of Response.



19
20
21
22
# File 'lib/stella/data/http/response.rb', line 19

def initialize(raw_data=nil)
  @raw_data = raw_data
  parse(@raw_data)
end

Instance Attribute Details

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



8
9
10
# File 'lib/stella/data/http/response.rb', line 8

def raw_data
  @raw_data
end

Instance Method Details

#bodyObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stella/data/http/response.rb', line 40

def body
  return nil unless @body
  #TODO: Move to HTTPResponse::Body.to_s
  if is_binary?
   "[skipping binary content]"
  elsif is_gzip?
    #require 'zlib'
    #Zlib::Inflate.inflate(@body)
     "[skipping gzip content]"
  else
    @body
  end
end

#cookiesObject



87
88
89
90
# File 'lib/stella/data/http/response.rb', line 87

def cookies
  return [] unless header.is_a?(Array) && !header[:Set_Cookie].empty?
  header[:Set_Cookie] 
end

#has_body?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/stella/data/http/response.rb', line 29

def has_body?
  !@body.nil? && !@body.empty?
end

#has_request?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/stella/data/http/response.rb', line 32

def has_request?
  false
end

#has_response?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/stella/data/http/response.rb', line 35

def has_response?
  false
end

#headersObject



54
55
56
57
58
59
60
# File 'lib/stella/data/http/response.rb', line 54

def headers
  headers = []
  header.each_pair do |n,v|
    headers << [n.to_s.gsub('_', '-'), v[0]]
  end
  headers
end

#inspectObject



74
75
76
77
78
79
# File 'lib/stella/data/http/response.rb', line 74

def inspect
  str = "HTTP/%s %s (%s)" % [@http_version, @status, @message]
  str << $/ + headers.join($/)
  str << $/ + $/ + body if body
  str
end

#is_binary?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/stella/data/http/response.rb', line 62

def is_binary?
  (!is_text?) == true
end

#is_gzip?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/stella/data/http/response.rb', line 70

def is_gzip?
  (!header[:Content_Encoding].nil? && (header[:Content_Encoding][0].is_a? String) && header[:Content_Encoding][0][/gzip/] != nil)
end

#is_text?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/stella/data/http/response.rb', line 66

def is_text?
  (!header[:Content_Type].nil? && (header[:Content_Type][0].is_a? String) && header[:Content_Type][0][/text/] != nil)
end

#parse(raw) ⇒ Object



24
25
26
27
# File 'lib/stella/data/http/response.rb', line 24

def parse(raw)
  return unless raw
  @status, @http_version, @message, @header, @body = HTTPUtil::parse_http_response(raw)
end

#to_sObject



81
82
83
84
# File 'lib/stella/data/http/response.rb', line 81

def to_s
  str = "%s: HTTP/%s %s (%s)" % [time.strftime(NICE_TIME_FORMAT), @http_version, @status, @message]
  str 
end