Class: Stylo::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Response

Returns a new instance of Response.



3
4
5
6
# File 'lib/stylo/response.rb', line 3

def initialize(path)
  @headers = {}
  @path = path
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#buildObject



36
37
38
# File 'lib/stylo/response.rb', line 36

def build
  [200, headers, self]
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/stylo/response.rb', line 40

def each(&block)
  block.call(body)
end

#extensionObject



14
15
16
# File 'lib/stylo/response.rb', line 14

def extension
  File.extname @path
end

#has_content?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/stylo/response.rb', line 10

def has_content?
  !body.nil?
end

#set_body(content, content_type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stylo/response.rb', line 18

def set_body(content, content_type)
  @body = content

  set_header 'Content-Length', content.length.to_s
  set_header "Content-Type", case content_type
                               when :css
                                 'text/css'
                               when :javascript
                                 'text/javascript'
                               else
                                 raise "Unknown content type #{content_type}"
                             end
end

#set_header(name, value) ⇒ Object



32
33
34
# File 'lib/stylo/response.rb', line 32

def set_header(name, value)
  @headers[name] = value
end