opi 0.3.13 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/opi.rb +2 -1
- data/lib/opi/api.rb +9 -41
- data/lib/opi/context.rb +3 -5
- data/lib/opi/resource.rb +64 -0
- data/lib/opi/router.rb +40 -25
- data/lib/opi/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 154b71e2b501ee4c31cadf2d4565912b4b2fb28f
|
4
|
+
data.tar.gz: aebcc97537ef16ce7f7eaa1cf6d050850cc37226
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4258a8d8ca649e147103b918948e98e74bbab94b45320eee8f3caae54093880bf0de58cbb7a51f12ba4a2e0dc2969c62731dd58e38539758322b59ea1bd73b84
|
7
|
+
data.tar.gz: f382eafd4e539be20464227f177caf5e474693bf87dd23b44dff102ace7e260548f51c82bcb6fc597112ac296d7a3b138cb0cf585cef33adab01e5569ca193cf
|
data/lib/opi.rb
CHANGED
data/lib/opi/api.rb
CHANGED
@@ -3,45 +3,12 @@ module Opi
|
|
3
3
|
|
4
4
|
class << self
|
5
5
|
|
6
|
-
def
|
7
|
-
|
6
|
+
def root
|
7
|
+
@root ||= Resource.new
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
def put(path, options={}, &block)
|
15
|
-
route 'PUT', path, options, block
|
16
|
-
end
|
17
|
-
|
18
|
-
def delete(path, options={}, &block)
|
19
|
-
route 'DELETE', path, options, block
|
20
|
-
end
|
21
|
-
|
22
|
-
def route(method, path, options={}, block)
|
23
|
-
# TODO: remove&replace existing routes (on reload)
|
24
|
-
router.routes.unshift({:method => method, :path => path, :options => options, :block => block})
|
25
|
-
end
|
26
|
-
|
27
|
-
def before(method)
|
28
|
-
before_filters << method
|
29
|
-
end
|
30
|
-
|
31
|
-
def after(method)
|
32
|
-
after_filters << method
|
33
|
-
end
|
34
|
-
|
35
|
-
def before_filters
|
36
|
-
@before_filters ||= []
|
37
|
-
end
|
38
|
-
|
39
|
-
def after_filters
|
40
|
-
@after_filters ||= []
|
41
|
-
end
|
42
|
-
|
43
|
-
def router
|
44
|
-
@router ||= Router.new
|
10
|
+
def method_missing(method, *args, &block)
|
11
|
+
root.respond_to?(method) ? root.send(method, *args, &block) : super
|
45
12
|
end
|
46
13
|
|
47
14
|
def helpers(&block)
|
@@ -58,6 +25,7 @@ module Opi
|
|
58
25
|
puts "* Opi Version: #{Opi::VERSION} initializing".green
|
59
26
|
@logger = options[:logger] || Logger.new(STDOUT)
|
60
27
|
@logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
|
28
|
+
@router = Router.new(self.class.root)
|
61
29
|
end
|
62
30
|
|
63
31
|
def call(env)
|
@@ -69,17 +37,17 @@ module Opi
|
|
69
37
|
begin
|
70
38
|
Loader.reload!
|
71
39
|
|
72
|
-
route, params =
|
40
|
+
route, params = @router.route(request.method, request.path)
|
73
41
|
request.params.merge!(params) if params and params.is_a? Hash
|
74
42
|
request.params.merge!('splat' => params.join(',')) if params and params.is_a? Array
|
75
43
|
|
76
44
|
start_time = Time.now
|
77
|
-
logger.info " Started #{request.method} \"#{request.path}\" for #{request.ip} at #{start_time.
|
45
|
+
logger.info " Started #{request.method} \"#{request.path}\" for #{request.ip} at #{start_time.strftime('%d %b %H:%M')}"
|
78
46
|
logger.info " Parameters: #{request.params}"
|
79
|
-
|
47
|
+
|
80
48
|
if route
|
81
49
|
logger.debug "#{request.method} #{request.path} => route #{route.inspect}".green
|
82
|
-
context = Context.new(env, logger, route, request, response
|
50
|
+
context = Context.new(env, logger, route, request, response)
|
83
51
|
response = context.run
|
84
52
|
else
|
85
53
|
logger.debug "#{request.method} #{request.path} => route not found".red
|
data/lib/opi/context.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
module Opi
|
2
2
|
class Context
|
3
3
|
|
4
|
-
attr_reader :env, :logger, :route, :request, :response, :
|
4
|
+
attr_reader :env, :logger, :route, :request, :response, :error
|
5
5
|
|
6
|
-
def initialize(env, logger, route, request, response
|
6
|
+
def initialize(env, logger, route, request, response)
|
7
7
|
@env = env
|
8
8
|
@logger = logger
|
9
9
|
@route = route
|
10
10
|
@request = request
|
11
11
|
@response = response
|
12
|
-
@before = before
|
13
|
-
@after = after
|
14
12
|
@error = nil
|
15
13
|
end
|
16
14
|
|
@@ -29,7 +27,7 @@ module Opi
|
|
29
27
|
route_before = route[:options][:before] || []
|
30
28
|
route_before = [route_before] unless route_before.is_a? Array
|
31
29
|
|
32
|
-
(
|
30
|
+
(route[:before] + route_before).each do |before|
|
33
31
|
next if skip.include? before
|
34
32
|
|
35
33
|
self.send before # execute before filter
|
data/lib/opi/resource.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Opi
|
2
|
+
class Resource
|
3
|
+
attr_reader :root, :options, :resources, :before_filters, :after_filters, :routes
|
4
|
+
|
5
|
+
def initialize(root='', options={}, before=[], after=[], block=nil)
|
6
|
+
@root = root
|
7
|
+
@options = options
|
8
|
+
@before_filters = before
|
9
|
+
@after_filters = after
|
10
|
+
@resources = []
|
11
|
+
@routes = []
|
12
|
+
instance_eval &block if block
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(path=nil, options={}, &block)
|
16
|
+
route 'GET', path, options, block
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(path=nil, options={}, &block)
|
20
|
+
route 'POST', path, options, block
|
21
|
+
end
|
22
|
+
|
23
|
+
def put(path=nil, options={}, &block)
|
24
|
+
route 'PUT', path, options, block
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(path=nil, options={}, &block)
|
28
|
+
route 'DELETE', path, options, block
|
29
|
+
end
|
30
|
+
|
31
|
+
def before(method)
|
32
|
+
before_filters << method
|
33
|
+
end
|
34
|
+
|
35
|
+
def after(method)
|
36
|
+
after_filters << method
|
37
|
+
end
|
38
|
+
|
39
|
+
def resource(path, options={}, &block)
|
40
|
+
resources << Resource.new(
|
41
|
+
"#{self.root}/#{path}",
|
42
|
+
self.options.merge(options),
|
43
|
+
self.before_filters.dup,
|
44
|
+
self.after_filters.dup,
|
45
|
+
block
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def route(method, path, options={}, block)
|
51
|
+
full_path = "#{self.root}/#{path}".gsub(/\/\//, '/')
|
52
|
+
full_path.gsub!(/\/$/, '') unless full_path == '/'
|
53
|
+
routes.unshift({
|
54
|
+
:method => method,
|
55
|
+
:path => full_path,
|
56
|
+
:options => self.options.merge(options),
|
57
|
+
:before => self.before_filters,
|
58
|
+
:after => self.after_filters,
|
59
|
+
:block => block
|
60
|
+
})
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
data/lib/opi/router.rb
CHANGED
@@ -1,32 +1,47 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Opi
|
2
|
+
class Router
|
3
|
+
attr_accessor :routes
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
WILDCARD_PATTERN = /\/\*/
|
6
|
+
NAMED_SEGMENTS_PATTERN = /\/:([^$\/]+)/
|
7
|
+
NAMED_SEGMENTS_REPLACEMENT_PATTERN = /\/:([^$\/]+)/
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def initialize(root)
|
10
|
+
@root = root
|
11
|
+
@routes = []
|
12
|
+
compile_routes(@root)
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
# def route(method, path, options={}, block)
|
16
|
+
# # TODO: remove&replace existing routes (on reload)
|
17
|
+
# router.routes.unshift({:method => method, :path => path, :options => options, :block => block})
|
18
|
+
# end
|
19
|
+
|
20
|
+
def route(method, path)
|
21
|
+
method_routes = self.routes.find_all{|x| x[:method] == method}
|
22
|
+
method_routes.each do |route|
|
23
|
+
if route[:path] =~ WILDCARD_PATTERN
|
24
|
+
src = "\\A#{route[:path].gsub('*','(.*)')}\\Z"
|
25
|
+
if match = path.match(Regexp.new(src))
|
26
|
+
return [route, match[1].split('/')]
|
27
|
+
end
|
28
|
+
elsif route[:path] =~ NAMED_SEGMENTS_PATTERN
|
29
|
+
src = "\\A#{route[:path].gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')}\\Z"
|
30
|
+
if match = path.match(Regexp.new(src))
|
31
|
+
return [route, Hash[match.names.zip(match.captures)]]
|
32
|
+
end
|
33
|
+
elsif path == route[:path]
|
34
|
+
return [route]
|
24
35
|
end
|
25
|
-
elsif path == route[:path]
|
26
|
-
return [route]
|
27
36
|
end
|
37
|
+
nil
|
28
38
|
end
|
29
|
-
nil
|
30
|
-
end
|
31
39
|
|
32
|
-
|
40
|
+
private
|
41
|
+
def compile_routes(root)
|
42
|
+
root.routes.each{|x| self.routes.unshift x}
|
43
|
+
root.resources.each{|x| compile_routes(x)}
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/opi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Taylor
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- lib/opi/context.rb
|
79
79
|
- lib/opi/loader.rb
|
80
80
|
- lib/opi/request.rb
|
81
|
+
- lib/opi/resource.rb
|
81
82
|
- lib/opi/response.rb
|
82
83
|
- lib/opi/router.rb
|
83
84
|
- lib/opi/version.rb
|