Class: EmmyHttp::Response

Inherits:
Object
  • Object
show all
Includes:
ModelPack::Document
Defined in:
lib/emmy_http/response.rb

Instance Method Summary collapse

Constructor Details

#initializeResponse

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

Returns:

  • (Boolean)


75
76
77
# File 'lib/emmy_http/response.rb', line 75

def body?
  !body.empty?
end

#chunked_encoding?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/emmy_http/response.rb', line 30

def chunked_encoding?
  headers['Transfer-Encoding'] =~ /chunked/i
end

#clearObject



19
20
21
22
23
# File 'lib/emmy_http/response.rb', line 19

def clear
  @status  = 0
  @headers = {}
  @body    = ''
end

#compressed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/emmy_http/response.rb', line 38

def compressed?
  headers['Content-Encoding'] =~ /gzip|compressed|deflate/i
end

#contentObject



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_lengthObject



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

Returns:

  • (Boolean)


42
43
44
# File 'lib/emmy_http/response.rb', line 42

def content_length?
  headers['Content-Length'] =~ /\A\d+\z/
end

#content_typeObject



50
51
52
# File 'lib/emmy_http/response.rb', line 50

def content_type
  headers['Content-Type'] =~ /\A([^;]*)/; $1
end

#finishObject



25
26
27
28
# File 'lib/emmy_http/response.rb', line 25

def finish
  @finished = true
  done!
end

#json(options = {}) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/emmy_http/response.rb', line 34

def keepalive?
  headers['Keep-Alive'] =~ /keep-alive/i
end

#locationObject



54
55
56
# File 'lib/emmy_http/response.rb', line 54

def location
  headers['Location']
end

#redirection?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/emmy_http/response.rb', line 79

def redirection?
  300 <= status && 400 > status
end

#to_aObject



83
84
85
# File 'lib/emmy_http/response.rb', line 83

def to_a
  [status, headers, body]
end