culpa 0.1.0.5 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/bin/culpa +0 -12
- data/lib/action.rb +9 -13
- data/lib/culpa.rb +34 -46
- data/lib/path_parser.rb +44 -0
- data/templates/config.ru +0 -12
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODRhZjgzN2MwYjZlMWVjNjZhMzk4NWYxYjFhOTE3YzQ5YzE3NjhlYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjJlMWU0ZTViOWE1NTFmM2E3ZmQ0YzY0NzMxM2RjYWM1ODBkMGM5Nw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODk0NTAzM2FjZjQ5MmM5NDA3Yjc0MTZlOGEzNzFmODVkMzM3Y2NhNzM4ZTc4
|
10
|
+
OTA1N2JhNDFhODEwZDdjMTA4NTI3ZGUxOWRmZTM5ZTM3NzIxZjk0NzhiNDM4
|
11
|
+
ZjQyMDBhM2Q5ODc4NTAwZGVmZjdhNTBhYzk3YjM1NDg3NmE0YTE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2VkOWY1NzFjOGZmYzMzNDNmYWM0MDlhNzZhNWU0Y2ZhMTJiZDI0YzYxMmQz
|
14
|
+
ZjE0OTc2NWMxMDBlOThlMDRhZDllN2UxZTRiZmNkM2EwNGU2ZjVmMDgyNjQ3
|
15
|
+
ZTg0Njk2ODM2NGYyOTY5NmJjOTMwOTZlYjY1NGRlMmI3MmU0MjU=
|
data/bin/culpa
CHANGED
@@ -2,18 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'fileutils'
|
4
4
|
|
5
|
-
module Actions
|
6
|
-
Dir['./actions/*.rb'].each do |file|
|
7
|
-
eval(File.open(file).read)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module Models
|
12
|
-
Dir['./models/*.rb'].each do |file|
|
13
|
-
eval(File.open(file).read)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
5
|
def create_project(project_path)
|
18
6
|
puts "Initializing Culpa project in #{project_path}"
|
19
7
|
|
data/lib/action.rb
CHANGED
@@ -1,19 +1,15 @@
|
|
1
|
-
|
1
|
+
class Action
|
2
2
|
|
3
|
-
|
3
|
+
attr_accessor :to_render
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@to_render = nil
|
11
|
-
end
|
12
|
-
|
13
|
-
def render(response_object, options={})
|
14
|
-
@to_render = {object: response_object, options: options}
|
15
|
-
end
|
5
|
+
def initialize(envelope, request)
|
6
|
+
@e = envelope
|
7
|
+
@r = request
|
8
|
+
@to_render = nil
|
9
|
+
end
|
16
10
|
|
11
|
+
def render(response_object, options={})
|
12
|
+
@to_render = {object: response_object, options: options}
|
17
13
|
end
|
18
14
|
|
19
15
|
end
|
data/lib/culpa.rb
CHANGED
@@ -1,39 +1,33 @@
|
|
1
1
|
require 'bundler'
|
2
|
-
require 'envelope'
|
3
|
-
require 'action'
|
4
2
|
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
require 'envelope'
|
6
|
+
require 'path_parser'
|
7
|
+
|
8
|
+
ACTIONS_PATH ||= './actions/*.rb'
|
9
|
+
MODELS_PATH ||= './models/*.rb'
|
10
|
+
|
11
|
+
module Actions
|
12
|
+
require 'action'
|
13
|
+
Dir[ACTIONS_PATH].each do |file|
|
14
|
+
require file
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Models
|
19
|
+
Dir[MODELS_PATH].each do |file|
|
20
|
+
require file
|
21
|
+
end
|
22
|
+
end
|
5
23
|
|
6
24
|
module Culpa
|
7
25
|
|
8
|
-
|
9
|
-
|
10
|
-
regex: Regexp.new('^/(\w+)$'),
|
11
|
-
id_to_sym: {
|
12
|
-
1 => :res_name
|
13
|
-
}
|
14
|
-
},
|
15
|
-
'/:res_name/:id' => {
|
16
|
-
regex: Regexp.new('^/(\w+)/(\w+)$'),
|
17
|
-
id_to_sym: {
|
18
|
-
1 => :res_name,
|
19
|
-
2 => :id
|
20
|
-
}
|
21
|
-
},
|
22
|
-
'/:res_name/:id/:sub_call' => {
|
23
|
-
regex: Regexp.new('^/(\w+)/(\w+)/(\w+)$'),
|
24
|
-
id_to_sym: {
|
25
|
-
1 => :res_name,
|
26
|
-
2 => :id,
|
27
|
-
3 => :sub_call
|
28
|
-
}
|
29
|
-
}
|
30
|
-
}
|
26
|
+
class UnpredictableSubCallError < StandardError; end
|
27
|
+
class NoRenderCalled < StandardError; end
|
31
28
|
|
32
29
|
class Application
|
33
30
|
|
34
|
-
class UnpredictableSubCallError < StandardError; end
|
35
|
-
class NoRenderCalled < StandardError; end
|
36
|
-
|
37
31
|
def initialize(router = {})
|
38
32
|
@router = router
|
39
33
|
end
|
@@ -72,25 +66,28 @@ module Culpa
|
|
72
66
|
raise NoRenderCalled.new
|
73
67
|
end
|
74
68
|
|
69
|
+
# Render a view
|
75
70
|
def do_render(to_render)
|
76
71
|
code = to_render[:options][:status] || 200
|
77
72
|
content_type = to_render[:options][:content_type] || 'application/json'
|
78
73
|
[code.to_s, {'Content-Type' => content_type}, [to_render[:object].to_json]]
|
79
74
|
end
|
80
75
|
|
76
|
+
# Called by rack
|
81
77
|
def call(env)
|
82
78
|
call_options = {
|
83
79
|
verb: env['REQUEST_METHOD'].downcase.to_sym
|
84
80
|
}
|
85
|
-
if env['
|
86
|
-
|
87
|
-
|
81
|
+
if env['CONTENT_TYPE'] == 'application/json'
|
82
|
+
body = JSON.parse(env['rack.input'].read)
|
83
|
+
if body.has_key? 'sub_call'
|
84
|
+
call_options[:sub_call] = body['sub_call']
|
88
85
|
end
|
89
|
-
call_options[:input] =
|
86
|
+
call_options[:input] = body['data']
|
90
87
|
else
|
91
88
|
call_options[:input] = env['rack.input']
|
92
89
|
end
|
93
|
-
if !(route_params =
|
90
|
+
if !(route_params = PathParser.parse('/:res_name', env['PATH_INFO'])).nil?
|
94
91
|
call_options[:res_name] = route_params[:res_name]
|
95
92
|
case call_options[:verb]
|
96
93
|
when :get
|
@@ -100,7 +97,7 @@ module Culpa
|
|
100
97
|
else
|
101
98
|
raise UnpredictableSubCallError.new
|
102
99
|
end unless call_options.has_key? :sub_call
|
103
|
-
elsif !(route_params =
|
100
|
+
elsif !(route_params = PathParser.parse('/:res_name/:id', env['PATH_INFO'])).nil?
|
104
101
|
call_options[:res_name] = route_params[:res_name]
|
105
102
|
call_options[:id] = route_params[:id]
|
106
103
|
case call_options[:verb]
|
@@ -113,7 +110,7 @@ module Culpa
|
|
113
110
|
else
|
114
111
|
raise UnpredictableSubCallError.new
|
115
112
|
end unless call_options.has_key? :sub_call
|
116
|
-
elsif !(route_params =
|
113
|
+
elsif !(route_params = PathParser.parse('/:res_name/:id/:sub_call', env['PATH_INFO'])).nil?
|
117
114
|
call_options[:res_name] = route_params[:res_name]
|
118
115
|
call_options[:id] = route_params[:id]
|
119
116
|
call_options[:sub_call] = route_params[:sub_call]
|
@@ -123,21 +120,12 @@ module Culpa
|
|
123
120
|
call_action call_options
|
124
121
|
end
|
125
122
|
|
126
|
-
|
127
|
-
current_pattern = ROUTE_PATTERNS[pattern]
|
128
|
-
result = path.match(current_pattern[:regex])
|
129
|
-
return unless result
|
130
|
-
returned = {}
|
131
|
-
current_pattern[:id_to_sym].each do |id, sym|
|
132
|
-
returned[sym] = result[id]
|
133
|
-
end
|
134
|
-
returned
|
135
|
-
end
|
136
|
-
|
123
|
+
# 400 : Bad request
|
137
124
|
def bad_request
|
138
125
|
['400', {'Content-Type' => 'application/json'}, ['{ "error": "bad_request" }']]
|
139
126
|
end
|
140
127
|
|
128
|
+
# 404 : Not found
|
141
129
|
def not_found
|
142
130
|
['404', {'Content-Type' => 'application/json'}, ['{ "error": "not_found" }']]
|
143
131
|
end
|
data/lib/path_parser.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Culpa
|
2
|
+
|
3
|
+
class PathParser
|
4
|
+
|
5
|
+
ROUTE_PATTERNS = {
|
6
|
+
'/:res_name' => {
|
7
|
+
regex: Regexp.new('^/(\w+)$'),
|
8
|
+
id_to_sym: {
|
9
|
+
1 => :res_name
|
10
|
+
}
|
11
|
+
},
|
12
|
+
'/:res_name/:id' => {
|
13
|
+
regex: Regexp.new('^/(\w+)/(\w+)$'),
|
14
|
+
id_to_sym: {
|
15
|
+
1 => :res_name,
|
16
|
+
2 => :id
|
17
|
+
}
|
18
|
+
},
|
19
|
+
'/:res_name/:id/:sub_call' => {
|
20
|
+
regex: Regexp.new('^/(\w+)/(\w+)/(\w+)$'),
|
21
|
+
id_to_sym: {
|
22
|
+
1 => :res_name,
|
23
|
+
2 => :id,
|
24
|
+
3 => :sub_call
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
# Parsing a path
|
30
|
+
def self.parse(pattern, path)
|
31
|
+
current_pattern = ROUTE_PATTERNS[pattern]
|
32
|
+
result = path.match(current_pattern[:regex])
|
33
|
+
return unless result
|
34
|
+
returned = {}
|
35
|
+
current_pattern[:id_to_sym].each do |id, sym|
|
36
|
+
returned[sym] = result[id]
|
37
|
+
end
|
38
|
+
returned
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/templates/config.ru
CHANGED
@@ -3,16 +3,4 @@ require 'bundler/setup'
|
|
3
3
|
require 'yaml'
|
4
4
|
require 'culpa'
|
5
5
|
|
6
|
-
module Actions
|
7
|
-
Dir['./actions/*.rb'].each do |file|
|
8
|
-
eval(File.open(file).read)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
module Models
|
13
|
-
Dir['./models/*.rb'].each do |file|
|
14
|
-
eval(File.open(file).read)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
6
|
run Culpa::Application.new(YAML.load_file('./config/router.yml'))
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: culpa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jérémy SEBAN
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/action.rb
|
50
50
|
- lib/culpa.rb
|
51
51
|
- lib/envelope.rb
|
52
|
+
- lib/path_parser.rb
|
52
53
|
- templates/Gemfile
|
53
54
|
- templates/config.ru
|
54
55
|
homepage: https://github.com/HipsterWhale/culpa
|