opi 0.4.1 → 0.4.3

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: 154b71e2b501ee4c31cadf2d4565912b4b2fb28f
4
- data.tar.gz: aebcc97537ef16ce7f7eaa1cf6d050850cc37226
3
+ metadata.gz: 53ac0fb7d35e2dcbdd535f9f4142f0714abf6735
4
+ data.tar.gz: 059c4e77a4c7a9b62fd87871081afe080e4d1022
5
5
  SHA512:
6
- metadata.gz: 4258a8d8ca649e147103b918948e98e74bbab94b45320eee8f3caae54093880bf0de58cbb7a51f12ba4a2e0dc2969c62731dd58e38539758322b59ea1bd73b84
7
- data.tar.gz: f382eafd4e539be20464227f177caf5e474693bf87dd23b44dff102ace7e260548f51c82bcb6fc597112ac296d7a3b138cb0cf585cef33adab01e5569ca193cf
6
+ metadata.gz: 309590e6916000a3c7b1c0f2ee287cd3690be801ea3b3d2d497ae1095b1ff0428ace423b9073a83b816a5b7d1e8d4f8085810460444b6d3ebe8f8f696983d3d4
7
+ data.tar.gz: d0ab6dc92dd12939fa9975bd8472fe4d75bfbfeeb19b11f7a94c62cfc756009a0e6965e36d3ae9de5bb26b1282ecba03ed411da7ff8ca04e7af530601b564845
data/lib/opi.rb CHANGED
@@ -4,13 +4,15 @@ require 'json'
4
4
  require 'logger'
5
5
 
6
6
  require_relative './opi/version'
7
- require_relative './opi/router'
7
+ require_relative './opi/loader'
8
+
8
9
  require_relative './opi/api'
9
10
  require_relative './opi/request'
10
11
  require_relative './opi/response'
11
- require_relative './opi/context'
12
- require_relative './opi/loader'
13
12
  require_relative './opi/resource'
13
+ require_relative './opi/route'
14
+ require_relative './opi/router'
15
+ require_relative './opi/context'
14
16
 
15
17
  module Opi
16
18
  end
data/lib/opi/api.rb CHANGED
@@ -12,6 +12,7 @@ module Opi
12
12
  end
13
13
 
14
14
  def helpers(&block)
15
+ # TODO: allow argument to be a module
15
16
  mod = Module.new
16
17
  mod.class_eval &block
17
18
  Context.send :include, mod
@@ -26,6 +27,7 @@ module Opi
26
27
  @logger = options[:logger] || Logger.new(STDOUT)
27
28
  @logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
28
29
  @router = Router.new(self.class.root)
30
+ @router.routes.collect{|x| @logger.debug "Route: #{x.method} #{x.path}"}
29
31
  end
30
32
 
31
33
  def call(env)
@@ -46,7 +48,7 @@ module Opi
46
48
  logger.info " Parameters: #{request.params}"
47
49
 
48
50
  if route
49
- logger.debug "#{request.method} #{request.path} => route #{route.inspect}".green
51
+ logger.debug "#{request.method} #{request.path} => #{route.inspect}".green
50
52
  context = Context.new(env, logger, route, request, response)
51
53
  response = context.run
52
54
  else
data/lib/opi/context.rb CHANGED
@@ -21,13 +21,13 @@ module Opi
21
21
  end
22
22
 
23
23
  def run
24
- skip = route[:options][:skip] || []
24
+ skip = route.options[:skip] || []
25
25
  skip = [skip] unless skip.is_a? Array
26
26
 
27
- route_before = route[:options][:before] || []
27
+ route_before = route.options[:before] || []
28
28
  route_before = [route_before] unless route_before.is_a? Array
29
29
 
30
- (route[:before] + route_before).each do |before|
30
+ (route.before + route_before).each do |before|
31
31
  next if skip.include? before
32
32
 
33
33
  self.send before # execute before filter
@@ -40,7 +40,7 @@ module Opi
40
40
  end
41
41
 
42
42
  # before filters must have succeeded
43
- action = instance_eval &route[:block]
43
+ action = instance_eval &route.block
44
44
 
45
45
  if action.respond_to? :success?
46
46
  if action.success?
data/lib/opi/resource.rb CHANGED
@@ -37,8 +37,11 @@ module Opi
37
37
  end
38
38
 
39
39
  def resource(path, options={}, &block)
40
+ # TODO: clean this up, should be able to determine the child resource path
41
+ root = "#{self.root}/#{path}"
42
+ root = "#{self.root}/:id/#{path}" unless self.root.empty?
40
43
  resources << Resource.new(
41
- "#{self.root}/#{path}",
44
+ root,
42
45
  self.options.merge(options),
43
46
  self.before_filters.dup,
44
47
  self.after_filters.dup,
@@ -48,16 +51,15 @@ module Opi
48
51
 
49
52
  private
50
53
  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
- })
54
+ path = ":#{path}" if path.is_a? Symbol # TODO: maybe not?
55
+ routes.push Route.new(
56
+ method,
57
+ "#{self.root}/#{path}",
58
+ self.options.merge(options),
59
+ self.before_filters,
60
+ self.after_filters,
61
+ block
62
+ )
61
63
  end
62
64
 
63
65
  end
data/lib/opi/route.rb ADDED
@@ -0,0 +1,22 @@
1
+ module Opi
2
+ class Route
3
+ attr_reader :method, :path, :options, :before, :after, :block
4
+
5
+ def initialize(method, path, options, before, after, block)
6
+ @method = method
7
+ @path = clean_path(path)
8
+ @options = options
9
+ @before = before
10
+ @after = after
11
+ @block = block
12
+ end
13
+
14
+ private
15
+ def clean_path(path)
16
+ path = path.gsub(/\/\//, '/')
17
+ path = path.gsub(/\/$/, '') unless path == '/'
18
+ path
19
+ end
20
+
21
+ end
22
+ end
data/lib/opi/router.rb CHANGED
@@ -18,19 +18,19 @@ module Opi
18
18
  # end
19
19
 
20
20
  def route(method, path)
21
- method_routes = self.routes.find_all{|x| x[:method] == method}
21
+ method_routes = self.routes.find_all{|x| x.method == method}
22
22
  method_routes.each do |route|
23
- if route[:path] =~ WILDCARD_PATTERN
24
- src = "\\A#{route[:path].gsub('*','(.*)')}\\Z"
23
+ if route.path =~ WILDCARD_PATTERN
24
+ src = "\\A#{route.path.gsub('*','(.*)')}\\Z"
25
25
  if match = path.match(Regexp.new(src))
26
26
  return [route, match[1].split('/')]
27
27
  end
28
- elsif route[:path] =~ NAMED_SEGMENTS_PATTERN
29
- src = "\\A#{route[:path].gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')}\\Z"
28
+ elsif route.path =~ NAMED_SEGMENTS_PATTERN
29
+ src = "\\A#{route.path.gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')}\\Z"
30
30
  if match = path.match(Regexp.new(src))
31
31
  return [route, Hash[match.names.zip(match.captures)]]
32
32
  end
33
- elsif path == route[:path]
33
+ elsif path == route.path
34
34
  return [route]
35
35
  end
36
36
  end
data/lib/opi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Opi
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.3"
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.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Taylor
@@ -80,6 +80,7 @@ files:
80
80
  - lib/opi/request.rb
81
81
  - lib/opi/resource.rb
82
82
  - lib/opi/response.rb
83
+ - lib/opi/route.rb
83
84
  - lib/opi/router.rb
84
85
  - lib/opi/version.rb
85
86
  homepage: http://github.com/moomerman/opi