Class: Curl::Response

Inherits:
Object show all
Defined in:
lib/rhack/curl/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(easy) ⇒ Response

Returns a new instance of Response.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rhack/curl/response.rb', line 25

def initialize(easy)
  @hash = {}
  @timestamp = @date = @header = nil
  if easy.base.error
    @error = easy.base.error
  else
    if headers = easy.header_str || easy.base.headers
      headers /= "\r\n"
      @header = headers.shift
      headers.each {|h|
        h /= ': '
        if h[0]
          h[0].downcase!
          if h[0] == 'set-cookie'
            (@hash.cookies ||= []) << h[1]
          else
            @hash[h[0]] = h[1]
          end
        end
      }
      @timestamp = if @hash.date
          begin
            @date = @hash.date.to_time
          rescue => e
            (@date = Time.now).strftime("%H:%M:%S")
            L < "Error #{e.class}:#{e.message} with @hash.date = #{@hash.date.inspect}"
          end
          @hash.date[/\d\d:\d\d:\d\d/]
        else
          (@date = Time.now).strftime("%H:%M:%S")
        end
    end
    @code	= easy.response_code
    @body	= easy.body_str.dup
    @time	= easy.total_time
  end
  
  @req = {}
  @req.url	        = easy.last_effective_url
  @req.headers	= easy.headers
  if range = easy.headers.Range and range[/(\d+)-(\d+)/]
    @req.range   = $1.to_i .. $2.to_i
  end
  if easy.base and @req.meth = easy.base.last_method and @req.meth.in [:post, :put]
    @req.body	  = easy.post_body.dup
    if @req.meth == :post
      @req.mp	  = easy.multipart_form_post?
    end
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def code
  @code
end

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def date
  @date
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def error
  @error
end

#hashObject (readonly) Also known as: headers

Returns the value of attribute hash.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def hash
  @hash
end

#headerObject (readonly)

Returns the value of attribute header.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def header
  @header
end

#reqObject (readonly)

Returns the value of attribute req.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def req
  @req
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def time
  @time
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/rhack/curl/response.rb', line 6

def timestamp
  @timestamp
end

Instance Method Details

#[](key_or_index) ⇒ Object



84
85
86
# File 'lib/rhack/curl/response.rb', line 84

def [](key_or_index)
  @error ? @error[key_or_index] : @hash[key_or_index.downcase]
end

#is(klass) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rhack/curl/response.rb', line 76

def is(klass)
  if @error
    klass == Array || klass = Curl::Response
  else
    klass == Curl::Response
  end
end

#to_sObject Also known as: inspect



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rhack/curl/response.rb', line 8

def to_s
  str = '<#'
  if @error
    str << "#{@error[0].self_name}: #{@error[1]}"
  else
    str << (@header[/\d{3}/] == @code.to_s ? @header : "#{@header[/\S+/]} #{@code}") if @header
    if @hash.location
      str << ' '+@req.url if $panic
      str << ' -> '+@hash.location 
    end
    str << " (#{@body ? @body.size.bytes : 'No'} Body)"
    str << " [#{@timestamp}]" if @timestamp
  end
  str << '>'
end