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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09b8df30901e0e79a902246bc575f64e1c864659
4
- data.tar.gz: a84fb9fbe8d3230683b9229ef57e45a7752565f2
3
+ metadata.gz: 154b71e2b501ee4c31cadf2d4565912b4b2fb28f
4
+ data.tar.gz: aebcc97537ef16ce7f7eaa1cf6d050850cc37226
5
5
  SHA512:
6
- metadata.gz: 87ee5a7dd3d2b495ac2ed08dba3055d14291a760e212f85c4c0d5813018c51eec7dc8ababc34cb08805312a3b76dedb0627a087f3d94914f45e383066af7582d
7
- data.tar.gz: 397f07bd4faf2c386f6f63d442b8ee68dd013a07db3f468da778d44d19185b338db8cf2e5d1594c2beae35d02304db18e5676e058fd431b5235f02b2e6531f06
6
+ metadata.gz: 4258a8d8ca649e147103b918948e98e74bbab94b45320eee8f3caae54093880bf0de58cbb7a51f12ba4a2e0dc2969c62731dd58e38539758322b59ea1bd73b84
7
+ data.tar.gz: f382eafd4e539be20464227f177caf5e474693bf87dd23b44dff102ace7e260548f51c82bcb6fc597112ac296d7a3b138cb0cf585cef33adab01e5569ca193cf
data/lib/opi.rb CHANGED
@@ -10,6 +10,7 @@ require_relative './opi/request'
10
10
  require_relative './opi/response'
11
11
  require_relative './opi/context'
12
12
  require_relative './opi/loader'
13
+ require_relative './opi/resource'
13
14
 
14
15
  module Opi
15
- end
16
+ end
@@ -3,45 +3,12 @@ module Opi
3
3
 
4
4
  class << self
5
5
 
6
- def get(path, options={}, &block)
7
- route 'GET', path, options, block
6
+ def root
7
+ @root ||= Resource.new
8
8
  end
9
9
 
10
- def post(path, options={}, &block)
11
- route 'POST', path, options, block
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 = self.class.router.route(request.method, request.path)
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.to_s(:short)}"
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, self.class.before_filters, self.class.after_filters)
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
@@ -1,16 +1,14 @@
1
1
  module Opi
2
2
  class Context
3
3
 
4
- attr_reader :env, :logger, :route, :request, :response, :before, :after, :error
4
+ attr_reader :env, :logger, :route, :request, :response, :error
5
5
 
6
- def initialize(env, logger, route, request, response, before, after)
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
- (self.before + route_before).each do |before|
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
@@ -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
@@ -1,32 +1,47 @@
1
- class Router
2
- attr_accessor :routes
1
+ module Opi
2
+ class Router
3
+ attr_accessor :routes
3
4
 
4
- WILDCARD_PATTERN = /\/\*/
5
- NAMED_SEGMENTS_PATTERN = /\/:([^$\/]+)/
6
- NAMED_SEGMENTS_REPLACEMENT_PATTERN = /\/:([^$\/]+)/
5
+ WILDCARD_PATTERN = /\/\*/
6
+ NAMED_SEGMENTS_PATTERN = /\/:([^$\/]+)/
7
+ NAMED_SEGMENTS_REPLACEMENT_PATTERN = /\/:([^$\/]+)/
7
8
 
8
- def initialize(routes=[])
9
- @routes = routes
10
- end
9
+ def initialize(root)
10
+ @root = root
11
+ @routes = []
12
+ compile_routes(@root)
13
+ end
11
14
 
12
- def route(method, path)
13
- method_routes = self.routes.find_all{|x| x[:method] == method}
14
- method_routes.each do |route|
15
- if route[:path] =~ WILDCARD_PATTERN
16
- src = "\\A#{route[:path].gsub('*','(.*)')}\\Z"
17
- if match = path.match(Regexp.new(src))
18
- return [route, match[1].split('/')]
19
- end
20
- elsif route[:path] =~ NAMED_SEGMENTS_PATTERN
21
- src = "\\A#{route[:path].gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')}\\Z"
22
- if match = path.match(Regexp.new(src))
23
- return [route, Hash[match.names.zip(match.captures)]]
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
- end
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
@@ -1,3 +1,3 @@
1
1
  module Opi
2
- VERSION = "0.3.13"
2
+ VERSION = "0.4.1"
3
3
  end
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.3.13
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