rack-app 5.7.0 → 5.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7171c075e5153081bdf87f6f73cb693e3f79258d
4
- data.tar.gz: 127afc8743a292af00e7d7a3ce21dedaf8bc78b3
3
+ metadata.gz: f508a985161b52acd4c9670809dd2e67e58e0651
4
+ data.tar.gz: 996142d38f74b27f7ae120711f7e46006801828f
5
5
  SHA512:
6
- metadata.gz: 0354dbeda51211982538fca1c621332c3b96519c6f64428ce00addbbae3f1cb89e14d36ad2c4303fafd9b162a0ee9574a45839a3e5cbf8d5a40682f9ba326a8f
7
- data.tar.gz: 0ce8d133bb66095d89728247937258b7ffe6fe5ebe605799fa9e016600fad65bf1e883ba01dac5476255f97a1f5fbfb8e106bdc06d7c2fb0ba2451327f943c6d
6
+ metadata.gz: 4ece40a6832073132ddcbb0d103615f189247899cd361761da3955af816ed61017bd9aca772d853e0fe0f76609b0ef77bdd86e3883c78747c5687e34bfcc5084
7
+ data.tar.gz: d1ba5d8943f0404cb48ed44cc2c58e06a740e79bc353d9d90b06de5609e4b4f8a6617d3e220879904be83932dd4ac5dc73dfaa8c77a72163a273c4f252a07053
data/README.md CHANGED
@@ -199,14 +199,14 @@ describe App do
199
199
 
200
200
  it { expect(subject.status).to eq 200 }
201
201
 
202
- it { expect(subject.body.join).to eq "Hello World!" }
202
+ it { expect(subject.body).to eq "Hello World!" }
203
203
  end
204
204
 
205
205
  describe '/users/:user_id' do
206
206
  # restful endpoint example
207
207
  subject{ get(url: '/users/1234') }
208
208
 
209
- it { expect(subject.body.join).to eq 'hello 1234!'}
209
+ it { expect(subject.body).to eq 'hello 1234!'}
210
210
 
211
211
  it { expect(subject.status).to eq 201 }
212
212
 
@@ -216,7 +216,7 @@ describe App do
216
216
  # error handled example
217
217
  subject{ get(url: '/make_error') }
218
218
 
219
- it { expect(subject.body.join).to eq '{:error=>"error block rescued"}' }
219
+ it { expect(subject.body).to eq '{:error=>"error block rescued"}' }
220
220
  end
221
221
 
222
222
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.7.0
1
+ 5.8.0
@@ -10,6 +10,7 @@ class Rack::App
10
10
  require 'rack/app/cli'
11
11
  require 'rack/app/test'
12
12
  require 'rack/app/utils'
13
+ require 'rack/app/logger'
13
14
  require 'rack/app/params'
14
15
  require 'rack/app/payload'
15
16
  require 'rack/app/router'
@@ -3,51 +3,10 @@ Rack::App::Extension.register(:logger) do
3
3
  require "logger"
4
4
  require "securerandom"
5
5
 
6
- logger_class = Class.new
7
- logger_class.class_eval do
8
-
9
- def initialize(request_id)
10
- @request_id = request_id
11
- @logger = ::Logger.new(STDOUT)
12
- end
13
-
14
- [:info, :warn, :error, :fatal, :unknown].each do |log_level|
15
- define_method(log_level) do |msg|
16
- if msg.is_a?(Hash)
17
- @logger.__send__(log_level, @request_id){format_hash(msg)}
18
- else
19
- @logger.__send__(log_level, @request_id){String(msg)}
20
- end
21
- end
22
- end
23
-
24
- protected
25
-
26
- begin
27
- require "json"
28
- rescue LoadError
29
- end
30
-
31
- def format_hash(hash)
32
- if defined?(::JSON)
33
- JSON.dump(hash)
34
- else
35
- hash.inspect
36
- end
37
- end
38
-
39
- def method_missing(name,*args)
40
- if @logger.respond_to?(:binding)
41
- @logger.__send__(name,*args)
42
- else
43
- super
44
- end
45
- end
46
-
47
- end
48
-
49
6
  define_method(:logger) do
50
- @logger ||= logger_class.new(request.env['HTTP_X_REQUEST_ID'] || SecureRandom.hex)
7
+ @logger ||= Rack::App::Logger.new(STDOUT).tap do |this|
8
+ this.id = request.env['HTTP_X_REQUEST_ID']
9
+ end
51
10
  end
52
11
 
53
12
  end
@@ -7,7 +7,6 @@ class Rack::App::Endpoint::Builder
7
7
 
8
8
  def build
9
9
  builder = Rack::Builder.new
10
- apply_core_middlewares(builder)
11
10
  apply_middleware_build_blocks(builder)
12
11
  builder.run(Rack::App::Endpoint::Executor.new(@config))
13
12
  builder.to_app
@@ -15,10 +14,6 @@ class Rack::App::Endpoint::Builder
15
14
 
16
15
  protected
17
16
 
18
- def apply_core_middlewares(builder)
19
- builder.use(Rack::App::Middlewares::Configuration::PayloadParserSetter)
20
- end
21
-
22
17
  def apply_middleware_build_blocks(builder)
23
18
  builder_blocks.each do |builder_block|
24
19
  builder_block.call(builder)
@@ -37,7 +32,7 @@ class Rack::App::Endpoint::Builder
37
32
  end
38
33
 
39
34
  def builder_blocks
40
- @config.app_class.middlewares + @config.middleware_builders_blocks
35
+ [@config.app_class.middlewares, @config.middleware_builders_blocks].flatten
41
36
  end
42
37
 
43
38
  end
@@ -8,6 +8,10 @@ class Rack::App::Endpoint::Config
8
8
  return @raw
9
9
  end
10
10
 
11
+ def payload_builder
12
+ @raw[:payload].parser_builder
13
+ end
14
+
11
15
  def application
12
16
  @raw[:application]
13
17
  end
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+ class Rack::App::Logger < ::Logger
3
+ attr_writer :id
4
+
5
+ def initialize(*args)
6
+ super
7
+ configure!
8
+ end
9
+
10
+ protected
11
+
12
+ def id
13
+ @id ||= SecureRandom.hex
14
+ end
15
+
16
+ def configure!
17
+ original_formatter = Logger::Formatter.new
18
+ self.formatter = proc do |severity, datetime, progname, msg|
19
+ fmsg = msg.is_a?(::Hash) ? (defined?(::JSON) ? JSON.dump(msg) : msg) : msg
20
+ fprname = progname || id
21
+ original_formatter.call(severity, datetime, fprname, fmsg)
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,4 @@
1
1
  module Rack::App::Middlewares
2
- require 'rack/app/middlewares/payload_parser_setter'
3
2
  require 'rack/app/middlewares/path_info_cutter'
4
3
  require 'rack/app/middlewares/configuration'
5
4
  require 'rack/app/middlewares/header_setter'
@@ -3,33 +3,37 @@ require "rack/request"
3
3
  require "rack/response"
4
4
  class Rack::App::Middlewares::Configuration
5
5
 
6
- require "rack/app/middlewares/configuration/handler_setter"
7
- require "rack/app/middlewares/configuration/serializer_setter"
8
- require "rack/app/middlewares/configuration/payload_parser_setter"
9
-
10
6
  require "rack/app/middlewares/configuration/path_params_matcher"
11
7
 
12
8
  def initialize(app, config)
13
- @stack = build_stack(app) do |builder|
14
- builder.use Rack::App::Middlewares::Configuration::SerializerSetter,
15
- config.serializer
16
-
17
- builder.use Rack::App::Middlewares::Configuration::HandlerSetter,
18
- config.app_class
19
-
20
- end
9
+ @app = app || raise
10
+ @serializer = config.serializer || raise
11
+ @handler_class = config.app_class || raise
12
+ @payload_parser = config.payload_builder.to_parser || raise
21
13
  end
22
14
 
23
15
  def call(env)
24
- @stack.call(env)
16
+ env[Rack::App::Constants::ENV::REQUEST_HANDLER]= handler(env)
17
+ env[::Rack::App::Constants::ENV::SERIALIZER]= @serializer
18
+ env[::Rack::App::Constants::ENV::PAYLOAD_PARSER]= @payload_parser
19
+ env[::Rack::App::Constants::ENV::PAYLOAD_GETTER]= lambda do
20
+ env[::Rack::App::Constants::ENV::PARSED_PAYLOAD] ||= env[::Rack::App::Constants::ENV::PAYLOAD_PARSER].parse_env(env)
21
+ end
22
+ @app.call(env)
25
23
  end
26
24
 
27
25
  protected
28
26
 
29
- def build_stack(app)
30
- builder = Rack::Builder.new
31
- yield(builder)
32
- builder.run(app)
33
- return builder.to_app
27
+ def handler(env)
28
+ new_handler = @handler_class.new
29
+ new_handler.request = ::Rack::Request.new(env)
30
+ new_handler.response = ::Rack::Response.new
31
+ new_handler
34
32
  end
33
+
34
+ def extname(env)
35
+ path_info = env[::Rack::App::Constants::ENV::PATH_INFO]
36
+ File.extname(path_info.split("/").last.to_s)
37
+ end
38
+
35
39
  end
@@ -1,9 +1,12 @@
1
1
  class Rack::App::Payload::Builder
2
-
3
- def parser(&block)
2
+
3
+ def parser_builder(&block)
4
4
  @parser_builder ||= Rack::App::Payload::Parser::Builder.new
5
5
  @parser_builder.instance_exec(&block) if block
6
6
  @parser_builder
7
7
  end
8
8
 
9
+ alias parser parser_builder
10
+ alias configure_parser parser_builder
11
+
9
12
  end
@@ -7,12 +7,11 @@ class Rack::App::Payload::Parser
7
7
 
8
8
  def initialize(content_type__parsers = {})
9
9
  raise unless content_type__parsers.is_a?(Hash)
10
- @content_type__parsers = Hash.new(DEFAULT_PARSER)
11
- @content_type__parsers.merge!(content_type__parsers)
10
+ @content_type__parsers = content_type__parsers
12
11
  end
13
12
 
14
13
  def parse_io(content_type, io)
15
- @content_type__parsers[content_type.to_s].call(io)
14
+ parser_for(content_type.to_s).call(io)
16
15
  end
17
16
 
18
17
  def parse_env(env)
@@ -23,4 +22,11 @@ class Rack::App::Payload::Parser
23
22
  def parse_string(content_type, str)
24
23
  parse_io(content_type, StringIO.new(str))
25
24
  end
25
+
26
+ protected
27
+
28
+ def parser_for(content_type)
29
+ @content_type__parsers[content_type] || DEFAULT_PARSER
30
+ end
31
+
26
32
  end
@@ -3,15 +3,15 @@ class Rack::App::Payload::Parser::Builder
3
3
  require "rack/app/payload/parser/builder/formats"
4
4
 
5
5
  def initialize
6
- @parsers = {}
6
+ @content_type__parsers = Hash.new(Rack::App::Payload::Parser::DEFAULT_PARSER)
7
7
  end
8
8
 
9
9
  def to_parser
10
- Rack::App::Payload::Parser.new(@parsers)
10
+ Rack::App::Payload::Parser.new(@content_type__parsers.dup)
11
11
  end
12
12
 
13
13
  def on(content_type, &parser)
14
- @parsers[content_type]= parser
14
+ @content_type__parsers[content_type]= parser
15
15
  self
16
16
  end
17
17
 
@@ -19,20 +19,24 @@ class Rack::App::Payload::Parser::Builder
19
19
  Rack::App::Payload::Parser::Builder::Formats.accept(self, *formats)
20
20
  end
21
21
 
22
- # def reject_unsupported_media_types
23
- # reject = proc do
24
- # rr = Rack::Response.new
25
- # rr.status = 415
26
- # rr.write("Unsupported Media Type")
27
- # throw(:rack_response, rr)
28
- # end
29
- # @parsers = Hash.new(reject).merge(@parsers)
30
- # nil
31
- # end
22
+ def reject_unsupported_media_types
23
+ reject = proc do |io|
24
+ rr = Rack::Response.new
25
+ rr.status = 415
26
+ rr.write("Unsupported Media Type")
27
+ rr.write("Accepted content-types:")
28
+ @content_type__parsers.each do |content_type, _|
29
+ rr.write(content_type.to_s)
30
+ end
31
+ throw(:rack_response, rr)
32
+ end
33
+ @content_type__parsers = Hash.new(reject).merge(@content_type__parsers)
34
+ nil
35
+ end
32
36
 
33
37
  def merge!(parser_builder)
34
38
  raise unless parser_builder.is_a?(self.class)
35
- @parsers.merge!(parser_builder.instance_variable_get(:@parsers))
39
+ @content_type__parsers.merge!(parser_builder.instance_variable_get(:@content_type__parsers))
36
40
  self
37
41
  end
38
42
 
@@ -1,12 +1,9 @@
1
1
  module Rack::App::SingletonMethods::Payload
2
2
 
3
3
  def payload(&block)
4
- unless @payload_builder
5
- @payload_builder = Rack::App::Payload::Builder.new
6
- use(Rack::App::Middlewares::Payload::ParserSetter, @payload_builder)
7
- end
4
+ @payload_builder ||= Rack::App::Payload::Builder.new
8
5
  @payload_builder.instance_exec(&block) if block
9
- nil
6
+ @payload_builder
10
7
  end
11
8
 
12
9
  end
@@ -25,6 +25,7 @@ module Rack::App::SingletonMethods::RouteHandling
25
25
  router.register_endpoint!(
26
26
  Rack::App::Endpoint.new({
27
27
  :app_class => self,
28
+ :payload => payload,
28
29
  :error_handler => error,
29
30
  :user_defined_logic => block,
30
31
  :request_method => request_method,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.7.0
4
+ version: 5.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-11 00:00:00.000000000 Z
11
+ date: 2016-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -121,12 +121,10 @@ files:
121
121
  - lib/rack/app/instance_methods/redirect_to.rb
122
122
  - lib/rack/app/instance_methods/serve_file.rb
123
123
  - lib/rack/app/instance_methods/streaming.rb
124
+ - lib/rack/app/logger.rb
124
125
  - lib/rack/app/middlewares.rb
125
126
  - lib/rack/app/middlewares/configuration.rb
126
- - lib/rack/app/middlewares/configuration/handler_setter.rb
127
127
  - lib/rack/app/middlewares/configuration/path_params_matcher.rb
128
- - lib/rack/app/middlewares/configuration/payload_parser_setter.rb
129
- - lib/rack/app/middlewares/configuration/serializer_setter.rb
130
128
  - lib/rack/app/middlewares/header_setter.rb
131
129
  - lib/rack/app/middlewares/hooks.rb
132
130
  - lib/rack/app/middlewares/hooks/after.rb
@@ -141,7 +139,6 @@ files:
141
139
  - lib/rack/app/middlewares/path_info_cutter.rb
142
140
  - lib/rack/app/middlewares/payload.rb
143
141
  - lib/rack/app/middlewares/payload/parser_setter.rb
144
- - lib/rack/app/middlewares/payload_parser_setter.rb
145
142
  - lib/rack/app/params.rb
146
143
  - lib/rack/app/payload.rb
147
144
  - lib/rack/app/payload/builder.rb
@@ -1,22 +0,0 @@
1
- class Rack::App::Middlewares::Configuration::HandlerSetter
2
-
3
- def initialize(app, handler_class)
4
- @app = app
5
- @handler_class = handler_class || raise
6
- end
7
-
8
- def call(env)
9
- env[Rack::App::Constants::ENV::REQUEST_HANDLER]= handler(env)
10
- @app.call(env)
11
- end
12
-
13
- protected
14
-
15
- def handler(env)
16
- new_handler = @handler_class.new
17
- new_handler.request = ::Rack::Request.new(env)
18
- new_handler.response = ::Rack::Response.new
19
- new_handler
20
- end
21
-
22
- end
@@ -1,18 +0,0 @@
1
- class Rack::App::Middlewares::Configuration::PayloadParserSetter
2
-
3
- PARSER = ::Rack::App::Constants::ENV::PAYLOAD_PARSER
4
- PARSED = ::Rack::App::Constants::ENV::PARSED_PAYLOAD
5
- GETTER = ::Rack::App::Constants::ENV::PAYLOAD_GETTER
6
-
7
- def initialize(app)
8
- @app = app
9
- @parser = Rack::App::Payload::Parser.new
10
- end
11
-
12
- def call(env)
13
- env[PARSER]= @parser
14
- env[GETTER]= lambda { env[PARSED] ||= env[PARSER].parse_env(env) }
15
- @app.call(env)
16
- end
17
-
18
- end
@@ -1,21 +0,0 @@
1
- class Rack::App::Middlewares::Configuration::SerializerSetter
2
-
3
- def initialize(app, serializer)
4
- @app = app
5
- @serializer = serializer || raise
6
- end
7
-
8
- def call(env)
9
- # env[::Rack::App::Constants::ENV::EXTNAME] ||= extname(env)
10
- env[::Rack::App::Constants::ENV::SERIALIZER]= @serializer
11
- @app.call(env)
12
- end
13
-
14
- protected
15
-
16
- def extname(env)
17
- path_info = env[::Rack::App::Constants::ENV::PATH_INFO]
18
- File.extname(path_info.split("/").last.to_s)
19
- end
20
-
21
- end
@@ -1,18 +0,0 @@
1
- class Rack::App::Middlewares::PayloadParserSetter
2
-
3
- PARSER = ::Rack::App::Constants::ENV::PAYLOAD_PARSER
4
- PARSED = ::Rack::App::Constants::ENV::PARSED_PAYLOAD
5
- GETTER = ::Rack::App::Constants::ENV::PAYLOAD_GETTER
6
-
7
- def initialize(app, payload_parser)
8
- @payload_parser = payload_parser
9
- @app = app
10
- end
11
-
12
- def call(env)
13
- env[PARSER]= @payload_parser
14
- env[GETTER]= lambda { env[PARSED] ||= env[PARSER].parse_env(env) }
15
- @app.call(env)
16
- end
17
-
18
- end