plezi 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +48 -1
- data/Rakefile +7 -1
- data/bin/plezi +7 -0
- data/lib/plezi.rb +2 -0
- data/lib/plezi/eventmachine/ssl_connection.rb +9 -3
- data/lib/plezi/handlers/controller_magic.rb +43 -1
- data/lib/plezi/handlers/magic_helpers.rb +9 -2
- data/lib/plezi/handlers/route.rb +108 -51
- data/lib/plezi/oauth.rb +3 -2
- data/lib/plezi/oauth/auth_controller.rb +48 -27
- data/lib/plezi/server/http_protocol.rb +11 -0
- data/lib/plezi/server/websocket_client.rb +172 -0
- data/lib/plezi/version.rb +1 -1
- data/resources/Gemfile +1 -0
- data/resources/database.yml +33 -0
- data/resources/db_ac_config.rb +34 -33
- data/resources/environment.rb +2 -2
- data/resources/oauth_config.rb +2 -2
- data/resources/rakefile +3 -3
- data/test/plezi_tests.rb +104 -27
- metadata +4 -3
- data/TODO.md +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f6b05d42e0915681853564afa40dde6362c11a5
|
4
|
+
data.tar.gz: 4acd945c253d4eb550f4c3d102494703c5aac507
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8e251f2f1e9b358612406cc13df2d59a3f601703000999a3424072c712ff2e6ebd0a3a2555dadaf5d8a11b26cc06ca75fea117b046b1b11967be99d27cd3e75
|
7
|
+
data.tar.gz: 5637b576b93d7672f83b3f5e7943e993aeeee2f6fdceb3125fe4e28df67c2830ca12959158c6043e0a5be5a608f0c26a341285fcaa7d53914dac5746acaf0095
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,16 @@
|
|
2
2
|
|
3
3
|
***
|
4
4
|
|
5
|
+
Change log v.0.8.5
|
6
|
+
|
7
|
+
**feature**: Plezi now includes a very simple Websocket Client (no support for cookies). It's used for testing the integrity of the Plezi Framework and could be used to test the Plezi apps.
|
8
|
+
|
9
|
+
**feature**: the Controller can now create easy urls for it's own paths, using the #url_for method. Works wonderfuly for simple routes (routes such as: '/path/to/restful/controller/(:id)/(:other)/(:simple)/(:options)').
|
10
|
+
|
11
|
+
**update**: better ActiveRecord support now adds AC rake tasks.
|
12
|
+
|
13
|
+
***
|
14
|
+
|
5
15
|
Change log v.0.8.4
|
6
16
|
|
7
17
|
**core routing changes**: moved the Controller's routing cache out of the global cahce store. This might provide a very slight performance increase.
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
> People who are serious about their frameworks, should write their own servers...
|
6
6
|
|
7
|
-
|
7
|
+
Find more info on [Plezi's framework, web server and websocket documentation](http://www.rubydoc.info/gems/plezi/)
|
8
8
|
|
9
9
|
## About the Plezi framework \ server
|
10
10
|
|
@@ -319,6 +319,53 @@ Plezi is ment to be very flexible. please take a look at the Plezi Module for se
|
|
319
319
|
|
320
320
|
Feel free to fork or contribute. right now I am one person, but together we can make something exciting that will help us enjoy Ruby in this brave new world and (hopefully) set an example that will induce progress in the popular mainstream frameworks such as Rails and Sinatra.
|
321
321
|
|
322
|
+
## OAuth2 and other Helpers
|
323
|
+
|
324
|
+
Plezi has a few helpers that help with common tasks.
|
325
|
+
|
326
|
+
For instance, Plezi has a built in controller that allows you to add social authentication using Google, Facebook
|
327
|
+
and and other OAuth2 authentication service. For example:
|
328
|
+
|
329
|
+
require 'plezi'
|
330
|
+
|
331
|
+
class Controller
|
332
|
+
def index
|
333
|
+
flash[:login] ? "You are logged in as #{flash[:login]}" : "You aren't logged in. Please visit one of the following:\n\n* #{request.base_url}#{Plezi::OAuth2Ctrl.url_for :google}\n\n* #{request.base_url}#{Plezi::OAuth2Ctrl.url_for :facebook}"
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
# set up the common social authentication variables for automatic Plezi::OAuth2Ctrl service recognition.
|
338
|
+
ENV["FB_APP_ID"] ||= "facebook_app_id / facebook_client_id"
|
339
|
+
ENV["FB_APP_SECRET"] ||= "facebook_app_secret / facebook_client_secret"
|
340
|
+
ENV['GOOGLE_APP_ID'] = "google_app_id / google_client_id"
|
341
|
+
ENV['GOOGLE_APP_SECRET'] = "google_app_secret / google_client_secret"
|
342
|
+
|
343
|
+
require 'plezi/oauth'
|
344
|
+
|
345
|
+
# manually setup any OAuth2 service (we'll re-setup facebook as an example):
|
346
|
+
Plezi::OAuth2Ctrl.register_service(:facebook, app_id: ENV['FB_APP_ID'],
|
347
|
+
app_secret: ENV['FB_APP_SECRET'],
|
348
|
+
auth_url: "https://www.facebook.com/dialog/oauth",
|
349
|
+
token_url: "https://graph.facebook.com/v2.3/oauth/access_token",
|
350
|
+
profile_url: "https://graph.facebook.com/v2.3/me",
|
351
|
+
scope: "public_profile,email") if ENV['FB_APP_ID'] && ENV['FB_APP_SECRET']
|
352
|
+
|
353
|
+
|
354
|
+
listen
|
355
|
+
|
356
|
+
create_auth_shared_route do |service_name, token, remote_user_id, remote_user_email, remote_response|
|
357
|
+
# we will create a temporary cookie storing a login message. replace this code with your app's logic
|
358
|
+
flash[:login] = "#{remote_response['name']} (#{remote_user_email}) from #{service_name}"
|
359
|
+
end
|
360
|
+
|
361
|
+
route "/" , Controller
|
362
|
+
|
363
|
+
exit
|
364
|
+
|
365
|
+
Plezi has a some more goodies under the hood.
|
366
|
+
|
367
|
+
Whether such goodies are part of the Plezi-App Template (such as rake tasks for ActiveRecord without Rails) or part of the Plezi Framework core (such as descried in the Plezi::ControllerMagic documentation: #flash, #url_for, #render, #send_data, etc'), these goodies are fun to work with and make completion of common tasks a breeze.
|
368
|
+
|
322
369
|
## Contributing
|
323
370
|
|
324
371
|
1. Fork it ( https://github.com/boazsegev/plezi/fork )
|
data/Rakefile
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
|
3
3
|
# create a default task
|
4
|
-
desc "
|
4
|
+
desc "Tests most of the basic features for the Plezi framework."
|
5
5
|
task :test do
|
6
6
|
load "./test/plezi_tests.rb"
|
7
7
|
end
|
8
|
+
desc "Leaves the testing server open for 1 minute after most of the tests are complete, delaying shutdown testing."
|
9
|
+
task :slowtest do
|
10
|
+
PLEZI_TEST_TIME = 60
|
11
|
+
load "./test/plezi_tests.rb"
|
12
|
+
end
|
13
|
+
|
data/bin/plezi
CHANGED
@@ -5,6 +5,7 @@ $0="Plezi Builder"
|
|
5
5
|
require 'irb'
|
6
6
|
require 'securerandom'
|
7
7
|
|
8
|
+
|
8
9
|
##########
|
9
10
|
# this is the template writer
|
10
11
|
#
|
@@ -72,6 +73,8 @@ class AppTemplate
|
|
72
73
|
#set up database stub folders
|
73
74
|
app_tree["db"] ||= {}
|
74
75
|
app_tree["db"]["migrate"] ||= {}
|
76
|
+
app_tree["db"]["fixtures"] ||= {}
|
77
|
+
app_tree["db"]["config.yml"] ||= IO.read(::File.expand_path(File.join("..", "..", "resources" ,"database.yml"), __FILE__))
|
75
78
|
|
76
79
|
#set up the extras folder, to be filled with future goodies.
|
77
80
|
app_tree["extras"] ||= {}
|
@@ -205,6 +208,10 @@ end
|
|
205
208
|
######################################################################
|
206
209
|
######################################################################
|
207
210
|
|
211
|
+
# update with http://ruby-doc.org/stdlib-2.2.0/libdoc/optparse/rdoc/OptionParser.html
|
212
|
+
|
213
|
+
# require 'optparser'
|
214
|
+
|
208
215
|
if ARGV[0] == 'new' || ARGV[0] == 'n' || ARGV[0] == "force"
|
209
216
|
#########
|
210
217
|
## set up building environment
|
data/lib/plezi.rb
CHANGED
@@ -56,6 +56,8 @@ require 'plezi/server/http_response.rb'
|
|
56
56
|
require 'plezi/server/websocket.rb'
|
57
57
|
require 'plezi/server/ws_response.rb'
|
58
58
|
|
59
|
+
### websocket client
|
60
|
+
require 'plezi/server/websocket_client.rb'
|
59
61
|
### Handlers / Framework
|
60
62
|
|
61
63
|
require "plezi/handlers/http_echo"
|
@@ -12,7 +12,13 @@ module Plezi
|
|
12
12
|
attr_reader :ssl_socket
|
13
13
|
|
14
14
|
def initialize socket, params
|
15
|
-
if params[:
|
15
|
+
if params[:ssl_client]
|
16
|
+
context = OpenSSL::SSL::SSLContext.new
|
17
|
+
context.set_params verify_mode: OpenSSL::SSL::VERIFY_NONE # OpenSSL::SSL::VERIFY_PEER #OpenSSL::SSL::VERIFY_NONE
|
18
|
+
@ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, context)
|
19
|
+
@ssl_socket.sync_close = true
|
20
|
+
@ssl_socket.connect
|
21
|
+
elsif params[:ssl] || params[:ssl_key] || params[:ssl_cert]
|
16
22
|
params[:ssl_cert], params[:ssl_key] = SSLConnection.self_cert unless params[:ssl_key] && params[:ssl_cert]
|
17
23
|
context = OpenSSL::SSL::SSLContext.new
|
18
24
|
context.set_params verify_mode: OpenSSL::SSL::VERIFY_NONE # OpenSSL::SSL::VERIFY_PEER #OpenSSL::SSL::VERIFY_NONE
|
@@ -24,7 +30,7 @@ module Plezi
|
|
24
30
|
@ssl_socket.sync_close = true
|
25
31
|
@ssl_socket.accept
|
26
32
|
end
|
27
|
-
raise "Not an SSL connection or SSL Socket creation failed" unless ssl_socket
|
33
|
+
raise "Not an SSL connection or SSL Socket creation failed" unless @ssl_socket
|
28
34
|
super
|
29
35
|
end
|
30
36
|
|
@@ -68,7 +74,7 @@ module Plezi
|
|
68
74
|
def read size = 1048576
|
69
75
|
data = ''
|
70
76
|
begin
|
71
|
-
|
77
|
+
(data << @ssl_socket.read_nonblock(size).to_s) until data.bytesize >= size
|
72
78
|
rescue => e
|
73
79
|
|
74
80
|
end
|
@@ -93,6 +93,28 @@ module Plezi
|
|
93
93
|
true
|
94
94
|
end
|
95
95
|
|
96
|
+
# Returns the RELATIVE url for methods in THIS controller (i.e.: "/path_to_controller/restful/params?non=restful¶ms=foo")
|
97
|
+
#
|
98
|
+
# accepts one parameter:
|
99
|
+
# dest:: a destination object, either a Hash, a Symbol, a Numerical or a String.
|
100
|
+
#
|
101
|
+
# If :dest is a Numerical, a Symbol or a String, it should signify the id of an object or the name of the method this controller should respond to.
|
102
|
+
#
|
103
|
+
# If :dest is a Hash, it should contain all the relevant parameters the url should set (i.e. `url_for id: :new, name: "Jhon Doe"`)
|
104
|
+
#
|
105
|
+
# If :dest is false (or nil), the String returned will be the url to the index.
|
106
|
+
#
|
107
|
+
# * If you use the same controller in different routes, the first route will dictate the returned url's structure (cause by route priority).
|
108
|
+
#
|
109
|
+
# * Not all controllers support this method. Regexp controller paths and multi-path options will throw an exception.
|
110
|
+
def url_for dest = nil
|
111
|
+
self.class.url_for dest
|
112
|
+
end
|
113
|
+
# same as #url_for, but returns the full URL (protocol:port:://host/path?params=foo)
|
114
|
+
def full_url_for dest
|
115
|
+
request.base_url + url_for(dest)
|
116
|
+
end
|
117
|
+
|
96
118
|
# this method adds data to be sent.
|
97
119
|
#
|
98
120
|
# this is usful for sending 'attachments' (data to be downloaded) rather then
|
@@ -234,7 +256,6 @@ module Plezi
|
|
234
256
|
self.class.__inner_redis_broadcast(uuid, method_name, args, &block) || self.class.__inner_process_broadcast(uuid, method_name.to_sym, args, &block)
|
235
257
|
end
|
236
258
|
|
237
|
-
|
238
259
|
# WebSockets.
|
239
260
|
#
|
240
261
|
# Use this to collect data from all 'sibling' websockets (websockets that have been created using the same Controller class).
|
@@ -274,8 +295,29 @@ module Plezi
|
|
274
295
|
end
|
275
296
|
|
276
297
|
module ClassMethods
|
298
|
+
protected
|
299
|
+
|
300
|
+
# Sets the HTTP route that is the owner of this controller.
|
301
|
+
#
|
302
|
+
# This is used by the Plezi framework internally and is supplied only for advanced purposes. It is better to avoid using this method.
|
303
|
+
def set_pl_route route
|
304
|
+
@pl_http_route = route
|
305
|
+
end
|
306
|
+
|
307
|
+
# Gets the HTTP route that is the owner of this controller.
|
308
|
+
#
|
309
|
+
# This is used to utilize the `url_for` method.
|
310
|
+
def get_pl_route
|
311
|
+
@pl_http_route
|
312
|
+
end
|
313
|
+
|
277
314
|
public
|
278
315
|
|
316
|
+
# This class method behaves the same way as the instance method #url_for. See the instance method's documentation for more details.
|
317
|
+
def url_for dest
|
318
|
+
get_pl_route.url_for dest
|
319
|
+
end
|
320
|
+
|
279
321
|
# lists the available methods that will be exposed to HTTP requests
|
280
322
|
def available_public_methods
|
281
323
|
# set class global to improve performance while checking for supported methods
|
@@ -14,12 +14,19 @@ module Plezi
|
|
14
14
|
def []= key, val
|
15
15
|
if key.is_a?(Symbol) && self.has_key?( key.to_s)
|
16
16
|
key = key.to_s
|
17
|
-
elsif
|
18
|
-
key = key.to_sym
|
17
|
+
elsif self.has_key?( key.to_s.to_sym)
|
18
|
+
key = key.to_s.to_sym
|
19
19
|
end
|
20
20
|
@controller.response.set_cookie key, (val ? val.to_s.dup : nil) if @controller
|
21
21
|
super
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
module Helpers
|
26
|
+
# a proc that allows Hashes to search for key-value pairs while also converting keys from objects to symbols and from symbols to strings.
|
27
|
+
#
|
28
|
+
# (key type agnostic search Hash proc)
|
29
|
+
HASH_SYM_PROC = Proc.new {|h,k| k = (Symbol === k ? k.to_s : k.to_s.to_sym); h[k] if h.has_key?(k) }
|
30
|
+
end
|
31
|
+
|
25
32
|
end
|
data/lib/plezi/handlers/route.rb
CHANGED
@@ -62,7 +62,7 @@ module Plezi
|
|
62
62
|
# a string can be either a simple string `"/users"` or a string with parameters:
|
63
63
|
# `"/static/:required/(:optional)/(:optional_with_format){[\d]*}/:optional_2"`
|
64
64
|
def initialize path, controller, params={}, &block
|
65
|
-
@
|
65
|
+
@original_path, @url_array, @params = path, false, params
|
66
66
|
initialize_path path
|
67
67
|
initialize_controller controller, block
|
68
68
|
end
|
@@ -75,10 +75,95 @@ module Plezi
|
|
75
75
|
@controller, @proc = controller, block
|
76
76
|
if controller.is_a?(Class)
|
77
77
|
# add controller magic
|
78
|
-
@controller = self.class.make_controller_magic controller
|
78
|
+
@controller = self.class.make_controller_magic controller, self
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
+
# # returns the url for THIS route (i.e. `url_for :index`)
|
83
|
+
# #
|
84
|
+
# # This will be usually used by the Controller's #url_for method to get the relative part of the url.
|
85
|
+
# def url_for dest = :index
|
86
|
+
# raise NotImplementedError, "#url_for isn't implemented for this router - could this be a Regexp based router?" unless @url_array
|
87
|
+
# # convert dest.id and dest[:id] to their actual :id value.
|
88
|
+
# dest = (dest.id rescue false) || (raise TypeError, "Expecting a Symbol, Hash, String, Numeric or an object that answers to obj[:id] or obj.id") unless !dest || dest.is_a?(Symbol) || dest.is_a?(String) || dest.is_a?(Numeric) || dest.is_a?(Hash)
|
89
|
+
# url = '/'
|
90
|
+
# case dest
|
91
|
+
# when false, nil, '', :index
|
92
|
+
# add = true
|
93
|
+
# @url_array.each do |sec|
|
94
|
+
# add = false unless sec[0] != :path
|
95
|
+
# url << sec[1] if add
|
96
|
+
# raise NotImplementedError, '#url_for(index) cannot be implementedfor this path.' if !add && sec[0] == :path
|
97
|
+
# # todo: :multi_path
|
98
|
+
# end
|
99
|
+
# when Hash
|
100
|
+
# when Symbol, String, Numeric
|
101
|
+
# end
|
102
|
+
# end
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
# returns the url for THIS route (i.e. `url_for :index`)
|
107
|
+
#
|
108
|
+
# This will be usually used by the Controller's #url_for method to get the relative part of the url.
|
109
|
+
def url_for dest = :index
|
110
|
+
raise NotImplementedError, "#url_for isn't implemented for this router - could this be a Regexp based router?" unless @url_array
|
111
|
+
case dest
|
112
|
+
when String
|
113
|
+
dest = {id: dest.dup}
|
114
|
+
when Numeric, Symbol
|
115
|
+
dest = {id: dest}
|
116
|
+
when Hash
|
117
|
+
dest = dest.dup
|
118
|
+
dest.each {|k,v| dest[k] = v.dup if v.is_a? String }
|
119
|
+
when nil, false
|
120
|
+
dest = {}
|
121
|
+
else
|
122
|
+
# convert dest.id and dest[:id] to their actual :id value.
|
123
|
+
dest = {id: (dest.id rescue false) || (raise TypeError, "Expecting a Symbol, Hash, String, Numeric or an object that answers to obj[:id] or obj.id") }
|
124
|
+
end
|
125
|
+
dest.default_proc = Plezi::Helpers::HASH_SYM_PROC
|
126
|
+
|
127
|
+
url = '/'
|
128
|
+
|
129
|
+
@url_array.each do |sec|
|
130
|
+
raise NotImplementedError, "#url_for isn't implemented for this router - Regexp multi-path routes are still being worked on... use a named parameter instead (i.e. '/foo/(:multi_route){route1|route2}/bar')" if REGEXP_FORMATTED_PATH === sec
|
131
|
+
|
132
|
+
param_name = (REGEXP_OPTIONAL_PARAMS.match(sec) || REGEXP_FORMATTED_OPTIONAL_PARAMS.match(sec) || REGEXP_REQUIRED_PARAMS.match(sec) || REGEXP_FORMATTED_REQUIRED_PARAMS.match(sec))
|
133
|
+
param_name = param_name[1].to_sym if param_name
|
134
|
+
|
135
|
+
if param_name && dest[param_name]
|
136
|
+
url << HTTP.encode(dest.delete(param_name).to_s, :url)
|
137
|
+
url << '/'
|
138
|
+
elsif !param_name
|
139
|
+
url << sec
|
140
|
+
url << '/'
|
141
|
+
elsif REGEXP_REQUIRED_PARAMS === sec || REGEXP_OPTIONAL_PARAMS === sec
|
142
|
+
url << '/'
|
143
|
+
elsif REGEXP_FORMATTED_REQUIRED_PARAMS === sec
|
144
|
+
raise ArgumentError, "URL can't be formatted becuse a required parameter (#{param_name.to_s}) isn't specified and it requires a special format (#{REGEXP_FORMATTED_REQUIRED_PARAMS.match(sec)[2]})."
|
145
|
+
end
|
146
|
+
end
|
147
|
+
unless dest.empty?
|
148
|
+
add = '?'
|
149
|
+
dest.each {|k, v| url << "#{add}#{HTTP.encode(k.to_s, :url)}=#{HTTP.encode(v.to_s, :url)}"; add = '&'}
|
150
|
+
end
|
151
|
+
url
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# Used to check for routes formatted: /:paramater - required parameters
|
157
|
+
REGEXP_REQUIRED_PARAMS = /^\:([^\(\)\{\}\:]*)$/
|
158
|
+
# Used to check for routes formatted: /(:paramater) - optional parameters
|
159
|
+
REGEXP_OPTIONAL_PARAMS = /^\(\:([^\(\)\{\}\:]*)\)$/
|
160
|
+
# Used to check for routes formatted: /(:paramater){regexp} - optional formatted parameters
|
161
|
+
REGEXP_FORMATTED_OPTIONAL_PARAMS = /^\(\:([^\(\)\{\}\:]*)\)\{(.*)\}$/
|
162
|
+
# Used to check for routes formatted: /:paramater{regexp} - required parameters
|
163
|
+
REGEXP_FORMATTED_REQUIRED_PARAMS = /^\:([^\(\)\{\}\:\/]*)\{(.*)\}$/
|
164
|
+
# Used to check for routes formatted: /{regexp} - required path
|
165
|
+
REGEXP_FORMATTED_PATH = /^\{(.*)\}$/
|
166
|
+
|
82
167
|
# initializes the path by converting the string into a Regexp
|
83
168
|
# and noting any parameters that might need to be extracted for RESTful routes.
|
84
169
|
def initialize_path path
|
@@ -88,46 +173,16 @@ module Plezi
|
|
88
173
|
elsif path.is_a? String
|
89
174
|
# prep used prameters
|
90
175
|
param_num = 0
|
176
|
+
|
91
177
|
section_search = "([\\/][^\\/]*)"
|
92
178
|
optional_section_search = "([\\/][^\\/]*)?"
|
93
179
|
|
94
|
-
# to check for routes formatted: /:paramater - required parameters
|
95
|
-
regexp_required_params = /^\:([^\(\)\{\}\:]*)$/
|
96
|
-
# to check for routes formatted: /(:paramater) - optional parameters
|
97
|
-
regexp_optional_params = /^\(\:([^\(\)\{\}\:]*)\)$/
|
98
|
-
# to check for routes formatted: /(:paramater){regexp} - optional formatted parameters
|
99
|
-
regexp_formatted_optional_params = /^\(\:([^\(\)\{\}\:]*)\)\{(.*)\}$/
|
100
|
-
# check for routes formatted: /:paramater{regexp} - required parameters
|
101
|
-
regexp_formatted_required_params = /^\:([^\(\)\{\}\:\/]*)\{(.*)\}$/
|
102
|
-
# check for routes formatted: /{regexp} - required path
|
103
|
-
regexp_formatted_path = /^\{(.*)\}$/
|
104
|
-
|
105
180
|
@path = '^'
|
106
|
-
|
107
|
-
# prep path string
|
108
|
-
# path = path.gsub(/(^\/)|(\/$)/, '')
|
109
|
-
|
110
|
-
# scan for the '/' divider
|
111
|
-
# (split path string only when the '/' is not inside a {} regexp)
|
112
|
-
# level = 0
|
113
|
-
# scn = StringScanner.new(path)
|
114
|
-
# while scn.matched != ''
|
115
|
-
# scn.scan_until /[^\\][\/\{\}]|$/
|
116
|
-
# case scn.matched
|
117
|
-
# when '{'
|
118
|
-
# level += 1
|
119
|
-
# when '}'
|
120
|
-
# level -= 1
|
121
|
-
# when '/'
|
122
|
-
# split_pos ||= []
|
123
|
-
# split_pos << scn.pos if level == 0
|
124
|
-
# end
|
125
|
-
# end
|
181
|
+
@url_array = []
|
126
182
|
|
127
183
|
# prep path string and split it where the '/' charected is unescaped.
|
128
|
-
|
129
|
-
@
|
130
|
-
path.each.with_index do |section, section_index|
|
184
|
+
@url_array = path.gsub(/(^\/)|(\/$)/, '').gsub(/([^\\])\//, '\1 - /').split ' - /'
|
185
|
+
@url_array.each.with_index do |section, section_index|
|
131
186
|
if section == '*'
|
132
187
|
# create catch all
|
133
188
|
section_index == 0 ? (@path << "(.*)") : (@path << "(\\/.*)?")
|
@@ -136,42 +191,41 @@ module Plezi
|
|
136
191
|
return
|
137
192
|
|
138
193
|
# check for routes formatted: /:paramater - required parameters
|
139
|
-
elsif section.match
|
194
|
+
elsif section.match REGEXP_REQUIRED_PARAMS
|
140
195
|
#create a simple section catcher
|
141
196
|
@path << section_search
|
142
197
|
# add paramater recognition value
|
143
|
-
@fill_parameters[param_num += 1] = section.match(
|
198
|
+
@fill_parameters[param_num += 1] = section.match(REGEXP_REQUIRED_PARAMS)[1]
|
144
199
|
|
145
200
|
# check for routes formatted: /(:paramater) - optional parameters
|
146
|
-
elsif section.match
|
201
|
+
elsif section.match REGEXP_OPTIONAL_PARAMS
|
147
202
|
#create a optional section catcher
|
148
203
|
@path << optional_section_search
|
149
204
|
# add paramater recognition value
|
150
|
-
|
205
|
+
@fill_parameters[param_num += 1] = section.match(REGEXP_OPTIONAL_PARAMS)[1]
|
151
206
|
|
152
207
|
# check for routes formatted: /(:paramater){regexp} - optional parameters
|
153
|
-
elsif section.match
|
208
|
+
elsif section.match REGEXP_FORMATTED_OPTIONAL_PARAMS
|
154
209
|
#create a optional section catcher
|
155
|
-
@path << ( "(\/(" + section.match(
|
210
|
+
@path << ( "(\/(" + section.match(REGEXP_FORMATTED_OPTIONAL_PARAMS)[2] + "))?" )
|
156
211
|
# add paramater recognition value
|
157
|
-
@fill_parameters[param_num += 1] = section.match(
|
212
|
+
@fill_parameters[param_num += 1] = section.match(REGEXP_FORMATTED_OPTIONAL_PARAMS)[1]
|
158
213
|
param_num += 1 # we are using two spaces - param_num += should look for () in regex ? /[^\\](/
|
159
214
|
|
160
215
|
# check for routes formatted: /:paramater{regexp} - required parameters
|
161
|
-
elsif section.match
|
216
|
+
elsif section.match REGEXP_FORMATTED_REQUIRED_PARAMS
|
162
217
|
#create a simple section catcher
|
163
|
-
@path << ( "(\/(" + section.match(
|
218
|
+
@path << ( "(\/(" + section.match(REGEXP_FORMATTED_REQUIRED_PARAMS)[2] + "))" )
|
164
219
|
# add paramater recognition value
|
165
|
-
@fill_parameters[param_num += 1] = section.match(
|
220
|
+
@fill_parameters[param_num += 1] = section.match(REGEXP_FORMATTED_REQUIRED_PARAMS)[1]
|
166
221
|
param_num += 1 # we are using two spaces - param_num += should look for () in regex ? /[^\\](/
|
167
222
|
|
168
223
|
# check for routes formatted: /{regexp} - formated path
|
169
|
-
elsif section.match
|
224
|
+
elsif section.match REGEXP_FORMATTED_PATH
|
170
225
|
#create a simple section catcher
|
171
|
-
@path << ( "\/(" + section.match(
|
226
|
+
@path << ( "\/(" + section.match(REGEXP_FORMATTED_PATH)[1] + ")" )
|
172
227
|
# add paramater recognition value
|
173
228
|
param_num += 1 # we are using one space - param_num += should look for () in regex ? /[^\\](/
|
174
|
-
|
175
229
|
else
|
176
230
|
@path << "\/"
|
177
231
|
@path << section
|
@@ -180,7 +234,9 @@ module Plezi
|
|
180
234
|
unless @fill_parameters.values.include?("id")
|
181
235
|
@path << optional_section_search
|
182
236
|
@fill_parameters[param_num += 1] = "id"
|
237
|
+
@url_array << '(:id)'
|
183
238
|
end
|
239
|
+
# set the Regexp and return the final result.
|
184
240
|
@path = /#{@path}$/
|
185
241
|
else
|
186
242
|
raise "Path cannot be initialized - path must be either a string or a regular experssion."
|
@@ -217,11 +273,11 @@ module Plezi
|
|
217
273
|
# cookies:: the request's cookies.
|
218
274
|
# flash:: an amazing Hash object that sets temporary cookies for one request only - greate for saving data between redirect calls.
|
219
275
|
#
|
220
|
-
def self.make_controller_magic(controller)
|
276
|
+
def self.make_controller_magic(controller, container)
|
221
277
|
new_class_name = "Plezi__#{controller.name.gsub /[\:\-\#\<\>\{\}\(\)\s]/, '_'}"
|
222
278
|
return Module.const_get new_class_name if Module.const_defined? new_class_name
|
223
279
|
# controller.include Plezi::ControllerMagic
|
224
|
-
controller.
|
280
|
+
controller.instance_exec(container) {|r| include Plezi::ControllerMagic; set_pl_route r;}
|
225
281
|
ret = Class.new(controller) do
|
226
282
|
|
227
283
|
def name
|
@@ -375,6 +431,7 @@ module Plezi
|
|
375
431
|
end
|
376
432
|
Object.const_set(new_class_name, ret)
|
377
433
|
Module.const_get(new_class_name).reset_routing_cache
|
434
|
+
ret.instance_exec(container) {|r| set_pl_route r;}
|
378
435
|
ret
|
379
436
|
end
|
380
437
|
|