pakyow-routing 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b2a9aed8421bd98e8fabc612f53b18d697a83a806bb1610d46091eba749d40f3
4
+ data.tar.gz: 5fe8c0e699df3ef047fd27452a3f53112d445d844ffbfddafadf6011c05d5f17
5
+ SHA512:
6
+ metadata.gz: e32e28a4d7cbe530e914852b491e7095ecbc7a9a03f3120be638eac9070292f6aff9a58a25304659831dbd1475c8753321b9fe281215369546dd72fa0b04accd
7
+ data.tar.gz: 66461e2cb4f2aeb6a39c608d2e61176281793f95bbbeebcfbd65e5b5d6e25d181816f7ee58b60a3ecb744274d9da780c53e0640bf92411dfaf4464de41803719
data/CHANGELOG.md ADDED
@@ -0,0 +1,137 @@
1
+ # 1.0.0
2
+
3
+ * IMPROVED helpers by removing `Helpers::App` and `Helpers::Context`. Instead,
4
+ `Helpers` is included only into `CallContext`. Extensions to `App` is now
5
+ considered an edge-case and should be done through other normal means.
6
+ * IMPROVED hooks by removing `Helpers::Hooks` in favor of `Support::Hookable`.
7
+ * ADDED verbose log level.
8
+ * CHANGED default production log level to `info`.
9
+
10
+ # 0.11.3
11
+
12
+ * Adds the ability to `send` StringIO objects
13
+
14
+ # 0.11.1
15
+
16
+ * Includes `Pakyow::Helpers` into `Pakyow::App`
17
+
18
+ # 0.11.0
19
+
20
+ * Adds a `configure` hook for evaluating code before/after configuring app
21
+ * The `Pakyow::App.define` method now returns the defined app
22
+ * Explicitly requires `pakyow-support` so core works on its own
23
+ * Adds a convenience method for defining a restful resource
24
+ * Adds a `reload` hook for evaluating code before/after reloading app
25
+ * Better support for sending any IO object or String data
26
+ * Defaults to `global` when defining app configuration
27
+ * Adds a new `reloader` config namespace
28
+ * Redirects requests with `//` to single-slash path
29
+ * Adds the ability to easily disable middleware
30
+ * Bundles and configures middleware for session handling
31
+ * Automatically removes the trailing `/` in request path
32
+ * Now loads the global config options before env-specific options
33
+ * Adds the source directory to the load path
34
+ * Sets default Content-Type to utf-8
35
+ * Moves everything into the Pakyow namespace
36
+ * Changes to allow error hooks to halt execution
37
+ * Adds a convenience method for checking the existence of a route
38
+ * Protects against internal failures when calling a route with an unknown method
39
+ * Fixes route hook execution order, prioritizing hooks defined in outer scopes
40
+
41
+ # 0.10.2 / 2015-11-15
42
+
43
+ * Fixes issues presenting error views from gem
44
+ * Fixes a bug causing routes to inherit hooks from previously defined routes
45
+ * No longer resets the working context when redirecting or sending a file
46
+ * Uses Rack's delete_cookie method rather than unset_cookie
47
+
48
+ # 0.10.0 / 2015-10-19
49
+
50
+ * Adds nested route groups / namespaces inherit hooks
51
+ * Static files are now served by default
52
+ * Consistently handles externally defined config options
53
+ * Adds post-processing step to route template expansions
54
+ * Prevents the logger from breaking when no log to write to
55
+ * Prevents resouces config from being reset on access
56
+ * Don't add query params when route building
57
+ * Fixes bug when defining nested restful routes
58
+ * Support passing group + route name to `redir` and `reroute`
59
+ * Updated status code names
60
+ * Sets mime type of response when setting type
61
+ * Exposes content type on response object
62
+ * Adds support for `pakyow.data` in Rack env
63
+ * Runs global config *after* local config
64
+ * Makes JSON body available in request params
65
+ * Fixes a bug in app reloading
66
+ * Ported all tests to rspec
67
+ * Adds the ability to halt execution in a 500 handler
68
+ * Fixes namespace collisions
69
+ * Use app's template for displaying Pakyow error views
70
+ * Provides default values for helpers when no context available
71
+ * Use `Bundler.require` to load dependencies in global config block
72
+ * Respects before hook order
73
+ * No longer overrides user-provided type when sending data/files
74
+
75
+ # 0.9.1 / 2014-12-06
76
+
77
+ * No changes -- bumped version to be consistent
78
+
79
+ # 0.9.0 / 2014-11-09
80
+
81
+ * Renames restful "remove" action to "delete"
82
+ * Improves app generator bundle install by showing progress
83
+ * Complete refactor of config handling with a shiny DSL
84
+ * Includes pakyow-rake as a dependency, and updates the generated Rakefile
85
+ * Removes support for Ruby versions < 2.0.0
86
+
87
+ # 0.8.0 / 2014-03-02
88
+
89
+ * Major rewrite, including changes to app definition and routing
90
+
91
+ # 0.7.2 / 2012-02-29
92
+
93
+ * Application server shuts down gracefully
94
+ * Fix issue requesting route with format
95
+ * Fix issue surrounding ignore_routes -- now matches request path in all cases
96
+
97
+ # 0.7.1 / 2012-01-08
98
+
99
+ * Changed loader to only load ruby files
100
+ * Moved session from app to request
101
+ * Replaced autoload with require
102
+ * Fixed generated rackup to use builder
103
+ * Fixed generated rakefile so it runs in a specific environment
104
+ * Fixed issue running with ignore_routes turned on
105
+
106
+ # 0.7.0 / 2011-11-19
107
+
108
+ * Added middleware for logging, static, and reloading
109
+ * Added invoke_route! and invoke_handler! methods
110
+ * Added before, after, and around hooks to routes
111
+ * Added pakyow console
112
+ * Changed methods that modify request/response life cycle to bang methods
113
+ * Fixed regex route error (was removing route vars)
114
+ * App file is no longer loaded twice upon initialization
115
+ * Fix cookie creation when cookie is a non-nil value but not a String
116
+
117
+ # 0.6.3 / 2011-09-13
118
+
119
+ * Fixes several load path issues
120
+ * Fixes gemspecs so gem can be built/used from anywhere
121
+ * Fixes inconsistency with with request.params having string and symbol keys
122
+ * Fixes loading of middleware when staging application (simplifies rackup)
123
+
124
+ # 0.6.2 / 2011-08-20
125
+
126
+ * Fixes issue running pakyow server on Windows
127
+ * Fixes several issues related to error handlers
128
+ * Fixes an issue when using alphanumeric ids in restful routes
129
+ * JRuby Support
130
+
131
+ # 0.6.1 / 2011-08-20
132
+
133
+ * Fixes gemspec problem
134
+
135
+ # 0.6.0 / 2011-08-20
136
+
137
+ * Initial gem release of 0.6.0 codebase
data/LICENSE ADDED
@@ -0,0 +1,4 @@
1
+ Copyright (c) Metabahn, LLC
2
+
3
+ Pakyow Routing is an open-source project licensed under the terms of the LGPLv3 license.
4
+ See <https://choosealicense.com/licenses/lgpl-3.0/> for license text.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # pakyow-routing
2
+
3
+ Routing for Pakyow.
4
+
5
+ # Download
6
+
7
+ The latest version of Pakyow Routing can be installed with RubyGems:
8
+
9
+ ```
10
+ gem install pakyow-routing
11
+ ```
12
+
13
+ Source code can be downloaded as part of the Pakyow project on Github:
14
+
15
+ - https://github.com/pakyow/pakyow/tree/master/pakyow-routing
16
+
17
+ # License
18
+
19
+ Pakyow Routing is free and open-source under the [LGPLv3 license](https://choosealicense.com/licenses/lgpl-3.0/).
20
+
21
+ # Support
22
+
23
+ Documentation is available here:
24
+
25
+ - http://pakyow.org/docs/routing
26
+
27
+ Found a bug? Tell us about it here:
28
+
29
+ - https://github.com/pakyow/pakyow/issues
30
+
31
+ We'd love to have you in the community:
32
+
33
+ - http://pakyow.org/get-involved
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/support/extension"
4
+
5
+ module Pakyow
6
+ module Behavior
7
+ module Definition
8
+ extend Support::Extension
9
+
10
+ apply_extension do
11
+ include Methods
12
+ extend Methods
13
+ end
14
+
15
+ module Methods
16
+ # Defines a RESTful resource.
17
+ #
18
+ # @see Routing::Extension::Resource
19
+ #
20
+ def resource(name, path, *args, param: Pakyow::Routing::Extension::Resource::DEFAULT_PARAM, &block)
21
+ controller name, path, *args do
22
+ expand_within(:resource, param: param, &block)
23
+ end
24
+ end
25
+
26
+ # Registers an error handler automatically available in all Controller instances.
27
+ #
28
+ # @see Routing::Behavior::ErrorHandling#handle
29
+ def handle(name_exception_or_code, as: nil, &block)
30
+ const_get(:Controller).handle(name_exception_or_code, as: as, &block)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pakyow
4
+ module Routing
5
+ module Actions
6
+ class RespondMissing
7
+ def call(connection)
8
+ connection.app.controller_for_connection(connection).trigger(404)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/support/class_state"
4
+ require "pakyow/support/deep_dup"
5
+ require "pakyow/support/extension"
6
+
7
+ require "pakyow/connection/statuses"
8
+
9
+ module Pakyow
10
+ module Routing
11
+ module Behavior
12
+ module ErrorHandling
13
+ extend Support::Extension
14
+
15
+ apply_extension do
16
+ class_state :handlers, default: {}, inheritable: true
17
+ class_state :exceptions, default: {}, inheritable: true
18
+
19
+ include API
20
+ extend API
21
+ end
22
+
23
+ prepend_methods do
24
+ using Support::DeepDup
25
+
26
+ def initialize(*)
27
+ @handlers = self.class.handlers.deep_dup
28
+ @exceptions = self.class.exceptions.deep_dup
29
+
30
+ super
31
+ end
32
+ end
33
+
34
+ # Calls the handler for a particular http status code.
35
+ #
36
+ def trigger(name_or_code)
37
+ code = Connection::Statuses.code(name_or_code)
38
+ connection.status = code
39
+ trigger_for_code(code)
40
+ end
41
+
42
+ def handle_error(error)
43
+ connection.error = error
44
+ connection.status = 500
45
+
46
+ catch :halt do
47
+ call_handlers_with_args(
48
+ exceptions_for_class(error.class) || handlers_for_code(500),
49
+ error
50
+ )
51
+ end
52
+
53
+ halt
54
+ end
55
+
56
+ private
57
+
58
+ def call_handlers_with_args(handlers, *args)
59
+ handlers.to_a.reverse.each do |status_code, handler|
60
+ catch :reject do
61
+ connection.status = status_code
62
+
63
+ if handler
64
+ instance_exec(*args, &handler)
65
+ end
66
+
67
+ halt
68
+ end
69
+ end
70
+ end
71
+
72
+ def trigger_for_code(code)
73
+ call_handlers_with_args(handlers_for_code(code)); halt
74
+ end
75
+
76
+ def handlers_for_code(code)
77
+ @handlers[code]
78
+ end
79
+
80
+ def exceptions_for_class(klass)
81
+ @exceptions[klass]
82
+ end
83
+
84
+ module API
85
+ # Registers an error handler used within a controller or request lifecycle.
86
+ #
87
+ # @example Defining for a controller:
88
+ # Pakyow::App.controller do
89
+ # handle 500 do
90
+ # # build and send a response for `request.error`
91
+ # end
92
+ #
93
+ # default do
94
+ # # do something that might cause an error
95
+ # end
96
+ # end
97
+ #
98
+ # @example Defining for a request lifecycle:
99
+ # Pakyow::App.controller do
100
+ # default do
101
+ # handle 500 do
102
+ # # build and send a response for `request.error`
103
+ # end
104
+ #
105
+ # # do something that might cause an error
106
+ # end
107
+ # end
108
+ #
109
+ # @example Handling by status code:
110
+ # handle 500 do
111
+ # # build and send a response
112
+ # end
113
+ #
114
+ # default do
115
+ # trigger 500
116
+ # end
117
+ #
118
+ # @example Handling by status name:
119
+ # handle :forbidden do
120
+ # # build and send a response
121
+ # end
122
+ #
123
+ # default do
124
+ # trigger 403 # or, `trigger :forbidden`
125
+ # end
126
+ #
127
+ # @example Handling an exception:
128
+ # handle Sequel::NoMatchingRow, as: 404 do
129
+ # # build and send a response
130
+ # end
131
+ #
132
+ # default do
133
+ # raise Sequel::NoMatchingRow
134
+ # end
135
+ #
136
+ def handle(name_exception_or_code, as: nil, &block)
137
+ if name_exception_or_code.is_a?(Class) && name_exception_or_code.ancestors.include?(Exception)
138
+ raise ArgumentError, "status code is required" if as.nil?
139
+ (@exceptions[name_exception_or_code] ||= []) << [Connection::Statuses.code(as), block]
140
+ else
141
+ status_code = Connection::Statuses.code(name_exception_or_code)
142
+ (@handlers[status_code] ||= []) << [as || status_code, block]
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pakyow/support/extension"
4
+
5
+ require "pakyow/errors"
6
+
7
+ require "pakyow/behavior/verification"
8
+
9
+ module Pakyow
10
+ module Routing
11
+ module Behavior
12
+ module ParamVerification
13
+ extend Support::Extension
14
+
15
+ apply_extension do
16
+ class_state :__allowed_params, default: [], inheritable: true
17
+
18
+ include Pakyow::Behavior::Verification
19
+
20
+ # Define the data we wish to verify.
21
+ #
22
+ verifies :params
23
+
24
+ # Handle all invalid data errors as a bad request, by default.
25
+ #
26
+ handle InvalidData, as: :bad_request
27
+ end
28
+
29
+ class_methods do
30
+ # Perform input verification before one or more routes, identified by name.
31
+ #
32
+ # @see Pakyow::Verifier
33
+ #
34
+ # @api public
35
+ def verify(*names, &block)
36
+ verification_method_name = :"verify_#{names.join("_")}"
37
+
38
+ define_method verification_method_name do
39
+ local_allowed_params = self.class.__allowed_params
40
+
41
+ verify do
42
+ local_allowed_params.each do |allowed_param|
43
+ optional allowed_param
44
+ end
45
+
46
+ instance_exec(&block)
47
+ end
48
+ end
49
+
50
+ action verification_method_name, only: names
51
+ end
52
+
53
+ # Set one or more params as optional in all routes.
54
+ #
55
+ def allow_params(*names)
56
+ @__allowed_params.concat(names).uniq!
57
+ end
58
+ end
59
+
60
+ prepend_methods do
61
+ def verify(values = nil, &block)
62
+ local_allowed_params = self.class.__allowed_params
63
+
64
+ super do
65
+ local_allowed_params.each do |allowed_param|
66
+ optional allowed_param
67
+ end
68
+
69
+ instance_exec(&block)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end