Class: Microengine::HTTP

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

Overview

Basic operation with HTTP request

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = format_cookie request.env['HTTP_COOKIE']
    @headers = []
    @status = '200 OK'
    @content = 'text/html'
    @request = request
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

Returns the value of attribute cookie.



29
30
31
# File 'lib/http.rb', line 29

def cookie
  @cookie
end

#envObject (readonly)

Returns the value of attribute env.



24
25
26
# File 'lib/http.rb', line 24

def env
  @env
end

#getObject (readonly)

Returns the value of attribute get.



30
31
32
# File 'lib/http.rb', line 30

def get
  @get
end

#ipObject (readonly)

Returns the value of attribute ip.



28
29
30
# File 'lib/http.rb', line 28

def ip
  @ip
end

#langsObject (readonly)

Returns the value of attribute langs.



27
28
29
# File 'lib/http.rb', line 27

def langs
  @langs
end

#postObject (readonly)

Returns the value of attribute post.



26
27
28
# File 'lib/http.rb', line 26

def post
  @post
end

#statusObject

Returns the value of attribute status.



31
32
33
# File 'lib/http.rb', line 31

def status
  @status
end

#urlObject (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

#finishObject

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

Returns:

  • (Boolean)


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



86
87
88
89
90
91
92
93
94
# File 'lib/http.rb', line 86

def set_cookie(name, value, days=nil)
  cookie = name + '=' + value + '; path=/'
  unless days.nil?
    time = Time.now + (days * 86400)
    cookie += '; expires=' + time.gmtime.strftime('%a %d-%b-%Y %H:%M:S %Z')
  end
  header 'Set-Cookie', cookie
  @cookie[name] = value
end

#url_start?(string) ⇒ Boolean

Is URL start with string

Returns:

  • (Boolean)


54
55
56
# File 'lib/http.rb', line 54

def url_start?(string)
  @url[0...string.length] == string
end