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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/http.rb', line 35

def initialize(request)
    @out = request.out
    @env = request.env
    @agent = request.env['HTTP_USER_AGENT']
    @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

#agentObject (readonly)

Returns the value of attribute agent.



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

def agent
  @agent
end

#contentObject

Returns the value of attribute content.



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

def content
  @content
end

Returns the value of attribute cookie.



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

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.



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

def get
  @get
end

#ipObject (readonly)

Returns the value of attribute ip.



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

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.



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

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



66
67
68
69
# File 'lib/http.rb', line 66

def << (content)
  send_headers unless header_sended?
  @out.print content
end

#finishObject

Finish HTTP request



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

def finish
  @request.finish
end

#header(name, value) ⇒ Object

Set header



83
84
85
# File 'lib/http.rb', line 83

def header(name, value)
  @headers << [name, value]
end

#header_sended?Boolean

Is HTTP headers already sended to user

Returns:

  • (Boolean)


61
62
63
# File 'lib/http.rb', line 61

def header_sended?
  @header_sended
end

#redirect(url) ⇒ Object

Redirect to another URL



72
73
74
75
76
77
78
79
80
# File 'lib/http.rb', line 72

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



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

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)


56
57
58
# File 'lib/http.rb', line 56

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