Class: Spyder::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/spyder/response.rb

Constant Summary collapse

CODE_TO_REASON_SENTENCE =
{
  100 => 'Continue',
  101 => 'Switching Protocols',
  200 => 'OK',
  201 => 'Created',
  202 => 'Accepted',
  203 => 'Non-Authoritative Information',
  204 => 'No Content',
  205 => 'Reset Content',
  206 => 'Partial Content',
  300 => 'Multiple Choices',
  301 => 'Moved Permanently',
  302 => 'Found',
  303 => 'See Other',
  304 => 'Not Modified',
  305 => 'Use Proxy',
  307 => 'Temporary Redirect',
  400 => 'Bad Request',
  401 => 'Unauthorized',
  402 => 'Payment Required',
  403 => 'Forbidden',
  404 => 'Not Found',
  405 => 'Method Not Allowed',
  406 => 'Not Acceptable',
  407 => 'Proxy Authentication Required',
  408 => 'Request Timeout',
  409 => 'Conflict',
  410 => 'Gone',
  411 => 'Length Required',
  412 => 'Precondition Failed',
  413 => 'Payload Too Large',
  414 => 'URI Too Long',
  415 => 'Unsupported Media Type',
  416 => 'Range Not Satisfiable',
  417 => 'Expectation Failed',
  418 => 'I\'m a teapot',
  422 => 'Unprocessable Entity',
  426 => 'Upgrade Required',
  500 => 'Internal Server Error',
  501 => 'Not Implemented',
  502 => 'Bad Gateway',
  503 => 'Service Unavailable',
  504 => 'Gateway Timeout',
  505 => 'HTTP Version Not Supported',
}.freeze
SYMBOL_TO_CODE =
CODE_TO_REASON_SENTENCE.to_a.to_h do |code, name|
  [
    name.downcase.tr(' ', '_').tr("'", '').to_sym,
    code
  ]
end.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



63
64
65
66
# File 'lib/spyder/response.rb', line 63

def initialize
  self.code = :ok
  @headers = HeaderStore.new(:response)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/spyder/response.rb', line 6

def body
  @body
end

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/spyder/response.rb', line 5

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/spyder/response.rb', line 7

def headers
  @headers
end

#reason_sentenceObject



86
87
88
# File 'lib/spyder/response.rb', line 86

def reason_sentence
  @reason_sentence || CODE_TO_REASON_SENTENCE[@code]
end

Class Method Details

.make_generic(code, payload = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/spyder/response.rb', line 90

def self.make_generic(code, payload=nil)
  new.tap do |r|
    code = SYMBOL_TO_CODE.fetch(code, code)
    r.body = (payload || "#{code} #{CODE_TO_REASON_SENTENCE[code]}")
    r.code = code
    r.add_standard_headers
    r.nocache!
  end
end

Instance Method Details

#add_standard_headersObject



68
69
70
71
# File 'lib/spyder/response.rb', line 68

def add_standard_headers
  set_header 'date', Time.now.httpdate
  set_header 'server', "spyder/#{::Spyder::VERSION}"
end

#nocache!Object



73
74
75
76
# File 'lib/spyder/response.rb', line 73

def nocache!
  set_header 'expires', Time.at(0).httpdate
  set_header 'cache-control', 'private, no-store, no-cache'
end

#set_header(key, value) ⇒ Object



82
83
84
# File 'lib/spyder/response.rb', line 82

def set_header(key, value)
  @headers.set_header(key, value)
end