culpa 0.5.0 → 0.5.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
- NmIyYjkwNWQxYjk0ODc5MzQ5MWIxOTFmMGUzYTYwNThlZWU3ZGI0Ng==
4
+ NDFjODlhNWQyYmVlMGRjYTU4MjM4Mzc4ZjhkZWY2YzI5NGE2ZjYzOQ==
5
5
  data.tar.gz: !binary |-
6
- NGQzM2I2ZGY0NzE5ODg0OTlkYWU5MjM2MzYxOGViMjdjMGE2YTExZQ==
6
+ YThiMTZhNDI0YzNkMDg2MTA2YTE5NTk2MzQzNDVjOTgwNWYxYjNlNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWVlZTE0Y2EyZjgzNzg2YWZmZjBlODdkZTEyZjMwMmFkODA4ZmI1MmNhOGVj
10
- MzkxYjA4NzdlMTc0NTk3MTcyMzdlZGYxZDM0YWFhMWQ1YjY0NTYxYTU2NTQ2
11
- YWRkYTFiZTkwNGQ2MzIyYWM1Y2JmZGFiOWViY2NlMjkzOGI0Mjc=
9
+ NjEzNTUxY2ZjYWE3Yzk5OTE1NTUyMzQxOTk2OTZjZDNlY2VlMGQ4ZTVmMmVh
10
+ MmFlY2Q1MmFiMTAzY2ExZDc3ZjA2ODY2YzMxNmVkZDlhNTg3MDZhZGVkNTc2
11
+ ZjBlMTBlOWQzZDg4N2NlZmJlMjAwODEzNWI0MjI4Yjk3YzEyOTk=
12
12
  data.tar.gz: !binary |-
13
- YjYyY2QxZGViYWE3ZTI5NTA2MDU0OWVkM2NiMjIwNzY1YTM2NjE1ODU1NWEy
14
- MzlmNWQ1ZmMyZDhhNTBjNTJlM2U5MDc5YWIwMWQ4YmUxNzYxNDhhYzYwYTZm
15
- YTg5NmVlYWQ1MzMxNjcxNTMwMWI4OGRjYmMzNmExODAwNzAwM2Q=
13
+ MWE4NDAwYTY3MGQzMWYxMDUxYzgyMDhlYzRkMjZjZTAxZTdhZDQ2ZDk4NTY4
14
+ ZTFhNDI1ZDQ3ZjBlZGZkNjZmYzM2MTVhODhjNDM2NTI3MGQ3NTNjNDUyNzQz
15
+ NzhhZTRmN2NkZDNmYzllNmU2NGQ4ZGRmMWQ1Y2UxOTIwNzg0MWY=
data/lib/action.rb CHANGED
@@ -63,6 +63,14 @@ class Action
63
63
  @to_render = nil
64
64
  end
65
65
 
66
+ def e
67
+ @e
68
+ end
69
+
70
+ def r
71
+ @r
72
+ end
73
+
66
74
  def render(options={})
67
75
  options[:headers] ||= {}
68
76
  if options.has_key? :json
@@ -95,4 +103,3 @@ class Action
95
103
  end
96
104
 
97
105
  end
98
-
data/lib/culpa.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'bundler'
2
2
  require 'json'
3
3
  require 'yaml'
4
+ require 'logger'
4
5
 
5
6
  require 'envelope'
6
7
  require 'path_parser'
@@ -36,42 +37,16 @@ module Culpa
36
37
  class Application
37
38
 
38
39
  def initialize(options = {})
39
- @brick_call_chains_cache = {}
40
40
  @router = YAML.load_file(options[:router] || './config/router.yml')
41
- end
42
-
43
- def call_brickchain(options)
44
- router_method_name = "#{options[:sub_call]}_#{options[:res_name]}"
45
- envelope = Envelope.new
46
- request = EnvelopeRequest.new(options)
47
- bricks = @router[router_method_name]
48
- return not_found unless bricks
49
- bricks.each do |brick|
50
- action_class_name, method_name = brick.split('.')
51
- action_class = Actions.const_get(action_class_name).new(envelope, request)
52
- action_class.send method_name
53
- return do_render action_class.to_render if action_class.to_render
54
- end
55
- raise NoRenderCalled.new
56
- end
57
-
58
- ##
59
- # Renders a json or anything else
60
- def do_render(to_render)
61
- body = case to_render[:format]
62
- when :json
63
- [ to_render[:object].to_json ]
64
- when :file_from_path, :file_from_io
65
- to_render[:object]
66
- when :status
67
- []
68
- end
69
- [ to_render[:status], to_render[:headers], body ]
41
+ @logger = Logger.new(STDOUT)
42
+ @logger.level = ENV['RACK_ENV'] == 'development' ? Logger::DEBUG : Logger::WARN
43
+ @logger.info 'Culpa initialized'
70
44
  end
71
45
 
72
46
  ##
73
47
  # Rack entrypoint
74
48
  def call(env)
49
+ @logger.debug "Request received : #{env['PATH_INFO']}"
75
50
  call_options = {
76
51
  verb: env['REQUEST_METHOD'].downcase.to_sym,
77
52
  params: Rack::Utils.parse_nested_query(env['QUERY_STRING'])
@@ -115,12 +90,44 @@ module Culpa
115
90
  call_options[:id] = route_params[:id]
116
91
  call_options[:sub_call] = route_params[:sub_call]
117
92
  else
93
+ @logger.debug "Refused request, #{env['PATH_INFO']} didn't match enforced routes"
118
94
  return bad_request
119
95
  end
120
96
  # Call the brickchain and return the value to Rack
97
+ @logger.debug "Calling brickchain => subcall: #{call_options[:sub_call]}, action: #{call_options[:res_name]}, id: #{call_options[:id]}"
121
98
  call_brickchain call_options
122
99
  end
123
100
 
101
+ def call_brickchain(options)
102
+ router_method_name = "#{options[:sub_call]}_#{options[:res_name]}"
103
+ envelope = Envelope.new
104
+ request = EnvelopeRequest.new(options)
105
+ bricks = @router[router_method_name]
106
+ return not_found unless bricks
107
+ @logger.info "Brickchain determined for #{router_method_name} :\n#{bricks.join("\n")}\n"
108
+ bricks.each do |brick|
109
+ action_class_name, method_name = brick.split('.')
110
+ action_class = Actions.const_get(action_class_name).new(envelope, request)
111
+ action_class.send method_name
112
+ return do_render action_class.to_render if action_class.to_render
113
+ end
114
+ raise NoRenderCalled.new
115
+ end
116
+
117
+ ##
118
+ # Renders a json or anything else
119
+ def do_render(to_render)
120
+ body = case to_render[:format]
121
+ when :json
122
+ [ to_render[:object].to_json ]
123
+ when :file_from_path, :file_from_io
124
+ to_render[:object]
125
+ when :status
126
+ []
127
+ end
128
+ [ to_render[:status], to_render[:headers], body ]
129
+ end
130
+
124
131
  ##
125
132
  # Rack pre-formatted 400 : Bad request
126
133
  def bad_request
@@ -135,4 +142,4 @@ module Culpa
135
142
 
136
143
  end
137
144
 
138
- end
145
+ end
@@ -0,0 +1,39 @@
1
+ require 'envelope'
2
+ require 'action'
3
+ require 'rspec/expectations'
4
+
5
+ module CulpaHelpers
6
+
7
+ class BrickCallMock
8
+ def initialize(opts)
9
+ @action, @method = opts.first
10
+ @action = @action.to_s.split('_').map{|w| w.capitalize}.join
11
+ opts.delete(@action)
12
+ @envelope = Envelope.new(opts[:envelope]) if opts.has_key? :envelope
13
+ @request = EnvelopeRequest.new(opts[:request]) if opts.has_key? :request
14
+ end
15
+
16
+ def call
17
+ action_class = Actions.const_get(@action).new(@envelope, @request)
18
+ action_class.send @method.to_s
19
+ return action_class.to_render
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ RSpec::Matchers.define :have_status do |expected|
26
+ match do |actual|
27
+ actual[:status] == Action::RETURN_CODES[expected]
28
+ end
29
+ end
30
+
31
+ RSpec::Matchers.define :have_headers do |expected|
32
+ match do |actual|
33
+ expected.each do |k,v|
34
+ return false unless actual[:headers].has_key?(k) and
35
+ actual[:headers][k] == expected[k]
36
+ end
37
+ true
38
+ end
39
+ end
data/templates/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'culpa', '~> 0.3'
3
+ gem 'culpa', '~> 0.5'
4
4
 
5
5
  # Use rspec to test your project.
6
6
  gem 'rspec', '~> 3.4'
data/templates/config.ru CHANGED
@@ -1,6 +1,2 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require 'yaml'
4
1
  require 'culpa'
5
-
6
2
  run Culpa::Application.new
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.5.0
4
+ version: 0.5.2
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-06-20 00:00:00.000000000 Z
11
+ date: 2016-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '1.6'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: '1.6'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +65,7 @@ files:
65
65
  - lib/envelope.rb
66
66
  - lib/file_helpers.rb
67
67
  - lib/path_parser.rb
68
+ - lib/rspec_helper.rb
68
69
  - templates/Dockerfile
69
70
  - templates/Gemfile
70
71
  - templates/config.ru