Class: HQ::Web::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/web/container.rb

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



7
8
# File 'lib/hq/web/container.rb', line 7

def initialize
end

Instance Method Details

#get_provider(app_elem) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hq/web/container.rb', line 104

def get_provider app_elem

  require \
    app_elem.attributes["require"]

  provider_class =
    eval app_elem.attributes["class"]

  provider =
    provider_class.get_provider app_elem

  return provider

end

#handle(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hq/web/container.rb', line 61

def handle env

  $stdout.sync = true
  $stderr.sync = true

  # iterate all apps

  @hq_apps.each do |app|

    # find app which matches path

    match =
      app[:pattern].match env["PATH_INFO"]

    next unless match

    # create hash from params

    params = Hash[
      app[:params].each_with_index.map { |name, index|
        [ name.to_sym, match[index + 1] ]
      }
    ]

    # call that app's provider

    return app[:provider].call env, params

  end

  # default error page

  headers = {
    "Content-Type" => "text/html",
  }

  body = [
    "404 Not found",
  ]

  return [ 404, headers, body ]
end

#init(config_path) ⇒ Object



10
11
12
13
# File 'lib/hq/web/container.rb', line 10

def init config_path
  load_config config_path
  init_apps
end

#init_app(app_elem) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hq/web/container.rb', line 40

def init_app app_elem

  return {

    pattern:
      Regexp.new("^#{app_elem.attributes["pattern"]}$"),

    params:
      app_elem.find("param").to_a \
        .map {
          |param_elem|
          param_elem.attributes["name"]
        },

    provider:
      get_provider(app_elem)

  }

end

#init_appsObject



30
31
32
33
34
35
36
37
38
# File 'lib/hq/web/container.rb', line 30

def init_apps

  @hq_apps =
    @config_elem
      .find("app")
      .to_a
      .map { |app_elem| init_app app_elem }

end

#load_config(config_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hq/web/container.rb', line 15

def load_config config_path

  config_doc =
    XML::Document.file config_path

  config_elem =
    config_doc.root

  config_elem.name == "hq-web" \
    or raise "Error"

  @config_elem =
    config_elem
end