Class: Requests::Response
- Inherits:
-
Object
- Object
- Requests::Response
- Defined in:
- lib/requests/models.rb
Instance Attribute Summary collapse
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#elapsed ⇒ Object
Returns the value of attribute elapsed.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#history ⇒ Object
Returns the value of attribute history.
-
#raw_body ⇒ Object
Returns the value of attribute raw_body.
-
#reason ⇒ Object
Returns the value of attribute reason.
-
#request ⇒ Object
Returns the value of attribute request.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #apparent_encoding ⇒ Object
- #client_error? ⇒ Boolean
- #content ⇒ Object
-
#initialize ⇒ Response
constructor
A new instance of Response.
- #iter_content(chunk_size: 1024) ⇒ Object
- #iter_lines(chunk: nil) ⇒ Object
- #json(**kw) ⇒ Object
- #links ⇒ Object
- #ok? ⇒ Boolean (also: #ok)
- #permanent_redirect? ⇒ Boolean (also: #is_permanent_redirect?)
- #raise_for_status ⇒ Object (also: #raise_for_status!)
- #redirect? ⇒ Boolean (also: #is_redirect?)
- #save_to(path) ⇒ Object
- #server_error? ⇒ Boolean
- #status ⇒ Object
- #text ⇒ Object
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
29 30 31 |
# File 'lib/requests/models.rb', line 29 def initialize @history = [] end |
Instance Attribute Details
#cookies ⇒ Object
Returns the value of attribute cookies.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def @cookies end |
#elapsed ⇒ Object
Returns the value of attribute elapsed.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def elapsed @elapsed end |
#encoding ⇒ Object
Returns the value of attribute encoding.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def encoding @encoding end |
#headers ⇒ Object
Returns the value of attribute headers.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def headers @headers end |
#history ⇒ Object
Returns the value of attribute history.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def history @history end |
#raw_body ⇒ Object
Returns the value of attribute raw_body.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def raw_body @raw_body end |
#reason ⇒ Object
Returns the value of attribute reason.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def reason @reason end |
#request ⇒ Object
Returns the value of attribute request.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def request @request end |
#status_code ⇒ Object
Returns the value of attribute status_code.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def status_code @status_code end |
#url ⇒ Object
Returns the value of attribute url.
27 28 29 |
# File 'lib/requests/models.rb', line 27 def url @url end |
Instance Method Details
#apparent_encoding ⇒ Object
97 98 99 |
# File 'lib/requests/models.rb', line 97 def apparent_encoding encoding end |
#client_error? ⇒ Boolean
47 48 49 |
# File 'lib/requests/models.rb', line 47 def client_error? status_code.to_i.between?(400, 499) end |
#content ⇒ Object
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((e), response: self) end |
#links ⇒ Object
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
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?
43 44 45 |
# File 'lib/requests/models.rb', line 43 def permanent_redirect? [301, 308].include?(status_code) end |
#raise_for_status ⇒ Object 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?
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
50 51 52 |
# File 'lib/requests/models.rb', line 50 def server_error? status_code.to_i >= 500 end |
#status ⇒ Object
36 37 38 |
# File 'lib/requests/models.rb', line 36 def status status_code end |
#text ⇒ Object
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 |