class EmmyHttp::Response

Public Class Methods

new() click to toggle source
# File lib/emmy_http/response.rb, line 13
def initialize
  on :data do |chunk|
    body << chunk
  end
end

Public Instance Methods

body?() click to toggle source
# File lib/emmy_http/response.rb, line 75
def body?
  !body.empty?
end
chunked_encoding?() click to toggle source
# File lib/emmy_http/response.rb, line 30
def chunked_encoding?
  headers['Transfer-Encoding'] =~ /chunked/i
end
clear() click to toggle source
# File lib/emmy_http/response.rb, line 19
def clear
  @status  = 0
  @headers = {}
  @body    = ''
end
compressed?() click to toggle source
# File lib/emmy_http/response.rb, line 38
def compressed?
  headers['Content-Encoding'] =~ /gzip|compressed|deflate/i
end
content() click to toggle source
# 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() click to toggle source
# File lib/emmy_http/response.rb, line 46
def content_length
  content_length? ? headers['Content-Length'].to_i : nil
end
content_length?() click to toggle source
# File lib/emmy_http/response.rb, line 42
def content_length?
  headers['Content-Length'] =~ /\A\d+\z/
end
content_type() click to toggle source
# File lib/emmy_http/response.rb, line 50
def content_type
  headers['Content-Type'] =~ /\A([^;]*)/; $1
end
finish() click to toggle source
# File lib/emmy_http/response.rb, line 25
def finish
  @finished = true
  done!
end
json(options={}) click to toggle source
# File lib/emmy_http/response.rb, line 66
def json(options={})
  raise RequestError, 'Wrong Content-Type' unless content_type == 'application/json'
  begin
    JSON.parse(body, options)
  rescue JSON::ParserError => e
    raise ParserError, e.to_s
  end
end
keepalive?() click to toggle source
# File lib/emmy_http/response.rb, line 34
def keepalive?
  headers['Keep-Alive'] =~ /keep-alive/i
end
location() click to toggle source
# File lib/emmy_http/response.rb, line 54
def location
  headers['Location']
end
redirection?() click to toggle source
# File lib/emmy_http/response.rb, line 79
def redirection?
  300 <= status && 400 > status
end
to_a() click to toggle source
# File lib/emmy_http/response.rb, line 83
def to_a
  [status, headers, body]
end