Class: EmmyHttp::Response
- Inherits:
-
Object
- Object
- EmmyHttp::Response
- Includes:
- ModelPack::Document
- Defined in:
- lib/emmy_http/response.rb
Instance Method Summary collapse
- #body? ⇒ Boolean
- #chunked_encoding? ⇒ Boolean
- #clear ⇒ Object
- #compressed? ⇒ Boolean
- #content ⇒ Object
- #content_length ⇒ Object
- #content_length? ⇒ Boolean
- #content_type ⇒ Object
- #finish ⇒ Object
-
#initialize ⇒ Response
constructor
A new instance of Response.
- #json(options = {}) ⇒ Object
- #keepalive? ⇒ Boolean
- #location ⇒ Object
- #redirection? ⇒ Boolean
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
13 14 15 16 17 |
# File 'lib/emmy_http/response.rb', line 13 def initialize on :data do |chunk| body << chunk end end |
Instance Method Details
#body? ⇒ Boolean
75 76 77 |
# File 'lib/emmy_http/response.rb', line 75 def body? !body.empty? end |
#chunked_encoding? ⇒ Boolean
30 31 32 |
# File 'lib/emmy_http/response.rb', line 30 def chunked_encoding? headers['Transfer-Encoding'] =~ /chunked/i end |
#clear ⇒ Object
19 20 21 22 23 |
# File 'lib/emmy_http/response.rb', line 19 def clear @status = 0 @headers = {} @body = '' end |
#compressed? ⇒ Boolean
38 39 40 |
# File 'lib/emmy_http/response.rb', line 38 def compressed? headers['Content-Encoding'] =~ /gzip|compressed|deflate/i end |
#content ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/emmy_http/response.rb', line 58 def content @content ||= case content_type when 'application/json' then json else nil end end |
#content_length ⇒ Object
46 47 48 |
# File 'lib/emmy_http/response.rb', line 46 def content_length content_length? ? headers['Content-Length'].to_i : nil end |
#content_length? ⇒ Boolean
42 43 44 |
# File 'lib/emmy_http/response.rb', line 42 def content_length? headers['Content-Length'] =~ /\A\d+\z/ end |
#content_type ⇒ Object
50 51 52 |
# File 'lib/emmy_http/response.rb', line 50 def content_type headers['Content-Type'] =~ /\A([^;]*)/; $1 end |
#finish ⇒ Object
25 26 27 28 |
# File 'lib/emmy_http/response.rb', line 25 def finish @finished = true done! end |
#json(options = {}) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/emmy_http/response.rb', line 66 def json(={}) raise RequestError, 'Wrong Content-Type' unless content_type == 'application/json' begin JSON.parse(body, ) rescue JSON::ParserError => e raise ParserError, e.to_s end end |
#keepalive? ⇒ Boolean
34 35 36 |
# File 'lib/emmy_http/response.rb', line 34 def keepalive? headers['Keep-Alive'] =~ /keep-alive/i end |
#location ⇒ Object
54 55 56 |
# File 'lib/emmy_http/response.rb', line 54 def location headers['Location'] end |
#redirection? ⇒ Boolean
79 80 81 |
# File 'lib/emmy_http/response.rb', line 79 def redirection? 300 <= status && 400 > status end |
#to_a ⇒ Object
83 84 85 |
# File 'lib/emmy_http/response.rb', line 83 def to_a [status, headers, body] end |