class Invoker::Power::HttpResponse

Constants

HTTP_HEADER_FIELDS
STATUS_MAPS

Attributes

body[RW]
header[RW]
status[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

[]=(key, value) click to toggle source
# File lib/invoker/power/http_response.rb, line 50
def []=(key, value)
  header[key] = value
end
http_string() click to toggle source
# 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) click to toggle source
# File lib/invoker/power/http_response.rb, line 54
def use_file_as_body(file_name)
  if file_name && File.exist?(file_name)
    file_content = File.read(file_name)
    self.body = file_content
  else
    raise Invoker::Errors::InvalidFile, "Invalid file as body"
  end
end