Class: Requests::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/requests/models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



29
30
31
# File 'lib/requests/models.rb', line 29

def initialize
  @history = []
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



27
28
29
# File 'lib/requests/models.rb', line 27

def cookies
  @cookies
end

#elapsedObject

Returns the value of attribute elapsed.



27
28
29
# File 'lib/requests/models.rb', line 27

def elapsed
  @elapsed
end

#encodingObject

Returns the value of attribute encoding.



27
28
29
# File 'lib/requests/models.rb', line 27

def encoding
  @encoding
end

#headersObject

Returns the value of attribute headers.



27
28
29
# File 'lib/requests/models.rb', line 27

def headers
  @headers
end

#historyObject

Returns the value of attribute history.



27
28
29
# File 'lib/requests/models.rb', line 27

def history
  @history
end

#raw_bodyObject

Returns the value of attribute raw_body.



27
28
29
# File 'lib/requests/models.rb', line 27

def raw_body
  @raw_body
end

#reasonObject

Returns the value of attribute reason.



27
28
29
# File 'lib/requests/models.rb', line 27

def reason
  @reason
end

#requestObject

Returns the value of attribute request.



27
28
29
# File 'lib/requests/models.rb', line 27

def request
  @request
end

#status_codeObject

Returns the value of attribute status_code.



27
28
29
# File 'lib/requests/models.rb', line 27

def status_code
  @status_code
end

#urlObject

Returns the value of attribute url.



27
28
29
# File 'lib/requests/models.rb', line 27

def url
  @url
end

Instance Method Details

#apparent_encodingObject



97
98
99
# File 'lib/requests/models.rb', line 97

def apparent_encoding
  encoding
end

#client_error?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/requests/models.rb', line 47

def client_error?
  status_code.to_i.between?(400, 499)
end

#contentObject



53
54
55
# File 'lib/requests/models.rb', line 53

def content
  raw_body
end

#iter_content(chunk_size: 1024) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/requests/models.rb', line 83

def iter_content(chunk_size: 1024)
  return enum_for(:iter_content, chunk_size: chunk_size) unless block_given?
  s = content.to_s
  i = 0
  while i < s.bytesize
    yield s.byteslice(i, chunk_size)
    i += chunk_size
  end
end

#iter_lines(chunk: nil) ⇒ Object



78
79
80
81
# File 'lib/requests/models.rb', line 78

def iter_lines(chunk: nil)
  return enum_for(:iter_lines, chunk: chunk) unless block_given?
  text.each_line { |l| yield l.chomp }
end

#json(**kw) ⇒ Object



65
66
67
68
69
# File 'lib/requests/models.rb', line 65

def json(**kw)
  JSON.parse(text, **kw)
rescue JSON::ParserError => e
  raise Requests::JSONDecodeError.new(build_json_error_message(e), response: self)
end


100
101
102
# File 'lib/requests/models.rb', line 100

def links
  Requests::Utils.parse_header_links(headers['link'])
end

#ok?Boolean Also known as: ok

Returns:

  • (Boolean)


32
33
34
# File 'lib/requests/models.rb', line 32

def ok?
  status_code.to_i < 400
end

#permanent_redirect?Boolean Also known as: is_permanent_redirect?

Returns:

  • (Boolean)


43
44
45
# File 'lib/requests/models.rb', line 43

def permanent_redirect?
  [301, 308].include?(status_code)
end

#raise_for_statusObject Also known as: raise_for_status!



70
71
72
73
74
75
# File 'lib/requests/models.rb', line 70

def raise_for_status
  return self if ok?
  kind = status_code.to_i >= 500 ? 'Server' : 'Client'
  msg = "#{status_code} #{kind} Error: #{reason} for url: #{url}"
  raise Requests::HTTPError.new(msg, response: self)
end

#redirect?Boolean Also known as: is_redirect?

Returns:

  • (Boolean)


39
40
41
# File 'lib/requests/models.rb', line 39

def redirect?
  [301, 302, 303, 307, 308].include?(status_code)
end

#save_to(path) ⇒ Object



92
93
94
95
# File 'lib/requests/models.rb', line 92

def save_to(path)
  File.open(path, 'wb') { |f| f.write(raw_body.to_s.dup.force_encoding(Encoding::ASCII_8BIT)) }
  path
end

#server_error?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/requests/models.rb', line 50

def server_error?
  status_code.to_i >= 500
end

#statusObject



36
37
38
# File 'lib/requests/models.rb', line 36

def status
  status_code
end

#textObject



56
57
58
59
60
61
62
63
64
# File 'lib/requests/models.rb', line 56

def text
  enc = encoding || 'UTF-8'
  s = raw_body.to_s.dup
  begin
    s.force_encoding(enc).encode('UTF-8', invalid: :replace, undef: :replace)
  rescue ArgumentError, Encoding::ConverterNotFoundError
    s.force_encoding('UTF-8')
  end
end