culpa 0.2.1 → 0.2.2

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzFhNDg4ZjgyOGZkYzVhNTFhNTU0ZmNkMThlYjA5OWRiNzY2ZDZkOA==
4
+ YTNmOTRkMzhlZjlhYTQ5ZmQ0MGNiNjQ0Mjc3MTQyZjhjMDQzNmVlYg==
5
5
  data.tar.gz: !binary |-
6
- YWJjNTlkOTdmN2JiOTA2NTg3MGU4ZmU1YjNmNWU5NjNkZWM5YjM0Nw==
6
+ ZWEwODA0NzhiZTdkYjgyOWZkOGJiMmY3NWYzOGFhZGZmMTlhZDg0MA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTRhYzE3NGM2ZWYxY2MxMDQ4YTNhNTg5NTk4YzliMWUzODdhMDBlNTk4OGY0
10
- ZDBjODkwYmE4ZmZmZjlmODZiNjYxN2QwMzIxZDQzNjY4YWI3NjNkMDA4ZjQ1
11
- MTczYWE2MjhlODU2NTgzMmU4YTAyY2Y0MzBmNTFhNTJjYThhMmU=
9
+ NjZkZmJiMzJjZGYyNjkzODYzMmMyZTI2YmY1OWU5MmZiYzBkNDBjOGYwZDE5
10
+ ZTk0NWQ3MmMzZTVjMGI1OTUwN2QzY2U4YWNhMmJjZTUxOTJhYTY4MDIwMzU5
11
+ MDdkNjAwODhmNzA5NWQ5NzM3YWYyZTQ5NWEyYTIzMzRhYmU4ZjE=
12
12
  data.tar.gz: !binary |-
13
- NjU3ODhiOTI4ZDJmNGY4ZDY4N2NlZWU3ZDZhMzMyYjk0YTMwNGI5ZmQxNjNk
14
- NTc5ZTFkOGE3ZGY3YjA5MDkxNWJhOGY2NTc4ZDY5NDc5Y2JlYTQ5ZTRhODkw
15
- MTM4YzMyMjg3ZDk3YjkwN2IzZDg3NWE2NWM0ZTQ4NDI2ZDI0Mjc=
13
+ ZmEwZDQ3ODAwNmMyZDg2Njc2NzA1ZjI5ZGNhYzdhNmY0YWVkMmI1NjdhYTRh
14
+ ZGU5ZWRiNzU5MWY1MjUxZmE5ZmNiMGM4MTU5NzNjMTJmMjI4YTM1YjBjNWZl
15
+ YjBhNmE4YzNiZDJmYTk1MDhkNTAzMzcxOGFhMzNiYWU5OTE0ODE=
data/bin/culpa CHANGED
@@ -17,6 +17,7 @@ def create_project(project_path)
17
17
  FileUtils.touch "#{project_path}/models/.keep"
18
18
  FileUtils.mkdir "#{project_path}/config"
19
19
  FileUtils.touch "#{project_path}/config/router.yml"
20
+ FileUtils.touch "#{project_path}/config/rev_router.yml"
20
21
  FileUtils.mkdir "#{project_path}/config/initializers"
21
22
  FileUtils.touch "#{project_path}/config/initializers/.keep"
22
23
 
data/lib/culpa.rb CHANGED
@@ -30,17 +30,20 @@ module Culpa
30
30
 
31
31
  class Application
32
32
 
33
- def initialize(router = {})
34
- @router = router
33
+ def initialize(options = {})
34
+ @brick_call_chains_cache = {}
35
+ @router = YAML.load_file(options[:router] || './config/router.yml')
36
+ @rev_router = YAML.load_file(options[:rev_router] || './config/rev_router.yml')
35
37
  end
36
38
 
37
39
  def call_action(options)
38
40
  router_method_name = "#{options[:sub_call]}_#{options[:res_name]}"
39
41
  envelope = Envelope.new
40
42
  request = EnvelopeRequest.new(options)
41
- return not_found unless @router.has_key? router_method_name
42
- @router[router_method_name].each do |method|
43
- action_class_name, method_name = method.split('.')
43
+ bricks = brick_call_chain(router_method_name)
44
+ return not_found unless bricks
45
+ bricks.each do |brick|
46
+ action_class_name, method_name = brick.split('.')
44
47
  action_class = Actions.const_get(action_class_name).new(envelope, request)
45
48
  action_class.send method_name
46
49
  return do_render action_class.to_render if action_class.to_render
@@ -48,7 +51,42 @@ module Culpa
48
51
  raise NoRenderCalled.new
49
52
  end
50
53
 
51
- # Render a json or anything else
54
+ ##
55
+ # Building the brick chain for a router method.
56
+ # Gets before and after bricks in reverse router and merge it with standard calls.
57
+ # Keeps the brickchain in cache for later calls.
58
+ def brick_call_chain(router_method_name)
59
+ unless @brick_call_chains_cache.has_key? router_method_name
60
+ return unless @router.has_key? router_method_name
61
+ final_call_chain = []
62
+ # Search for rev_router matches
63
+ @rev_router.each do |rev_router_item|
64
+ if rev_router_item.has_key? 'match_only'
65
+ rev_router_item['match_only'].each do |matcher|
66
+ if router_method_name.match(Regexp.new("^#{matcher}$"))
67
+ final_call_chain.concat rev_router_item['bricks']
68
+ break
69
+ end
70
+ end
71
+ elsif rev_router_item.has_key? 'all_except'
72
+ one_match = false
73
+ rev_router_item['all_except'].each do |matcher|
74
+ one_match ||= router_method_name.match(Regexp.new("^#{matcher}$"))
75
+ break if one_match
76
+ end
77
+ final_call_chain.concat rev_router_item['bricks'] unless one_match
78
+ end
79
+ end
80
+ # Append router call chain
81
+ final_call_chain.concat @router[router_method_name]
82
+ # Save in cache
83
+ @brick_call_chains_cache[router_method_name] = final_call_chain
84
+ end
85
+ @brick_call_chains_cache[router_method_name]
86
+ end
87
+
88
+ ##
89
+ # Renders a json or anything else
52
90
  def do_render(to_render)
53
91
  body = case to_render[:format]
54
92
  when :json
@@ -61,7 +99,8 @@ module Culpa
61
99
  [ to_render[:status], to_render[:headers], body ]
62
100
  end
63
101
 
64
- # Called by rack
102
+ ##
103
+ # Rack entrypoint
65
104
  def call(env)
66
105
  call_options = {
67
106
  verb: env['REQUEST_METHOD'].downcase.to_sym
@@ -108,12 +147,14 @@ module Culpa
108
147
  call_action call_options
109
148
  end
110
149
 
111
- # 400 : Bad request
150
+ ##
151
+ # Rack pre-formatted 400 : Bad request
112
152
  def bad_request
113
153
  ['400', {'Content-Type' => 'application/json'}, []]
114
154
  end
115
155
 
116
- # 404 : Not found
156
+ ##
157
+ # Rack pre-formatted 404 : Not found
117
158
  def not_found
118
159
  ['404', {'Content-Type' => 'application/json'}, []]
119
160
  end
data/templates/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM ruby:2.3.1
1
+ FROM ruby:2.3-alpine
2
2
  MAINTAINER Your Name <your@email.com>
3
3
 
4
4
  # Create app folder
data/templates/config.ru CHANGED
@@ -3,4 +3,4 @@ require 'bundler/setup'
3
3
  require 'yaml'
4
4
  require 'culpa'
5
5
 
6
- run Culpa::Application.new(YAML.load_file('./config/router.yml'))
6
+ run Culpa::Application.new
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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérémy SEBAN