orbit-rb 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/orbit/controller.rb +24 -0
- data/lib/orbit/interceptors/base.rb +25 -0
- data/lib/orbit/interceptors/item.rb +17 -0
- data/lib/orbit/interceptors/list.rb +36 -0
- data/lib/orbit/loaders/directory_loader.rb +6 -1
- data/lib/orbit/loaders/file_reloader.rb +30 -0
- data/lib/orbit/response.rb +16 -0
- data/lib/orbit/router.rb +1 -1
- data/lib/orbit/version.rb +1 -1
- data/lib/orbit.rb +13 -3
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b74c5688905fd2955181080ff01041a27234d65
|
4
|
+
data.tar.gz: e6696a577daa1fa3fbf0176d57600e3d164173e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 011afb5588defb88810229b862eaf925b9020da5665c9b88c7df01e74d36af5ddf6b6eaa1ea68e161b0a27e22b83ac12a44d517acba849234bbf529d542974bc
|
7
|
+
data.tar.gz: 9db074c686fb0de31aed54a818227f67e0b27da34776f0934e195b95ce00e85a110b9f5d0a164f992cdf2a764323928b63da15f7cebec17f22432e6bc3c45f1a
|
data/lib/orbit/controller.rb
CHANGED
@@ -26,6 +26,10 @@ module Orbit
|
|
26
26
|
@base_path += path
|
27
27
|
end
|
28
28
|
|
29
|
+
def self.routes
|
30
|
+
@routes ||= []
|
31
|
+
end
|
32
|
+
|
29
33
|
def self.base_path
|
30
34
|
@base_path ||= '/'
|
31
35
|
end
|
@@ -55,6 +59,17 @@ module Orbit
|
|
55
59
|
end
|
56
60
|
|
57
61
|
def self.add_route(verb, action, &handler)
|
62
|
+
route = "#{verb.downcase}_#{action}"
|
63
|
+
|
64
|
+
if routes.include?(route)
|
65
|
+
update_method(verb, action, &handler)
|
66
|
+
else
|
67
|
+
routes.push(route)
|
68
|
+
create_route(verb, action, &handler)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.create_route(verb, action, &handler)
|
58
73
|
method_name = create_method(verb, action, &handler)
|
59
74
|
|
60
75
|
full_path = "#{@base_path}/#{action}"
|
@@ -100,6 +115,15 @@ module Orbit
|
|
100
115
|
method_name
|
101
116
|
end
|
102
117
|
|
118
|
+
def self.update_method(verb, action, &handler)
|
119
|
+
method = (action == '/') ? "root" : parameterize(action.to_s.gsub("*", "splat"))
|
120
|
+
method = "#{verb.downcase}_#{method}".to_sym
|
121
|
+
|
122
|
+
define_method method, &handler
|
123
|
+
|
124
|
+
method
|
125
|
+
end
|
126
|
+
|
103
127
|
def self.parameterize(string)
|
104
128
|
sep = '_'
|
105
129
|
# Turn unwanted chars into the separator
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Orbit
|
2
|
+
module Interceptors
|
3
|
+
class Base < Rack::Response
|
4
|
+
def initialize
|
5
|
+
super
|
6
|
+
@status = 302
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.execute
|
10
|
+
new.execute
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
@intercept = redirect(intercept, status)
|
15
|
+
|
16
|
+
|
17
|
+
@intercept ? self : nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def intercept
|
21
|
+
raise NotImplementedError.new("#intercept method must be implemented on #{self.class.name}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Orbit
|
2
|
+
module Interceptors
|
3
|
+
class Item
|
4
|
+
attr_accessor :path, :interceptor_class, :excludes
|
5
|
+
|
6
|
+
def initialize(path, interceptor_class, excludes=[])
|
7
|
+
@path = path
|
8
|
+
@interceptor_class = interceptor_class
|
9
|
+
@excludes = excludes
|
10
|
+
end
|
11
|
+
|
12
|
+
def match_path?(requested_path)
|
13
|
+
requested_path.match("^#{path}") && !excludes.include?(requested_path)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Orbit
|
2
|
+
module Interceptors
|
3
|
+
class List
|
4
|
+
include Singleton
|
5
|
+
attr_reader :interceptors
|
6
|
+
|
7
|
+
def initialize(interceptors=[])
|
8
|
+
@interceptors = interceptors
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.add_interceptor(path, interceptor_class, excludes=[])
|
12
|
+
interceptor = Item.new(path, interceptor_class, excludes)
|
13
|
+
|
14
|
+
instance.interceptors.push(interceptor)
|
15
|
+
end
|
16
|
+
|
17
|
+
def interceptors_for_path(path)
|
18
|
+
return [] unless path
|
19
|
+
|
20
|
+
interceptors.select do |item|
|
21
|
+
item.match_path?(path)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.intercept_path(path)
|
26
|
+
instance.interceptors_for_path(path).each do |hash|
|
27
|
+
result = hash.interceptor_class.execute
|
28
|
+
|
29
|
+
return result if result
|
30
|
+
end
|
31
|
+
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,15 +1,20 @@
|
|
1
1
|
module Orbit
|
2
2
|
module Loaders
|
3
3
|
class DirectoryLoader
|
4
|
+
attr_reader :reloader
|
5
|
+
|
4
6
|
def initialize
|
5
7
|
base_path = "#{Dir.pwd}/#{Orbit::Config.app_path}"
|
6
8
|
|
7
9
|
@retries = 0
|
8
10
|
@files = Dir["#{base_path}/**/*.rb"]
|
11
|
+
@reloader = FileReloader.new(files)
|
9
12
|
end
|
10
13
|
|
11
14
|
def self.load
|
12
|
-
new.
|
15
|
+
new.tap do |instance|
|
16
|
+
instance.load
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
20
|
def load
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Orbit
|
2
|
+
module Loaders
|
3
|
+
class FileReloader
|
4
|
+
def initialize(files)
|
5
|
+
@files = files
|
6
|
+
@last_updated = {}
|
7
|
+
set_last_updated_dates
|
8
|
+
end
|
9
|
+
|
10
|
+
def set_last_updated_dates
|
11
|
+
@files.each do |file|
|
12
|
+
@last_updated[file] = File.mtime(file).to_i
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def reload
|
17
|
+
@files.each do |file|
|
18
|
+
was_updated = File.mtime(file).to_i > @last_updated[file]
|
19
|
+
|
20
|
+
if was_updated
|
21
|
+
p "reloading #{file}"
|
22
|
+
|
23
|
+
load file
|
24
|
+
@last_updated[file] = File.mtime(file).to_i
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/orbit/response.rb
CHANGED
@@ -4,5 +4,21 @@ module Orbit
|
|
4
4
|
super
|
5
5
|
headers['Content-Type'] ||= 'text/html'
|
6
6
|
end
|
7
|
+
|
8
|
+
def self.not_found(verb, path)
|
9
|
+
body = ["Oops! No route for #{verb} #{path}"]
|
10
|
+
|
11
|
+
[404, {}, body]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.server_error(exception, verb, path)
|
15
|
+
body = "Error processing: #{verb} #{path}\n\n"
|
16
|
+
body += "#{exception.class.name}: #{exception.to_s}\n\n"
|
17
|
+
body += "Backtrace:\n\t#{exception.backtrace.join("\n\t")}"
|
18
|
+
|
19
|
+
Config.logger.error body
|
20
|
+
|
21
|
+
[500, {}, [body]]
|
22
|
+
end
|
7
23
|
end
|
8
24
|
end
|
data/lib/orbit/router.rb
CHANGED
data/lib/orbit/version.rb
CHANGED
data/lib/orbit.rb
CHANGED
@@ -16,7 +16,8 @@ module Orbit
|
|
16
16
|
setup_builder
|
17
17
|
load_middleware
|
18
18
|
|
19
|
-
Loaders::DirectoryLoader.load
|
19
|
+
loader = Loaders::DirectoryLoader.load
|
20
|
+
@reloader = loader.reloader
|
20
21
|
end
|
21
22
|
|
22
23
|
def setup_builder
|
@@ -71,6 +72,7 @@ module Orbit
|
|
71
72
|
end
|
72
73
|
|
73
74
|
def call(env)
|
75
|
+
@reloader.reload
|
74
76
|
@request = config.request_class.new(env)
|
75
77
|
verb = @request.request_method
|
76
78
|
requested_path = @request.path_info
|
@@ -78,13 +80,21 @@ module Orbit
|
|
78
80
|
route = Config.router_class.match(verb, requested_path)
|
79
81
|
|
80
82
|
if route
|
83
|
+
intercepted = Interceptors::List.intercept_path(requested_path)
|
84
|
+
|
85
|
+
return intercepted if intercepted
|
86
|
+
|
81
87
|
route_params = route[:route].path.get_params(requested_path) || {}
|
82
88
|
|
83
89
|
@request.params.merge!(route_params)
|
84
90
|
|
85
|
-
|
91
|
+
begin
|
92
|
+
route[:class].execute_action(@request, route[:action])
|
93
|
+
rescue Exception => exception
|
94
|
+
Config.response_class.server_error(exception, verb, requested_path)
|
95
|
+
end
|
86
96
|
else
|
87
|
-
|
97
|
+
Config.response_class.not_found(verb, requested_path)
|
88
98
|
end
|
89
99
|
end
|
90
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: orbit-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caio Torres
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -100,7 +100,11 @@ files:
|
|
100
100
|
- lib/orbit.rb
|
101
101
|
- lib/orbit/config.rb
|
102
102
|
- lib/orbit/controller.rb
|
103
|
+
- lib/orbit/interceptors/base.rb
|
104
|
+
- lib/orbit/interceptors/item.rb
|
105
|
+
- lib/orbit/interceptors/list.rb
|
103
106
|
- lib/orbit/loaders/directory_loader.rb
|
107
|
+
- lib/orbit/loaders/file_reloader.rb
|
104
108
|
- lib/orbit/request.rb
|
105
109
|
- lib/orbit/response.rb
|
106
110
|
- lib/orbit/router.rb
|