Class: Hyde::Response

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

Overview

Rack protocol response wrapper.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • response (Array(Integer, Hash, Array), nil) (defaults to: nil)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hyde/response.rb', line 11

def initialize(response = nil)
  if response
    @status = response[0]
    @headers = response[1]
    @body = response[2]
  else
    @status = 404
    @headers = {}
    @body = []
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



71
72
73
# File 'lib/hyde/response.rb', line 71

def body
  @body
end

#chunk_sizeObject

Returns the value of attribute chunk_size.



8
9
10
# File 'lib/hyde/response.rb', line 8

def chunk_size
  @chunk_size
end

#headersObject

Returns the value of attribute headers.



71
72
73
# File 'lib/hyde/response.rb', line 71

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



71
72
73
# File 'lib/hyde/response.rb', line 71

def status
  @status
end

Class Method Details

.chunk_body(text) ⇒ Array(String)

Turn body into array of chunks

Parameters:

  • text (String)

Returns:

  • (Array(String))


96
97
98
# File 'lib/hyde/response.rb', line 96

def self.chunk_body(text)
  text.chars.each_slice(@chunk_size).map(&:join)
end

.convert(obj) ⇒ Object

Ensure response correctness

Parameters:

Returns:

  • Response



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hyde/response.rb', line 76

def self.convert(obj)
  case obj
  when Response
    obj.validate
  when Array
    Response.new(obj).validate
  when String, File, IO
    Response.new([200,
                  {
                    "content-type" => "text/html"
                  },
                  obj]).validate
  else
    Response.new([404, {}, []])
  end
end

Instance Method Details

#add_header(key, value) ⇒ Object

Add a header to the headers hash

Parameters:

  • key (String)

    header name

  • value (String)

    header value



49
50
51
52
53
54
55
56
57
# File 'lib/hyde/response.rb', line 49

def add_header(key, value)
  if @headers[key].is_a? String
    @headers[key] = [@headers[key], value]
  elsif @headers[key].is_a? Array
    @headers[key].append(value)
  else
    @headers[key] = value
  end
end

#delete_header(key, value = nil) ⇒ Object

Delete a header value from the headers hash If no value is provided, deletes all key entries

Parameters:

  • key (String)

    header name

  • value (String, nil) (defaults to: nil)

    header value



63
64
65
66
67
68
69
# File 'lib/hyde/response.rb', line 63

def delete_header(key, value = nil)
  if value and @response[key]
    @response[key].delete(value)
  else
    @response.delete(key)
  end
end

#finalizeArray(Integer,Hash,Array)

Return internal representation of Rack response

Returns:

  • (Array(Integer,Hash,Array))


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

def finalize
  [@status, @headers, @body]
end

#validateHyde::Response

Make internal representation conformant

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hyde/response.rb', line 31

def validate
  if [204, 304].include?(@status) or (100..199).include?(@status)
    @headers.delete "content-length"
    @headers.delete "content-type"
    @body = []
  elsif @headers.empty?
    @headers = {
      "content-length" => content_size,
      "content-type" => "text/html"
    }
  end
  @body = self.class.chunk_body(@body) if @body.is_a? String
  self
end