Class: Matryoshka::Answer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rack_env, chained_application) ⇒ Answer

Returns a new instance of Answer.



5
6
7
8
9
10
# File 'lib/matryoshka/answer.rb', line 5

def initialize(rack_env, chained_application)
  self.env = rack_env
  self.chained_application = chained_application
  
  self.data = Matryoshka::Data.new(env['HTTP_HOST'])
end

Instance Attribute Details

#chained_applicationObject

Returns the value of attribute chained_application.



3
4
5
# File 'lib/matryoshka/answer.rb', line 3

def chained_application
  @chained_application
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/matryoshka/answer.rb', line 3

def data
  @data
end

#envObject

Returns the value of attribute env.



3
4
5
# File 'lib/matryoshka/answer.rb', line 3

def env
  @env
end

Instance Method Details

#context_paths(path_to_context_array = path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/matryoshka/answer.rb', line 96

def context_paths(path_to_context_array = path)
  segments = path_to_context_array.split('/')
  
  # Remove the last element (the requested page)
  # Empty array only happens on '/'. No context. Shortcut out.
  return [] unless segments.pop
  
  paths = []
  
  # ['',1,2,3] becomes ['/','/1','/1/2','/1/2/3']
  segments.each_with_index do |segment,index|
    paths << segments[0..index].join('/')
  end
  
  paths # paths.collect {|p| p + '/index.html'}
end

#context_templates(opts = {}) ⇒ Object

Get file_data and full_path from here for a given context



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/matryoshka/answer.rb', line 114

def context_templates(opts = {})
  return @context_templates if @context_templates
  options =  {:path_array => context_paths, 
              :file_name => 'index', 
              :current_handler => handler}.merge(opts)
  path_array = options[:path_array]
  @context_templates = path_array.collect do |p|
    unless "#{p}/#{options[:file_name]}" == path_without_extention
      load_template("#{p}/#{options[:file_name]}", options[:current_handler])
    end
  end.compact
end

#delegate_handlerObject



154
155
156
# File 'lib/matryoshka/answer.rb', line 154

def delegate_handler
  Matryoshka::Document::Delegate
end

#fallback_handlerObject

This is for when a request makes it here, but cannot be handled.



150
151
152
# File 'lib/matryoshka/answer.rb', line 150

def fallback_handler
  Matryoshka::Document::Unknown
end

#handlerObject



135
136
137
# File 'lib/matryoshka/answer.rb', line 135

def handler
  matryoshka_handler or fallback_handler
end

#load_template(extention_optional_path, current_handler = handler) ⇒ Object

The request path, and the current handler



128
129
130
131
132
133
# File 'lib/matryoshka/answer.rb', line 128

def load_template(extention_optional_path,current_handler = handler)
  rack_arr = data.find extention_optional_path, current_handler.extentions
  if rack_arr
    current_handler.new(rack_arr)
  end
end

#local_templateObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/matryoshka/answer.rb', line 23

def local_template
  return false unless template = load_template(path_with_index)
  if template.proxy?
    context_templates << template
    proxy_template
  else
    template
  end
  
end

#matryoshka_handlerObject



143
144
145
146
147
# File 'lib/matryoshka/answer.rb', line 143

def matryoshka_handler
  # handler = Matryoshka::Document.detect_handler(@env)
  # handler.new(nil) if handler
  Matryoshka::Document.detect_handler(env)
end

#matryoshkable?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/matryoshka/answer.rb', line 139

def matryoshkable?
  matryoshka_handler
end

#merge(a, b) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/matryoshka/answer.rb', line 81

def merge(a,b)
  handler.new [
     b.status,
     b.headers,
     handler::Merge.new(a,b).run
    ]
end

#merged_contextObject



89
90
91
92
93
94
# File 'lib/matryoshka/answer.rb', line 89

def merged_context
  @merged_context ||=
  context_templates.inject do |result, template|
    result = merge(result,template)
  end
end

#not_found_templateObject



42
43
44
45
# File 'lib/matryoshka/answer.rb', line 42

def not_found_template
  # For now ...
  handler.new [404,{},'<div id="content">404\'d!</div>']
end

#pathObject



65
66
67
# File 'lib/matryoshka/answer.rb', line 65

def path
  env['REQUEST_URI']
end

#path_with_indexObject



73
74
75
76
77
78
79
# File 'lib/matryoshka/answer.rb', line 73

def path_with_index
  if path[/\/\Z/] # path ends with /
    path + 'index'
  else
    path
  end
end

#path_without_extentionObject



69
70
71
# File 'lib/matryoshka/answer.rb', line 69

def path_without_extention
  path.sub(/\.\w+\Z/,'')
end

#proxy_templateObject



34
35
36
37
38
39
40
# File 'lib/matryoshka/answer.rb', line 34

def proxy_template
  top_context_template = context_templates.last
  if (top_context_template and top_context_template.proxy?)
    proxy = top_context_template.prepare_proxy(path)
    proxy
  end
end

#requested_pageObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/matryoshka/answer.rb', line 12

def requested_page
  @requested_page ||=
  if chained_application # Rails or other Framework handles page
    handler.new(chained_application.call(env))
  else
    local_template or
    proxy_template or
    not_found_template
  end
end

#to_rackObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/matryoshka/answer.rb', line 47

def to_rack
  # can we handle this request? if not, just pass it along
  return requested_page.call(env) unless matryoshkable?
  
  # gather the files necessary for this request
  # merge them together
  # merged_context # referenced below
  requested_page
  
  result = if merged_context
    merge(merged_context, requested_page)
  else
    requested_page
  end
  
  result.call(env)
end