Class: Landline::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/landline/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
22
# File 'lib/landline/response.rb', line 11

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



99
100
101
# File 'lib/landline/response.rb', line 99

def body
  @body
end

#chunk_sizeObject

Returns the value of attribute chunk_size.



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

def chunk_size
  @chunk_size
end

#cookiesObject

Returns the value of attribute cookies.



99
100
101
# File 'lib/landline/response.rb', line 99

def cookies
  @cookies
end

#headersObject

Returns the value of attribute headers.



99
100
101
# File 'lib/landline/response.rb', line 99

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



99
100
101
# File 'lib/landline/response.rb', line 99

def status
  @status
end

Class Method Details

.chunk_body(text) ⇒ Array(String)

Turn body into array of chunks

Parameters:

  • text (String)

Returns:

  • (Array(String))


124
125
126
# File 'lib/landline/response.rb', line 124

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

.convert(obj) ⇒ Object

Ensure response correctness

Parameters:

Returns:

  • Response



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/landline/response.rb', line 104

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 a cookie to the response

Parameters:



54
55
56
57
58
59
60
# File 'lib/landline/response.rb', line 54

def add_cookie(cookie)
  if @cookies[cookie.key]
    @cookies[cookie.key].append(cookie)
  else
    @cookies[cookie.key] = [cookie]
  end
end

#add_header(key, value) ⇒ Object

Add a header to the headers hash

Parameters:

  • key (String)

    header name

  • value (String)

    header value



77
78
79
80
81
82
83
84
85
# File 'lib/landline/response.rb', line 77

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 a cookie If no value is provided, deletes all cookies with the same key

Parameters:

  • key (String)

    cookie key

  • value (String, nil)

    cookie value



66
67
68
69
70
71
72
# File 'lib/landline/response.rb', line 66

def delete_cookie(key, value)
  if value
    @cookies[key].delete(value)
  else
    @cookies.delete(key)
  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



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

def delete_header(key, value = nil)
  if value and @headers[key].is_a? Array
    @headers[key].delete(value)
  else
    @headers.delete(key)
  end
end

#finalizeArray(Integer,Hash,Array)

Return internal representation of Rack response

Returns:

  • (Array(Integer,Hash,Array))


26
27
28
29
30
31
32
33
# File 'lib/landline/response.rb', line 26

def finalize
  @cookies.each do |_, cookie_array|
    cookie_array.each do |cookie|
      add_header("set-cookie", cookie.finalize)
    end
  end
  [@status, @headers, @body]
end

#validateLandline::Response

Make internal representation conformant

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/landline/response.rb', line 37

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