culpa 0.7.1.1 → 0.7.2.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/lib/action.rb +4 -10
- data/lib/culpa.rb +23 -33
- data/lib/{file_helpers.rb → file_streamer.rb} +3 -3
- data/lib/rspec_helper.rb +2 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjY1YjM1ZmM4ZGJiYzBhYmMxMTM4MzMzYmEzMTZjYjg4OGU0YjZmOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MTBjYTBhYmY5OGIyYWUyYTgyZWMxNjlhODFhZTRiZDE4YTllYzgyOA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGRjZDI4OTBjYTYxYWNkMTRlZjA2YTFiZTk1NWM3NWU0ZTI0MTRmYzg4NGM5
|
10
|
+
NzUwZmU5ZTExMWJmZTczYjZjOTFjYTU3YWU3Y2U5YjRlZDNhYjE4ZjQxYmEx
|
11
|
+
YmNkOGRjYjA4MTE0MzNhNGY4ZjM2ZDIzNTI3NDczMmM0ZTk4OGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Mzg5ZDc5YmFjNjIwN2ZlZmYzZjM1ZDlhNjNjZWJkMjQwNGRkMDI2MTFiZDhl
|
14
|
+
YWM4NTc1MDM4MWIxMDM5OTIxY2JlMmZjYjk2Mjk3MDA3ZGMwZmNiMGMwOGQ2
|
15
|
+
NzEzZGU2NGExMTJhOThiMGVkNjFkZjllMmI1ZDUyZDYxZTgwMzU=
|
data/lib/action.rb
CHANGED
@@ -56,21 +56,15 @@ class Action
|
|
56
56
|
}
|
57
57
|
|
58
58
|
attr_accessor :to_render
|
59
|
+
attr_reader :e, :r, :logger
|
59
60
|
|
60
|
-
def initialize(envelope, request)
|
61
|
+
def initialize(envelope, request, logger)
|
61
62
|
@e = envelope
|
62
63
|
@r = request
|
64
|
+
@logger = logger
|
63
65
|
@to_render = nil
|
64
66
|
end
|
65
67
|
|
66
|
-
def e
|
67
|
-
@e
|
68
|
-
end
|
69
|
-
|
70
|
-
def r
|
71
|
-
@r
|
72
|
-
end
|
73
|
-
|
74
68
|
def render(options={})
|
75
69
|
options[:headers] ||= {}
|
76
70
|
if options.has_key? :json
|
@@ -88,7 +82,7 @@ class Action
|
|
88
82
|
'Content-Disposition' => "#{options[:disposition].to_s || 'attachment'}; filename=#{options[:filename] || 'unknown'}",
|
89
83
|
}.merge(options[:headers]),
|
90
84
|
status: RETURN_CODES[options[:status]] || 200,
|
91
|
-
object:
|
85
|
+
object: Culpa::FileStreamer.new(options[:file])
|
92
86
|
}
|
93
87
|
@to_render[:headers]['Content-Length'] = options[:size] if options.has_key? :size
|
94
88
|
elsif options.has_key? :status
|
data/lib/culpa.rb
CHANGED
@@ -5,9 +5,9 @@ require 'logger'
|
|
5
5
|
|
6
6
|
require 'envelope'
|
7
7
|
require 'path_parser'
|
8
|
-
require '
|
8
|
+
require 'file_streamer'
|
9
9
|
|
10
|
-
CULPA_VERSION='0.7.
|
10
|
+
CULPA_VERSION='0.7.2.1'
|
11
11
|
|
12
12
|
ACTIONS_PATH ||= './actions/*.rb'
|
13
13
|
MODELS_PATH ||= './models/*.rb'
|
@@ -41,7 +41,12 @@ module Culpa
|
|
41
41
|
def initialize(options = {})
|
42
42
|
@router = YAML.load_file(options[:router] || './config/router.yml')
|
43
43
|
@logger = Logger.new(options[:log_output] || STDOUT)
|
44
|
-
@logger.level = ENV['RACK_ENV']
|
44
|
+
@logger.level = case ENV['RACK_ENV']
|
45
|
+
when 'development', 'test'
|
46
|
+
Logger::DEBUG
|
47
|
+
else
|
48
|
+
Logger::WARN
|
49
|
+
end
|
45
50
|
@logger.info 'Culpa fully initialized'
|
46
51
|
end
|
47
52
|
|
@@ -52,7 +57,7 @@ module Culpa
|
|
52
57
|
verb: env['REQUEST_METHOD'].downcase.to_sym,
|
53
58
|
params: Rack::Utils.parse_nested_query(env['QUERY_STRING'])
|
54
59
|
}
|
55
|
-
# Parse body if in JSON
|
60
|
+
# Parse body if in JSON, otherwise pass the rack.input directly
|
56
61
|
if env['CONTENT_TYPE'] == 'application/json'
|
57
62
|
body = JSON.parse(env['rack.input'].read)
|
58
63
|
call_options[:sub_call] = body['sub_call'] if body.has_key? 'sub_call'
|
@@ -60,7 +65,7 @@ module Culpa
|
|
60
65
|
else
|
61
66
|
call_options[:input] = env['rack.input']
|
62
67
|
end
|
63
|
-
# Try the
|
68
|
+
# Try the enforced routes
|
64
69
|
if !(route_params = PathParser.parse('/:res_name', env['PATH_INFO'])).nil?
|
65
70
|
call_options[:res_name] = route_params[:res_name]
|
66
71
|
case call_options[:verb]
|
@@ -90,38 +95,35 @@ module Culpa
|
|
90
95
|
call_options[:sub_call] = route_params[:sub_call]
|
91
96
|
else
|
92
97
|
@logger.info "Refused request, #{env['PATH_INFO']} didn't match enforced routes"
|
93
|
-
return
|
98
|
+
return rack_error 400
|
94
99
|
end
|
95
100
|
# Call the brickchain and return the value to Rack
|
96
101
|
@logger.info "Calling brickchain => subcall: #{call_options[:sub_call]}, action: #{call_options[:res_name]}, id: #{call_options[:id]}"
|
97
102
|
call_brickchain call_options
|
98
103
|
rescue UnpredictableSubCallError, JSON::ParserError
|
99
|
-
|
104
|
+
rack_error 400
|
100
105
|
rescue StandardError => error
|
101
106
|
@logger.error "Error ecountered while treating request : #{error.message}, aborting...\n#{error.backtrace.join("\n")}"
|
102
|
-
|
107
|
+
rack_error 500
|
103
108
|
end
|
104
109
|
|
105
110
|
def call_brickchain(options)
|
106
111
|
router_method_name = "#{options[:sub_call]}_#{options[:res_name]}"
|
107
112
|
envelope = Envelope.new
|
108
113
|
request = EnvelopeRequest.new(options)
|
109
|
-
|
110
|
-
return not_found unless bricks
|
114
|
+
return rack_error(404) unless @router.has_key? router_method_name
|
111
115
|
@logger.info "Brickchain determined for #{router_method_name}"
|
112
|
-
|
113
|
-
# Iterating over
|
116
|
+
@router[router_method_name].each do |brick|
|
117
|
+
# Iterating over the brickchain of this request
|
114
118
|
if brick.is_a? String
|
115
119
|
# This brick is a sychronous call, processing call directly.
|
116
120
|
action_class = brick_call brick, envelope, request
|
117
121
|
return do_render(action_class.to_render) if action_class.to_render
|
118
122
|
elsif brick.is_a?(Hash) && brick.has_key?('async')
|
119
123
|
# This brick is a bundle of async bricks, treating it in threads.
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
threads.each { |thread|
|
124
|
+
brick['async'].map { |async_brick|
|
125
|
+
Thread.new { brick_call(async_brick, envelope, request) }
|
126
|
+
}.each { |thread|
|
125
127
|
action_class = thread.value
|
126
128
|
return do_render(action_class.to_render) if action_class.to_render
|
127
129
|
}
|
@@ -134,7 +136,7 @@ module Culpa
|
|
134
136
|
# Call a brick and get the instancied class of the brick's parent action
|
135
137
|
def brick_call(brick, envelope, request)
|
136
138
|
action_class_name, method_name = brick.split('.')
|
137
|
-
action_class = Actions.const_get(action_class_name).new(envelope, request)
|
139
|
+
action_class = Actions.const_get(action_class_name).new(envelope, request, @logger)
|
138
140
|
action_class.send method_name
|
139
141
|
action_class
|
140
142
|
end
|
@@ -154,21 +156,9 @@ module Culpa
|
|
154
156
|
end
|
155
157
|
|
156
158
|
##
|
157
|
-
# Rack pre-formatted
|
158
|
-
def
|
159
|
-
[
|
160
|
-
end
|
161
|
-
|
162
|
-
##
|
163
|
-
# Rack pre-formatted 404 : Not found
|
164
|
-
def not_found
|
165
|
-
['404', {'Content-Type' => 'application/json'}, []]
|
166
|
-
end
|
167
|
-
|
168
|
-
##
|
169
|
-
# Rack pre-formatted 404 : Not found
|
170
|
-
def internal_server_error
|
171
|
-
['500', {'Content-Type' => 'application/json'}, []]
|
159
|
+
# Rack pre-formatted error
|
160
|
+
def rack_error(code)
|
161
|
+
[code.to_s, {'Content-Type' => 'application/json'}, []]
|
172
162
|
end
|
173
163
|
|
174
164
|
end
|
data/lib/rspec_helper.rb
CHANGED
@@ -8,13 +8,14 @@ module CulpaHelpers
|
|
8
8
|
def initialize(opts)
|
9
9
|
@action, @method = opts.first
|
10
10
|
@action = @action.to_s.split('_').map{|w| w.capitalize}.join
|
11
|
+
@logger = Logger.new STDOUT
|
11
12
|
opts.delete(@action)
|
12
13
|
@envelope = Envelope.new(opts[:envelope]) if opts.has_key? :envelope
|
13
14
|
@request = EnvelopeRequest.new(opts[:request]) if opts.has_key? :request
|
14
15
|
end
|
15
16
|
|
16
17
|
def call
|
17
|
-
action_class = Actions.const_get(@action).new(@envelope, @request)
|
18
|
+
action_class = Actions.const_get(@action).new(@envelope, @request, @logger)
|
18
19
|
action_class.send @method.to_s
|
19
20
|
return action_class.to_render
|
20
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: culpa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jérémy SEBAN
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -63,7 +63,7 @@ files:
|
|
63
63
|
- lib/action.rb
|
64
64
|
- lib/culpa.rb
|
65
65
|
- lib/envelope.rb
|
66
|
-
- lib/
|
66
|
+
- lib/file_streamer.rb
|
67
67
|
- lib/path_parser.rb
|
68
68
|
- lib/rspec_helper.rb
|
69
69
|
- templates/Dockerfile
|