rocketio 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -5
  3. data/.pryrc +2 -0
  4. data/.travis.yml +3 -0
  5. data/README.md +22 -5
  6. data/Rakefile +7 -1
  7. data/bin/console +14 -0
  8. data/bin/setup +7 -0
  9. data/lib/rocketio.rb +131 -3
  10. data/lib/rocketio/application.rb +31 -0
  11. data/lib/rocketio/controller.rb +288 -0
  12. data/lib/rocketio/controller/authentication.rb +141 -0
  13. data/lib/rocketio/controller/authorization.rb +53 -0
  14. data/lib/rocketio/controller/cookies.rb +59 -0
  15. data/lib/rocketio/controller/error_handlers.rb +89 -0
  16. data/lib/rocketio/controller/filters.rb +119 -0
  17. data/lib/rocketio/controller/flash.rb +21 -0
  18. data/lib/rocketio/controller/helpers.rb +438 -0
  19. data/lib/rocketio/controller/middleware.rb +32 -0
  20. data/lib/rocketio/controller/render.rb +148 -0
  21. data/lib/rocketio/controller/render/engine.rb +76 -0
  22. data/lib/rocketio/controller/render/layout.rb +27 -0
  23. data/lib/rocketio/controller/render/layouts.rb +85 -0
  24. data/lib/rocketio/controller/render/templates.rb +83 -0
  25. data/lib/rocketio/controller/request.rb +115 -0
  26. data/lib/rocketio/controller/response.rb +84 -0
  27. data/lib/rocketio/controller/sessions.rb +64 -0
  28. data/lib/rocketio/controller/token_auth.rb +118 -0
  29. data/lib/rocketio/controller/websocket.rb +21 -0
  30. data/lib/rocketio/error_templates/404.html +3 -0
  31. data/lib/rocketio/error_templates/409.html +7 -0
  32. data/lib/rocketio/error_templates/500.html +3 -0
  33. data/lib/rocketio/error_templates/501.html +6 -0
  34. data/lib/rocketio/error_templates/layout.html +1 -0
  35. data/lib/rocketio/exceptions.rb +4 -0
  36. data/lib/rocketio/router.rb +65 -0
  37. data/lib/rocketio/util.rb +122 -0
  38. data/lib/rocketio/version.rb +2 -2
  39. data/rocketio.gemspec +21 -17
  40. data/test/aliases_test.rb +54 -0
  41. data/test/authentication_test.rb +307 -0
  42. data/test/authorization_test.rb +91 -0
  43. data/test/cache_control_test.rb +268 -0
  44. data/test/content_type_test.rb +124 -0
  45. data/test/cookies_test.rb +49 -0
  46. data/test/error_handlers_test.rb +125 -0
  47. data/test/etag_test.rb +445 -0
  48. data/test/filters_test.rb +177 -0
  49. data/test/halt_test.rb +73 -0
  50. data/test/helpers_test.rb +171 -0
  51. data/test/middleware_test.rb +57 -0
  52. data/test/redirect_test.rb +135 -0
  53. data/test/render/engine_test.rb +71 -0
  54. data/test/render/get.erb +1 -0
  55. data/test/render/items.erb +1 -0
  56. data/test/render/layout.erb +1 -0
  57. data/test/render/layout_test.rb +104 -0
  58. data/test/render/layouts/master.erb +1 -0
  59. data/test/render/layouts_test.rb +145 -0
  60. data/test/render/master.erb +1 -0
  61. data/test/render/post.erb +1 -0
  62. data/test/render/put.erb +1 -0
  63. data/test/render/render_test.rb +101 -0
  64. data/test/render/setup.rb +14 -0
  65. data/test/render/templates/a/get.erb +1 -0
  66. data/test/render/templates/master.erb +1 -0
  67. data/test/render/templates_test.rb +146 -0
  68. data/test/request_test.rb +105 -0
  69. data/test/response_test.rb +119 -0
  70. data/test/routes_test.rb +70 -0
  71. data/test/sendfile_test.rb +209 -0
  72. data/test/sessions_test.rb +176 -0
  73. data/test/setup.rb +59 -0
  74. metadata +144 -9
  75. data/LICENSE.txt +0 -22
@@ -0,0 +1,32 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # storing Rack middleware to be called when a request handled by controller
5
+ #
6
+ # @note middleware is inheritable
7
+ #
8
+ # @example add a middleware to be used in current controller
9
+ # and any controllers that inherits from it
10
+ #
11
+ # class Users < RocketIO::Controller
12
+ # use Rack::CommonLogger
13
+ # end
14
+ #
15
+ # @param w [Class, Proc] a valid Rack middleware that responds to `call`
16
+ # @param *a [Array] any args to be passed at middleware initialization
17
+ # @param &b [Proc] a block to be yielded at middleware initialization
18
+ #
19
+ def self.use w = nil, *a, &b
20
+ (@__middleware__ ||= []).push(proc {|app| w.new(app, *a, &b)})
21
+ define_middleware_methods
22
+ end
23
+
24
+ def self.define_middleware_methods source = self
25
+ middleware = ((source.instance_variable_get(:@__middleware__) || []) + allocate.middleware).uniq.freeze
26
+ return if middleware.empty?
27
+ define_method(:middleware) {middleware}
28
+ end
29
+
30
+ def middleware; RocketIO::EMPTY_ARRAY end
31
+ end
32
+ end
@@ -0,0 +1,148 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # if called without arguments render a template with lowercased name of current request method, e.g. get for GET, post for POST etc.
5
+ # at first it will look between defined templates.
6
+ # then it will search a file.
7
+ # it will try each extension the effective engine has registered, e.g. .erb, .rhtml for ERB.
8
+ # it will search in the folder the controller was defined in(NOT in path_to_templates, which is used for defined templates only).
9
+ # so put each controller in a separate folder to avoid templates clash.
10
+ #
11
+ # if no file found a TemplateError will be raised.
12
+ # if a block given it will use the the string returned by the block as template
13
+ # and wont search for defined nor file templates.
14
+ #
15
+ # by default ERB engine will be used (@see engine).
16
+ #
17
+ # for layout it will take one given through options or one defined at class level.
18
+ # if none given, it will render without layout.
19
+ # to use a layout path_to_layouts should be defined at class level.
20
+ #
21
+ # by default it will use current instance as scope.
22
+ # to render in a isolated scope, set it via :scope option
23
+ #
24
+ # to pass some local variables use :locals option
25
+ #
26
+ # @example render ./get.erb without layout
27
+ # class Pages < RocketIO::Controller
28
+ #
29
+ # def get
30
+ # render
31
+ # end
32
+ # end
33
+ #
34
+ # @example render ./get.erb with :master layout
35
+ # class Pages < RocketIO::Controller
36
+ # layout :master
37
+ #
38
+ # def get
39
+ # render
40
+ # end
41
+ # end
42
+ #
43
+ # @example render ./get.erb with explicit :master layout
44
+ # class Pages < RocketIO::Controller
45
+ #
46
+ # def get
47
+ # render(layout: :master)
48
+ # end
49
+ # end
50
+ #
51
+ # @example render within isolated scope
52
+ # class Pages < RocketIO::Controller
53
+ #
54
+ # def get
55
+ # render(scope: Object.new)
56
+ # end
57
+ # end
58
+ #
59
+ # @example render with custom locals
60
+ # class Pages < RocketIO::Controller
61
+ #
62
+ # def get
63
+ # render(locals: {x: 'y'})
64
+ # end
65
+ # end
66
+ #
67
+ def render template = nil, opts = {}
68
+ opts, template = template, nil if template.is_a?(::Hash)
69
+ engine, engine_opts = resolve_engine(opts)
70
+ template = block_given? ? yield : resolve_template(template, engine)
71
+ scope = opts.fetch(:scope, self)
72
+ locals = opts.fetch(:locals, RocketIO::EMPTY_HASH).freeze
73
+ layout = opts.fetch(:layout, self.layout)
74
+ template = compile_template(template, engine, engine_opts).render(scope, locals)
75
+ layout ? render_layout(layout, opts) {template} : template
76
+ end
77
+
78
+ # render a template that yields the given block.
79
+ # that's it, a layout is a template that yields given string.
80
+ # if no layout name given, it will use the one set at class level.
81
+ # if no layout set at class level and no layout given, it will raise a RuntimeError.
82
+ # it will firstly search for given layout between defined ones.
83
+ # if none found, it will search a file in `path_to_layouts` folder.
84
+ # it will try each extension registered with effective engine.
85
+ # if no file found it will raise a TemplateError.
86
+ #
87
+ # block is required and should return the string to be yielded.
88
+ #
89
+ def render_layout template = nil, opts = {}, &block
90
+ opts, template = template, nil if template.is_a?(::Hash)
91
+ template ||= self.layout
92
+ template || raise(RocketIO::LayoutError, 'No default layout set and no explicit layout given')
93
+ engine, engine_opts = resolve_engine(opts)
94
+ template = resolve_layout(template, engine)
95
+ scope = opts.fetch(:scope, self)
96
+ locals = opts.fetch(:locals, RocketIO::EMPTY_HASH).freeze
97
+ compile_template(template, engine, engine_opts)
98
+ .render(scope, locals, &(block || RocketIO::EMPTY_STRING_PROC))
99
+ end
100
+
101
+ private
102
+ def resolve_template template, engine
103
+ template ||= requested_method
104
+ return __send__(templates[template]) if templates[template]
105
+ read_template(find_template(dirname, template, engine))
106
+ end
107
+
108
+ def resolve_layout layout, engine
109
+ return __send__(layouts[layout]) if layouts[layout]
110
+ read_template(find_template(dirname, layout, engine))
111
+ end
112
+
113
+ def resolve_engine opts = nil
114
+ return engine_const(engine_class(opts[:engine])) if opts && opts[:engine]
115
+ [engine_const(engine[0]), engine[1]]
116
+ end
117
+
118
+ def read_template file
119
+ RocketIO::READ_TEMPLATES[[file, ::File.mtime(file).to_i]] ||= ::File.read(file)
120
+ end
121
+
122
+ def find_template path, template, engine
123
+ RocketIO::FOUND_TEMPLATES[[path, template, engine]] ||= begin
124
+ extensions = ::Tilt.default_mapping.extensions_for(engine)
125
+ file = nil
126
+ extensions.each do |ext|
127
+ try = RocketIO::TEMPLATE_PATH_FORMAT % [path, template, ext]
128
+ next unless File.exists?(try)
129
+ file = try
130
+ break
131
+ end
132
+ file || raise(RocketIO::TemplateError, '"%s" template not found in "%s".
133
+ Tried extensions: %s' % [template, path, extensions.join(', ')])
134
+ end
135
+ end
136
+
137
+ def compile_template template, engine, engine_opts = nil
138
+ RocketIO::COMPILED_TEMPLATES[[template.hash, engine, engine_opts]] ||= begin
139
+ engine.new(0, *engine_opts) { template }
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ require 'rocketio/controller/render/engine'
146
+ require 'rocketio/controller/render/layout'
147
+ require 'rocketio/controller/render/layouts'
148
+ require 'rocketio/controller/render/templates'
@@ -0,0 +1,76 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # if no engine set, templates will be rendered using ERB engine.
5
+ # any engine supported by [Tilt](github.com/rtomayko/tilt) can be used.
6
+ # to set engine use symbolized constant name, e.g. :Slim, :Haml
7
+ # engine name is Case Sensitive and there should be a Tilt::{ENGINE}Template class defined
8
+ # e.g. `engine :Slim` will look for Tilt::SlimTemplate
9
+ # and `engine RDiscount` will look for Tilt::RDiscountTemplate
10
+ #
11
+ # @note if a block given it will be executed at instance level
12
+ # and result used for engine. To have any options passed at engine initialization
13
+ # the block should return an array having engine as first element
14
+ # and options as consequent elements.
15
+ #
16
+ # @note if given block returns no engine, inherited engine will be used
17
+ #
18
+ # @example use static engine
19
+ # engine :Slim
20
+ #
21
+ # @example use static engine with options
22
+ # engine :Slim, pretty_print: true
23
+ #
24
+ # @example use dynamically set engine
25
+ # engine do
26
+ # some_condition ? :SomeEngine : AnotherEngine
27
+ # end
28
+ #
29
+ # @example use dynamically set engine with options
30
+ # engine do
31
+ # some_condition ? [:SomeEngine, :opt1, :opt2] : AnotherEngine
32
+ # end
33
+ #
34
+ # @example Search will use ERB when requested by a bot and Slim otherwise
35
+ #
36
+ # class BaseController < RocketIO
37
+ # engine :Slim
38
+ # end
39
+ #
40
+ # class Search < BaseController
41
+ # engine do
42
+ # if request.user_agent =~ /i'm a bot/
43
+ # # requested by a bot, using ERB
44
+ # :ERB
45
+ # end
46
+ # # requested by a user, returning no engine for inherited engine to be used
47
+ # end
48
+ # end
49
+ #
50
+ # @param engine engine name.
51
+ # @param *engine_options any arguments to be passed at engine initialization
52
+ # @param block to be executed at instance level
53
+ #
54
+ def self.engine engine = nil, *engine_options, &block
55
+ @__engine__ = block || [RocketIO.engine_class(engine), engine_options.freeze].freeze
56
+ define_engine_methods
57
+ end
58
+
59
+ def self.define_engine_methods source = self
60
+ return unless engine = source.instance_variable_get(:@__engine__)
61
+ if Proc === engine
62
+ selfengine = allocate.engine
63
+ define_method(:__rocketio_engine__, &engine)
64
+ define_method(:engine) {
65
+ engine, *engine_options = __rocketio_engine__
66
+ return selfengine unless engine
67
+ [RocketIO.engine_class(engine), engine_options.freeze].freeze
68
+ }
69
+ else
70
+ define_method(:engine) {engine}
71
+ end
72
+ end
73
+
74
+ def engine; RocketIO::DEFAULT_ENGINE end
75
+ end
76
+ end
@@ -0,0 +1,27 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # by default templates will be rendered without layout.
5
+ # to make them render inside a layout use `layout :layout_name` at class level.
6
+ # to use a layout it should be defined at first (@see define_layout)
7
+ #
8
+ # @note to disable layout set it to false: `layout false`
9
+ #
10
+ # @param layout name of a defined layout
11
+ #
12
+ def self.layout layout
13
+ @__layout__ = layout
14
+ define_layout_methods
15
+ end
16
+
17
+ def self.define_layout_methods source = self
18
+ return unless source.instance_variables.include?(:@__layout__)
19
+ layout = source.instance_variable_get(:@__layout__)
20
+ define_method(:layout) {layout}
21
+ end
22
+
23
+ # by default no layout used, so this method returns nil.
24
+ # controllers that uses a layout will override this method.
25
+ def layout; end
26
+ end
27
+ end
@@ -0,0 +1,85 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # if only name given it will search for a file with same name in controller's dirname.
5
+ #
6
+ # if file name differs from layout name pass it as :file option.
7
+ # file path should be relative to controller's dirname.
8
+ # also a block accepted for :file option. the block will be executed at controllers's instance level
9
+ # and should return path to layout file.
10
+ # file name should NOT include extension.
11
+ #
12
+ # if a block given NO file will be searched and returned value will be used as layout.
13
+ #
14
+ # @note files will be searched relative to controller's dirname,
15
+ # that's it, the folder controller was defined in and operates from.
16
+ #
17
+ # @note when searching for file multiple extensions will be tried,
18
+ # that's it, all extensions controller's engine actually supports.
19
+ #
20
+ # @note controllers that inherits named layouts will always search for files in own dirname.
21
+ # controllers that inherits :file layouts will search files in the original controller's dirname.
22
+ #
23
+ # @example define :master layout.
24
+ # ./master.erb file will be used
25
+ #
26
+ # define_layout :master
27
+ #
28
+ # @example define :master layout.
29
+ # ../layouts/master.erb file will be used
30
+ #
31
+ # define_layout :master, file: '../layouts/master'
32
+ #
33
+ # @example define :master layout.
34
+ # ./admin file to be used when user logged in and ./user otherwise
35
+ #
36
+ # define_layout :master, file: -> {user? ? 'admin' : 'user'}
37
+ #
38
+ # @example define :master layout using a block that returns the layout string.
39
+ # no file will be used.
40
+ #
41
+ # define_layout(:master) do
42
+ # layout = Layouts.find_by(id: params[:layout_id]) || halt(400, 'Template not found')
43
+ # layout.source
44
+ # end
45
+ #
46
+ # @param name
47
+ # @param file
48
+ # @param block
49
+ #
50
+ def self.define_layout name, file: nil, &block
51
+ file && block && raise(::ArgumentError, 'both file and block given, please use either one')
52
+ (@__layouts__ ||= {})[name.to_sym] = {block: block, root: dirname, file: file, name: name}.freeze
53
+ define_layouts_methods
54
+ end
55
+
56
+ def self.define_layouts_methods source = self
57
+ return unless source.instance_variables.include?(:@__layouts__)
58
+ layouts = source.instance_variable_get(:@__layouts__).each_with_object(allocate.layouts.dup) do |(name,setup),o|
59
+ o[name] = :"__#{name}_layout__"
60
+ if setup[:block]
61
+ # block given, do not search for file, use returned value instead
62
+ define_method(o[name], &setup[:block])
63
+ elsif setup[:file]
64
+ # file given, search the file in original controller dirname
65
+ meth_name = :"__#{name}_layout_file__"
66
+ meth_proc = setup[:file].is_a?(::Proc) ? setup[:file] : -> {setup[:file]}
67
+ define_method(meth_name, &meth_proc)
68
+ define_method o[name] do
69
+ engine, * = resolve_engine
70
+ read_template(find_template(setup[:root], __send__(meth_name), engine))
71
+ end
72
+ else
73
+ # only name given, search for a file with same name in controller's dirname
74
+ define_method o[name] do
75
+ engine, * = resolve_engine
76
+ read_template(find_template(self.dirname, setup[:name], engine))
77
+ end
78
+ end
79
+ end.freeze
80
+ define_method(:layouts) {layouts}
81
+ end
82
+
83
+ def layouts; RocketIO::EMPTY_HASH end
84
+ end
85
+ end
@@ -0,0 +1,83 @@
1
+ module RocketIO
2
+ class Controller
3
+
4
+ # if only name given it will search for a file with same name in controller's dirname
5
+ #
6
+ # if file name differs from template name pass it as :file option.
7
+ # file path should be relative to controller's dirname.
8
+ # also a block accepted for :file option. the block will be executed at controllers's instance level
9
+ # and should return path to template file.
10
+ # file name should NOT include extension.
11
+ #
12
+ # if a block given NO file will be searched and returned value will be used as template.
13
+ #
14
+ # @note files will be searched relative to controller's dirname,
15
+ # that's it, the folder controller was defined in and operates from.
16
+ #
17
+ # @note when searching for file multiple extensions will be tried,
18
+ # that's it, all extensions controller's engine actually supports.
19
+ #
20
+ # @note controllers that inherits named templates will always search for files in own dirname.
21
+ # controllers that inherits :file templates will search files in the original controller's dirname.
22
+ #
23
+ # @example define :items template.
24
+ # ./items.erb file will be used
25
+ # define_template :items
26
+ #
27
+ # @example define :items template.
28
+ # ../shared_templates/items.erb file will be used
29
+ # define_template :items, file: '../shared_templates/items'
30
+ #
31
+ # @example define :items template.
32
+ # ./admin-items.erb file to be used when user logged in and ./items.erb otherwise
33
+ #
34
+ # define_template :items, file: -> {user? ? 'admin-items' : 'items'}
35
+ #
36
+ # @example define :items template using a block that returns the template string.
37
+ # no file will be used.
38
+ #
39
+ # define_template(:items) do
40
+ # template = Templates.find_by(id: params[:template_id]) || halt(400, 'Template not found')
41
+ # template.source
42
+ # end
43
+ #
44
+ # @param name
45
+ # @param file
46
+ # @param block
47
+ #
48
+ def self.define_template name, file: nil, &block
49
+ file && block && raise(ArgumentError, 'both file and block given, please use either one')
50
+ (@__templates__ ||= {})[name.to_sym] = {block: block, root: dirname, file: file, name: name}.freeze
51
+ define_templates_methods
52
+ end
53
+
54
+ def self.define_templates_methods source = self
55
+ return unless source.instance_variables.include?(:@__templates__)
56
+ templates = source.instance_variable_get(:@__templates__).each_with_object(allocate.templates.dup) do |(name,setup),o|
57
+ o[name] = :"__#{name}_template__"
58
+ if setup[:block]
59
+ # block given, do not search for file, use returned value instead
60
+ define_method(o[name], &setup[:block])
61
+ elsif setup[:file]
62
+ # file given, search the file in original controller dirname
63
+ meth_name = :"__#{name}_template_file__"
64
+ meth_proc = setup[:file].is_a?(Proc) ? setup[:file] : -> {setup[:file]}
65
+ define_method(meth_name, &meth_proc)
66
+ define_method o[name] do
67
+ engine, * = resolve_engine
68
+ read_template(find_template(setup[:root], __send__(meth_name), engine))
69
+ end
70
+ else
71
+ # only name given, search for a file with same name in controller's dirname
72
+ define_method o[name] do
73
+ engine, * = resolve_engine
74
+ read_template(find_template(self.dirname, setup[:name], engine))
75
+ end
76
+ end
77
+ end.freeze
78
+ define_method(:templates) {templates}
79
+ end
80
+
81
+ def templates; RocketIO::EMPTY_HASH end
82
+ end
83
+ end