Module: Presto

Defined in:
lib/presto.rb

Defined Under Namespace

Modules: Api Classes: App

Class Method Summary (collapse)

Class Method Details

+ (Object) included(ctrl)

extending controllers that included Presto



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
# File 'lib/presto.rb', line 55

def included ctrl

  class << ctrl

    # set and/or get controller API
    # @return instance of ::PrestoCore::Controller
    def ctrl
      @__presto_api_ctrl_class__ ||= ::PrestoCore::Controller.new(self)
    end

    # set and/or get HTTP API
    # @return [Object] instance of ::PrestoHTTP::ClassAPI
    def http
      @__presto_api_http_class__ ||= ::PrestoHTTP::ClassAPI.new(self)
    end

    # set and/or get View API
    # @return [Object] instance of ::PrestoView::ClassAPI
    def view
      @__presto_api_view_class__ ||= ::PrestoView::ClassAPI.new(self)
    end

    # act as Rack middleware
    def call env
      ::Presto.mount(self).to_app.call env
    end
  end

  ctrl.class_exec do

    # the actual constructor for Presto controllers.
    # Presto initialize an response object on each request.
    # then, the response instance, using this constructor
    # to initialize the controller containing the requested action.
    #
    # @example
    #    # if you need own constructor for some controller,
    #    # please define a callback to be executed on controller initialization,
    #    # by using `ctrl.on_init` syntax:
    #
    #    class App
    #      include Presto
    #      http.map
    #
    #      ctrl.on_init do
    #        # some logic
    #      end
    #    end
    #
    #    # you can also override initialize method and call super on it,
    #    # however it is recommended to use callbacks instead.
    #
    # @param env
    # @param [Symbol] action
    def initialize action, env

      @__presto_api__http_instance__ = ::PrestoHTTP::InstanceAPI.new(self, action, env)
      @__presto_api__http_instance__.initialize_auth
      @__presto_api__view_instance__ = ::PrestoView::InstanceAPI.new(self, action)

      self.instance_exec(&ctrl.on_init) if ctrl.on_init
    end

    # HTTP API reader/writer.
    # writer needed for sandbox functionality.
    # @return [Object] instance of {PrestoHTTP::InstanceAPI}
    def http api = nil
      @__presto_api__http_instance__ = api if api
      @__presto_api__http_instance__
    end

    # reader for View API
    # @return [Object] instance of {PrestoView::InstanceAPI}
    def view
      @__presto_api__view_instance__
    end

    # reader for Controller API
    # @return [Object] reference to #Controller API, stored at class level
    def ctrl
      self.class.ctrl
    end
  end
end

+ (Object) mount(namespace, root = nil, &proc)



46
47
48
# File 'lib/presto.rb', line 46

def mount namespace, root = nil, &proc
  new.mount namespace, root, &proc
end

+ (Object) new(&proc)



42
43
44
# File 'lib/presto.rb', line 42

def new &proc
  ::PrestoCore::App.new &proc
end

+ (Object) run(opts = {})



50
51
52
# File 'lib/presto.rb', line 50

def run opts = {}
  new.run opts
end

+ (Object) setup



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/presto.rb', line 140

def setup
  @setup ||= Class.new do

    def http
      @http ||= Class.new do

        def path_rules rules = nil
          @path_rules = rules.freeze if rules && configurable?
          @path_rules ||= {
              '____' => '.',
              '___' => '-',
              '__' => '/',
          }.freeze
        end

        def content_type content_type = nil
          @content_type = content_type if content_type && configurable?
          @content_type || ::PrestoHTTP::CONTENT_TYPE__HTML
        end

        def encoding *args
          @encoding = args.first if args.size > 0 && configurable?
          @encoding
        end

        def session
          @session ||= Class.new do

            def initialize configurable = nil
              @configurable = configurable
            end

            # marked sessions as expired after N seconds.
            # disabled by default.
            # to enable, set `Presto.setup.http.session.ttl` to a number bigger than zero.
            #
            # @param [Integer] seconds
            def ttl seconds = nil
              @ttl = seconds.to_i.freeze if seconds && configurable?
              @ttl
            end

            # by default, expired sessions will be swept every 60 seconds.
            # use `Presto.setup.http.session.sweep_interval` to set a custom interval.
            #
            # @example
            #    Presto.setup.http.session.sweep_interval
            #
            # @param [Integer] seconds
            def sweep_interval seconds = nil
              @sweep_interval = seconds.to_i.freeze if seconds && configurable?
              @sweep_interval ||= 60.freeze
            end

            # if sweep_callback is set, expired sessions will be swept selectively.
            # block will receive back the session name
            # and the session will be swept only if block returns true.
            # so, if block returns false, session will not be removed.
            #
            # @example
            #    Presto.setup.http.session.sweep_callback do |session_name|
            #      session_name == :forever_session ? false : true
            #    end
            #
            # @param [Proc] proc
            def sweep_callback &proc
              @sweep_callback = proc if proc && configurable?
              @sweep_callback
            end

            # be default, Presto will keep sessions in a memory pool.
            # use Presto.setup.http.session.pool to set a custom pool.
            #
            # @param [Object] pool
            def pool pool = nil
              @pool = pool if pool && configurable?
              @pool ||= ::PrestoCache::Memory.new
            end

            def configurable?
              @configurable
            end
          end.new configurable?
        end

        def cookie_name name = nil
          @cookie_name = name.to_s if name && configurable?
          @cookie_name ||= 'presto.sid'.freeze
        end

        def cache_pool pool = nil
          @cache_pool = pool if pool && configurable?
          @cache_pool ||= ::PrestoCache::Memory.new
        end

        def configurable?
          PrestoCore::App.started?.nil?
        end
      end.new
    end

    def html_auth
      @html_auth ||= Class.new do

        def username_field
          ::PrestoHTTP::HTMLAuth::Fields::USERNAME_FIELD
        end

        def password_field
          ::PrestoHTTP::HTMLAuth::Fields::PASSWORD_FIELD
        end

        def form_id
          ::PrestoHTTP::HTMLAuth::Fields::FORM_ID
        end

        def submit_field
          ::PrestoHTTP::HTMLAuth::Fields::SUBMIT_FIELD
        end
      end.new
    end

    def view
      @view ||= Class.new do

        def engine engine = nil
          @engine = engine if engine && configurable?
          @engine || Tilt::ERBTemplate
        end

        def engine_args *args
          @engine_args = args if configurable?
          @engine_args
        end

        def ext ext = nil
          @ext = ext if ext && configurable?
          @ext ||= 'rhtml'.freeze
        end

        def configurable?
          PrestoCore::App.started?.nil?
        end
      end.new
    end

    def debug
      @debug ||= Class.new do
        def initialize default_status
          @status = default_status
        end

        def enabled
          @status = :enabled
        end

        def enabled?
          @status == :enabled
        end

        def limited
          @status = :limited
        end

        def limited?
          @status == :limited
        end

        def disabled
          @status = :disabled
        end

        def disabled?
          @status == :disabled
        end
      end.new :enabled
    end

    def error
      @error_messages ||= Class.new do

        def not_found route, controller, action, *path_params
          " <h3>Not Found: 404</h3><hr/>
              Route: %s<br/>
              Controller: %s<br/>
              Action: %s<br/>
              Args: %s
            " % [
              route, controller, action, path_params.join(', ')
          ].map { |s| '<code>%s</code>' % s }
        end

        def fatal_error error
          ::Presto.setup.debug.enabled? ? raise(error) :
              ::Presto.setup.debug.limited? ? error.to_s : 'Server Error Occurred'
        end
      end.new
    end
  end.new

end