Class: Microengine::Dispatcher

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

Overview

Use FastCGI to return XHTML by HTTP request

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#admin=(value) ⇒ Object (writeonly)

Sets the attribute admin

Parameters:

  • value

    the value to set the attribute admin to.



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

def admin=(value)
  @admin = value
end

#cache=(value) ⇒ Object (writeonly)

Sets the attribute cache

Parameters:

  • value

    the value to set the attribute cache to.



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

def cache=(value)
  @cache = value
end

#config=(value) ⇒ Object (writeonly)

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



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

def config=(value)
  @config = value
end

#logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



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

def logger=(value)
  @logger = value
end

Instance Method Details

#language(http, available) ⇒ Object

Select best language for user



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dispatcher.rb', line 70

def language(http, available)
  # Language selected manually
  if not http.get['lang'].nil?
    return http.get['lang'] if available.include? http.get['lang']
  elsif http.cookie['lang']
    return http.cookie['lang'] if available.include? http.cookie['lang']
  end
  
  # Select user language
  http.langs.each do |lang|
    lang.downcase!
    return lang if available.include? lang
  end
  
  # Select any language
  if available.include? @config['default_language']
    @config['default_language']
  else
    available.first
  end
end

#not_found(http) ⇒ Object

Forward to 404 error



63
64
65
66
67
# File 'lib/dispatcher.rb', line 63

def not_found(http)
  http.status = '404 Not Found'
  http << @cache.get('/404/', language(http, @cache.langs['/404/']))
  http.finish
end

#runObject

Start FastCGI



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dispatcher.rb', line 33

def run
  FCGI.each do |request|
    http = HTTP.new(request)
    
    if @admin.dispatch(http)
      request.finish
      next
    else
      langs = @cache.langs[http.url]
      if langs.nil?
        not_found http
        request.finish
        next
      end
      
      if http.get['lang']
        http.set_cookie 'lang', http.get['lang'], 30
        unless langs.include? http.get['lang']
          http.redirect http.url
          request.finish
          next
        end
      end
      http << @cache.get(http.url, language(http, langs))
      request.finish
    end
  end
end