Class: Rad::Http::Response

Inherits:
Rack::Response
  • Object
show all
Includes:
RubyExt::OpenConstructor
Defined in:
lib/rad/http/_response.rb

Constant Summary collapse

STATUS_MESSAGES =
{
  ok: 200,
  not_found: 404,
  failed: 500,
  error: 500,
  redirect: 302
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Response

Returns a new instance of Response.



12
13
14
15
# File 'lib/rad/http/_response.rb', line 12

def initialize *args
  super
  clear
end

Class Method Details

.decode_status_message(message) ⇒ Object



63
64
65
# File 'lib/rad/http/_response.rb', line 63

def self.decode_status_message message
  STATUS_MESSAGES[message]
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/rad/http/_response.rb', line 40

def == other
  to_a2 == other
end

#body_as_stringObject

def to_a

[status, headers, body]

end



28
29
30
31
32
33
34
# File 'lib/rad/http/_response.rb', line 28

def body_as_string
  if @body.is_a? String
    @body
  else
    @body ? @body.join : ""
  end
end

#clearObject



48
49
50
51
52
53
# File 'lib/rad/http/_response.rb', line 48

def clear
  @status = 200
  @header = Rack::Utils::HeaderHash.new("Content-Type" => nil)
  @length = 0
  @body = []
end

#content_typeObject



18
# File 'lib/rad/http/_response.rb', line 18

def content_type; self["Content-Type"] end

#content_type=(type) ⇒ Object



17
# File 'lib/rad/http/_response.rb', line 17

def content_type= type; self["Content-Type"] = type end

#content_type?Boolean

Returns:

  • (Boolean)


19
# File 'lib/rad/http/_response.rb', line 19

def content_type?; !!content_type end

#cookiesObject



44
45
46
# File 'lib/rad/http/_response.rb', line 44

def cookies
  self['Set-Cookie']
end

#inspectObject



36
37
38
# File 'lib/rad/http/_response.rb', line 36

def inspect
  to_a2.inspect
end

#locationObject



21
# File 'lib/rad/http/_response.rb', line 21

def location; self['Location'] end

#location=(location) ⇒ Object



22
# File 'lib/rad/http/_response.rb', line 22

def location= location; self['Location'] = location end

#redirect?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rad/http/_response.rb', line 76

def redirect?
  super or (body and body.include?('window.location'))
end

#status=(code_or_message) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rad/http/_response.rb', line 55

def status= code_or_message
  @status = if code_or_message.is_a? Numeric
    code_or_message
  else
    self.class.decode_status_message(code_or_message) || raise("unknown http status message '#{code_or_message}'!")
  end
end