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.



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

def admin=(value)
  @admin = value
end

#cache=(value) ⇒ Object (writeonly)

Sets the attribute cache

Parameters:

  • value

    the value to set the attribute cache to.



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

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

#spiders=(value) ⇒ Object (writeonly)

Sets the attribute spiders

Parameters:

  • value

    the value to set the attribute spiders to.



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

def spiders=(value)
  @spiders = value
end

Instance Method Details

#language(http, available) ⇒ Object

Select best language for user



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dispatcher.rb', line 84

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



77
78
79
80
81
# File 'lib/dispatcher.rb', line 77

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

#runObject

Start FastCGI



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dispatcher.rb', line 34

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 spider? http.agent
        # Search engine spiders (another i18n logic)
        unless http.get['lang'].nil?
          if langs.include? http.get['lang']
            http << @cache.get(http.url, http.get['lang'])
            request.finish
            next
          end
        end
        http.redirect http.url + '?lang=' + language(http, langs)
      else
        # Human visitor
        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))
      end
      request.finish
    end
  end
end

#spider?(agent) ⇒ Boolean

Check HTTP_USER_AGENT value for search engine spiders

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'lib/dispatcher.rb', line 107

def spider?(agent)
  @spiders.each do |spider|
    return true if agent.include? spider
  end
  false
end