Class: Headers
- Inherits:
-
Object
- Object
- Headers
- Defined in:
- lib/headers.rb
Overview
Headers - A simple class defining basic attributes and methods for HTTP headers.
Direct Known Subclasses
Instance Method Summary collapse
-
#add_header(key, value) ⇒ Object
Add a header to the @headers hash.
-
#headers ⇒ Object
Return the @headers hash.
-
#initialize ⇒ Headers
constructor
Not much here.
-
#method_missing(method) ⇒ Object
If we reach this method, assume we want to find and return a value from the @headers hash using :method as the key name to look up.
Constructor Details
#initialize ⇒ Headers
Not much here. Instantiate a hash to store HTTP headers.
9 10 11 |
# File 'lib/headers.rb', line 9 def initialize @headers = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
If we reach this method, assume we want to find and return a value from the @headers hash using :method as the key name to look up. It’s a hack, but this is an MVP.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/headers.rb', line 37 def method_missing(method) header_name = method.id2name header_words = header_name.split('_') header_words.each do |word| word.capitalize! end header_name = header_words.join('-') if @headers.has_key?(header_name) # TODO: Add some color to help visually separate output. It'd be slick # if we used a different color based on request/response object. #puts "\e[1;33m#{@headers[header_name]}\e[0;0m" @headers[header_name] end end |
Instance Method Details
#add_header(key, value) ⇒ Object
Add a header to the @headers hash.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/headers.rb', line 14 def add_header(key, value) if @headers.has_key?(key) # If the value is a string, turn it into an array... if @headers[key].is_a?(String) previous_value = @headers[key] @headers[key] = [previous_value, value] # ... else append to the array. else @headers[key] << value end else @headers[key] = value end end |
#headers ⇒ Object
Return the @headers hash.
30 31 32 |
# File 'lib/headers.rb', line 30 def headers @headers end |