Class: AppHtmlLayer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apps = {}, filepath: '.', headings: false, debug: false) ⇒ AppHtmlLayer

Returns a new instance of AppHtmlLayer.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/apphtml_layer.rb', line 30

def initialize(apps={}, filepath: '.', headings: false, debug: false)
  
  @apps = if apps.is_a? Hash
    apps
  else
    {apps.class.to_s.downcase.to_sym => apps}
  end
  
  @filepath, @headings, @debug = filepath, headings, debug
  
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



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

def apps
  @apps
end

Instance Method Details

#lookup(s) ⇒ Object



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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/apphtml_layer.rb', line 42

def lookup(s)

  if s[-1] == '/' then
    
    fp = File.join(@filepath, 'index.html')      
    
    if File.exists?(fp) then
      
      return File.read(fp)
      
    else
      
      app = path(s)
      
      if app then
        return default_index(app) 
      else
        return ['path not found', 'text/plain', '404']
      end
      
    end

  end
 
  return [s.to_s, 'text/plain'] if s =~ /^\/\w+\.\w+/

  a2 = s.split('/')
  basepath = a2[0..-2].join('/')
  uri = URI(a2[-1])

  name = a2[-1][-1] == '?' ? a2[-1] : uri.path
  puts 'name: ' + name.inspect if @debug

  if uri.query then

    h = URI.decode_www_form(uri.query).inject({}) do |r,x|
      r.merge!(x[0].to_sym => x[1])
    end

    puts ('h: ' + h.inspect).debug if @debug
    args = h[:arg] ? [h[:arg]] : h
    puts 'args: ' + args.inspect if @debug
    
  end  

  puts 's: ' + s.inspect if @debug
  app = path(basepath)
  puts 'app: ' + app.inspect if @debug
  
  if app and app.respond_to? name.to_sym then

    begin
      
      method = app.method(name.to_sym)
      
      if method.arity > 0 and  args.length <= 0 then
        
        r = "
        <form action='#{name}'>
          <input type='text' name='arg'/>
          <input type='submit'/>
        </form>"
      else
        
        puts ('args: ' + args.inspect).debug if @debug
        
        r = case args
        when Array
          method.call(*args) 
        when Hash
          args.empty? ? method.call : method.call(args)
        else
          method.call
        end
        
      end
      
      puts ('name' + name.inspect) if @debug
      
      fp = File.join(@filepath, File.basename(name) + '.html')
      puts 'fp: ' + fp.inspect if @debug
      
      content = if File.exists?(fp) then        
        render_html(fp, r)
      else

        if @headings then
          markdown = "
# #{name.capitalize}          

<div>
#{r}
</div>
"

          Kramdown::Document.new(markdown).to_html
        else
          
          r
        end


      end

    rescue
      content = ($!).inspect
    end
    
    puts ('content: ' + content.inspect).debug if @debug

    case content.class.to_s
    when "String"
      media = content.lstrip[0] == '<' ? 'html' : 'plain'
      [content, 'text/' + media]
    when "Hash"
      [content.to_json,'application/json']
    else
      [content.to_s, 'text/plain']
    end
  end

end