Class: Invoker::Power::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/invoker/power/http_response.rb

Constant Summary collapse

STATUS_MAPS =
{
  200 => "OK",
  201 => "Created",
  202 => "Accepted",
  204 => "No Content",
  205 => "Reset Content",
  206 => "Partial Content",
  301 => "Moved Permanently",
  302 => "Found",
  304 => "Not Modified",
  400 => "Bad Request",
  401 => "Unauthorized",
  402 => "Payment Required",
  403 => "Forbidden",
  404 => "Not Found",
  411 => "Length Required",
  500 => "Internal Server Error",
  501 => "Not Implemented",
  502 => "Bad Gateway",
  503 => "Service Unavailable",
  504 => "Gateway Timeout"
}
HTTP_HEADER_FIELDS =
[
  'Cache-Control', 'Connection', 'Date',
  'Pragma', 'Trailer', 'Transfer-Encoding',
  'Accept-Ranges', 'Age', 'Etag',
  'Server', 'Location', 'Allow',
  'Content-Encoding', 'Content-Language', 'Content-Location',
  'Content-MD5', 'Content-Range',
  'Content-Type', 'Expires',
  'Last-Modified', 'extension-header'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpResponse

Returns a new instance of HttpResponse.



42
43
44
45
46
47
48
# File 'lib/invoker/power/http_response.rb', line 42

def initialize
  @header = {}
  header['Server'] = "Invoker #{Invoker::VERSION}"
  header['Date'] = Time.now.httpdate
  @status = 200
  @body = ""
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



40
41
42
# File 'lib/invoker/power/http_response.rb', line 40

def body
  @body
end

#headerObject

Returns the value of attribute header.



40
41
42
# File 'lib/invoker/power/http_response.rb', line 40

def header
  @header
end

#statusObject

Returns the value of attribute status.



40
41
42
# File 'lib/invoker/power/http_response.rb', line 40

def status
  @status
end

Instance Method Details

#[]=(key, value) ⇒ Object



50
51
52
# File 'lib/invoker/power/http_response.rb', line 50

def []=(key, value)
  header[key] = value
end

#http_stringObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/invoker/power/http_response.rb', line 63

def http_string
  final_string = []
  final_string << "HTTP/1.1 #{status} #{STATUS_MAPS[status]}"

  if header['Transfer-Encoding'].nil? && body.empty?
    header['Content-Length'] = body.length
  end

  HTTP_HEADER_FIELDS.each do |key|
    if value = header[key]
      final_string << "#{key}: #{value}"
    end
  end

  final_string.join("\r\n") + "\r\n\r\n" + body
end

#use_file_as_body(file_name) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/invoker/power/http_response.rb', line 54

def use_file_as_body(file_name)
  if file_name && File.exists?(file_name)
    file_content = File.read(file_name)
    self.body = file_content
  else
    raise Invoker::Errors::InvalidFile, "Invalid file as body"
  end
end