Class: Microengine::HTTP
- Inherits:
-
Object
- Object
- Microengine::HTTP
- Defined in:
- lib/http.rb
Overview
Basic operation with HTTP request
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#get ⇒ Object
readonly
Returns the value of attribute get.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#langs ⇒ Object
readonly
Returns the value of attribute langs.
-
#post ⇒ Object
readonly
Returns the value of attribute post.
-
#status ⇒ Object
Returns the value of attribute status.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#<<(content) ⇒ Object
Send content to user.
-
#finish ⇒ Object
Finish HTTP request.
-
#header(name, value) ⇒ Object
Set header.
-
#header_sended? ⇒ Boolean
Is HTTP headers already sended to user.
-
#initialize(request) ⇒ HTTP
constructor
A new instance of HTTP.
-
#redirect(url) ⇒ Object
Redirect to another URL.
-
#set_cookie(name, value, days = nil) ⇒ Object
Set cookie.
-
#url_start?(string) ⇒ Boolean
Is URL start with string.
Constructor Details
#initialize(request) ⇒ HTTP
Returns a new instance of HTTP.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/http.rb', line 34 def initialize(request) @out = request.out @env = request.env @ip = request.env['REMOTE_ADDR'] @langs = format_langs request.env['HTTP_ACCEPT_LANGUAGE'] @url = normalize_url format_url(request.env['REQUEST_URI']) @get = format_get request.env['QUERY_STRING'] @cookie = request.env['HTTP_COOKIE'] @headers = [] @status = '200 OK' @content = 'text/html' @request = request end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
32 33 34 |
# File 'lib/http.rb', line 32 def content @content end |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
29 30 31 |
# File 'lib/http.rb', line 29 def @cookie end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
24 25 26 |
# File 'lib/http.rb', line 24 def env @env end |
#get ⇒ Object (readonly)
Returns the value of attribute get.
30 31 32 |
# File 'lib/http.rb', line 30 def get @get end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
28 29 30 |
# File 'lib/http.rb', line 28 def ip @ip end |
#langs ⇒ Object (readonly)
Returns the value of attribute langs.
27 28 29 |
# File 'lib/http.rb', line 27 def langs @langs end |
#post ⇒ Object (readonly)
Returns the value of attribute post.
26 27 28 |
# File 'lib/http.rb', line 26 def post @post end |
#status ⇒ Object
Returns the value of attribute status.
31 32 33 |
# File 'lib/http.rb', line 31 def status @status end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
25 26 27 |
# File 'lib/http.rb', line 25 def url @url end |
Instance Method Details
#<<(content) ⇒ Object
Send content to user
64 65 66 67 |
# File 'lib/http.rb', line 64 def << (content) send_headers unless header_sended? @out.print content end |
#finish ⇒ Object
Finish HTTP request
97 98 99 |
# File 'lib/http.rb', line 97 def finish @request.finish end |
#header(name, value) ⇒ Object
Set header
81 82 83 |
# File 'lib/http.rb', line 81 def header(name, value) @headers << [name, value] end |
#header_sended? ⇒ Boolean
Is HTTP headers already sended to user
59 60 61 |
# File 'lib/http.rb', line 59 def header_sended? @header_sended end |
#redirect(url) ⇒ Object
Redirect to another URL
70 71 72 73 74 75 76 77 78 |
# File 'lib/http.rb', line 70 def redirect(url) @status = '301 Moved Permanently' if '/' == url[0] url = 'http://' + @env['HTTP_HOST'] + url end header 'Location', url send_headers finish end |
#set_cookie(name, value, days = nil) ⇒ Object
Set cookie
86 87 88 89 90 91 92 93 94 |
# File 'lib/http.rb', line 86 def (name, value, days=nil) = name + '=' + value + '; path=/' unless days.nil? time = Time.now + (days * 86400) += '; expires=' + time.gmtime.strftime('%a %d-%b-%Y %H:%M:S %Z') end header 'Set-Cookie', @cookie[name] = value end |
#url_start?(string) ⇒ Boolean
Is URL start with string
54 55 56 |
# File 'lib/http.rb', line 54 def url_start?(string) @url[0...string.length] == string end |