rack-app 4.0.1 → 5.0.0.rc1

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: 6cf38306bf272547c7a780a6a6e92f7138d8ebe5
4
- data.tar.gz: 05929c52596d77e1caca9c46895ade3aa6a5b36d
3
+ metadata.gz: 401d8f6ab979fa3f44891018a61dcf640d45b488
4
+ data.tar.gz: 5d02724b737e668072d4ca5972d76d043cf854a5
5
5
  SHA512:
6
- metadata.gz: acfe46345c8ac900aee026f9249f053e17252224354afc803732b0f3b878761c305a6dbc0e2c29054052e45ae004662d9380b82851559446dd7bbf74991752e5
7
- data.tar.gz: b4f5c6d7ce3e35cc34b16626641ae10dabd369adca3606f48098f6aaaf9f1ed50f687ed75c2a3cb35e56346d43607d859a020d0fd8294fc950617ff053438f7a
6
+ metadata.gz: 010fcc827baf6f48e2ed0882a8d0b17052afc2dd11ae20659b722b91720f06a9bf161e4560d804b9f83b3e7c58c803cab8e1765719b6a312e45fd6479f7efcba
7
+ data.tar.gz: ab3f8dec6d35b7c3ec9570d9af48a10d2ab9ff2e027755bd9dedded3648017ec5d5dd359d7becf3f4cd70688744b3164496328b190749fdfca91cd710956e13c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.0.1
1
+ 5.0.0.rc1
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.join(File.dirname(File.dirname(__FILE__)),'lib'))
3
+ require 'rack/app'
4
+
5
+ require "irb"
6
+ IRB.start
File without changes
@@ -18,6 +18,7 @@ class Rack::App
18
18
  require 'rack/app/middlewares'
19
19
  require 'rack/app/file_server'
20
20
  require 'rack/app/error_handler'
21
+ require 'rack/app/bundled_extensions'
21
22
  require 'rack/app/endpoint/not_found'
22
23
  require 'rack/app/request_configurator'
23
24
 
@@ -27,4 +28,4 @@ class Rack::App
27
28
  require 'rack/app/instance_methods'
28
29
  include Rack::App::InstanceMethods
29
30
 
30
- end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Rack::App::BundledExtensions
2
+ require "rack/app/bundled_extensions/logger"
3
+ end
@@ -0,0 +1,53 @@
1
+ Rack::App::Extension.register(:logger) do
2
+
3
+ require "logger"
4
+ require "securerandom"
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
+ define_method(:logger) do
50
+ @logger ||= logger_class.new(request.env['HTTP_X_REQUEST_ID'] || SecureRandom.hex)
51
+ end
52
+
53
+ end
@@ -11,49 +11,54 @@ class Rack::App::Endpoint
11
11
  @error_handler = properties[:error_handler] || Rack::App::ErrorHandler.new
12
12
  @serializer = properties[:serializer] || Rack::App::Serializer.new
13
13
 
14
- @middleware = (properties[:middleware] || Rack::Builder.new).dup
15
- @middleware.run(lambda { |env| self.execute(env) })
14
+ middleware = (properties[:middleware] || Rack::Builder.new).dup
15
+ middleware.run(lambda { |env| self.call_without_middlewares(env) })
16
16
  @endpoint_method_name = register_method_to_app_class(properties[:user_defined_logic])
17
17
 
18
- @app = @middleware.to_app
18
+ @app = middleware.to_app
19
19
  end
20
20
 
21
21
  def call(env)
22
22
  @app.call(env)
23
23
  end
24
24
 
25
- def execute(request_env)
26
-
27
- request = Rack::Request.new(request_env)
25
+ def call_without_middlewares(env)
26
+ request = Rack::Request.new(env)
28
27
  response = Rack::Response.new
29
-
30
- set_response_body(response, get_response_body(request, response))
31
- return response.finish
32
-
28
+ return catch(:rack_response){ execute(request, response) }.finish
33
29
  end
34
30
 
35
- def get_response_body(rack_request, rack_response)
31
+ protected
36
32
 
33
+ def execute(request,response)
37
34
  request_handler = @app_class.new
38
- request_handler.request = rack_request
39
- request_handler.response = rack_response
40
-
41
- return @error_handler.execute_with_error_handling_for(request_handler, @endpoint_method_name)
42
-
35
+ request_handler.request = request
36
+ request_handler.response = response
37
+ set_response_body(response, get_response_body(request_handler))
38
+ return response
43
39
  end
44
40
 
45
- protected
41
+ def get_response_body(request_handler)
42
+ catch :response_body do
43
+ evaluated_value = evaluate_value(request_handler)
44
+
45
+ evaluated_value
46
+ end
47
+ end
46
48
 
47
- def set_response_body(response, result)
48
- return nil unless response.body.is_a?(Array)
49
+ def set_response_body(response, response_body)
50
+ response.write(String(@serializer.serialize(response_body)))
51
+ end
49
52
 
50
- response.write(String(@serializer.serialize(result)))
53
+ def evaluate_value(request_handler)
54
+ @error_handler.execute_with_error_handling_for(request_handler) do
55
+ request_handler.__send__(@endpoint_method_name)
56
+ end
51
57
  end
52
58
 
53
59
  def register_method_to_app_class(proc)
54
- method_name = ::Rack::App::Utils.uuid
60
+ method_name = '__' + ::Rack::App::Utils.uuid
55
61
  @app_class.__send__(:define_method, method_name, &proc)
56
62
  return method_name
57
63
  end
58
-
59
64
  end
@@ -13,8 +13,8 @@ class Rack::App::ErrorHandler
13
13
  nil
14
14
  end
15
15
 
16
- def execute_with_error_handling_for(instance, endpoint_method_name)
17
- instance.__send__(endpoint_method_name)
16
+ def execute_with_error_handling_for(instance, &block)
17
+ block.call
18
18
  rescue *[Exception, @handlers.keys].flatten => ex
19
19
  instance.instance_exec(ex, &get_handler(ex))
20
20
  end
@@ -34,4 +34,4 @@ class Rack::App::ErrorHandler
34
34
  return handler.nil? ? nil : handler.last
35
35
  end
36
36
 
37
- end
37
+ end
@@ -3,11 +3,11 @@ module Rack::App::InstanceMethods
3
3
  require 'rack/app/instance_methods/core'
4
4
  require 'rack/app/instance_methods/http_control'
5
5
  require 'rack/app/instance_methods/payload'
6
- require 'rack/app/instance_methods/upload'
6
+ require 'rack/app/instance_methods/serve_file'
7
7
 
8
8
  include Rack::App::InstanceMethods::Core
9
9
  include Rack::App::InstanceMethods::HttpControl
10
10
  include Rack::App::InstanceMethods::Payload
11
- include Rack::App::InstanceMethods::Upload
11
+ include Rack::App::InstanceMethods::ServeFile
12
12
 
13
13
  end
@@ -18,4 +18,8 @@ module Rack::App::InstanceMethods::Core
18
18
  @response || raise("response object is not set for #{self.class}")
19
19
  end
20
20
 
21
+ def finish_response
22
+ throw(:rack_response, response)
23
+ end
24
+
21
25
  end
@@ -1,11 +1,19 @@
1
1
  module Rack::App::InstanceMethods::HttpControl
2
2
 
3
- def redirect_to(url)
4
- url = "#{url}?#{request.env['QUERY_STRING']}" unless request.env['QUERY_STRING'].empty?
3
+ def redirect_to(url, params={})
4
+
5
+ if params.empty?
6
+ url = [url, request.env['QUERY_STRING']].join('?') unless request.env['QUERY_STRING'].empty?
7
+ else
8
+ query_string = Rack::App::Utils.encode_www_form(params.to_a)
9
+ url = [url, query_string].join('?')
10
+ end
11
+
5
12
  response.status = 301
6
13
  response.headers.merge!({'Location' => url})
7
- 'See Ya!'
14
+
15
+ finish_response
16
+
8
17
  end
9
18
 
10
19
  end
11
-
@@ -1,12 +1,11 @@
1
- module Rack::App::InstanceMethods::Upload
1
+ module Rack::App::InstanceMethods::ServeFile
2
2
 
3
3
  def serve_file(file_path)
4
4
  raw_rack_resp = Rack::App::FileServer.serve_file(request.env, file_path)
5
5
  response.status = raw_rack_resp[0]
6
6
  response.headers.merge!(raw_rack_resp[1])
7
7
  response.body = raw_rack_resp[2]
8
- return nil
8
+ finish_response
9
9
  end
10
10
 
11
11
  end
12
-
@@ -2,6 +2,7 @@ module Rack::App::SingletonMethods
2
2
 
3
3
  require 'rack/app/singleton_methods/http_methods'
4
4
  require 'rack/app/singleton_methods/inheritance'
5
+ require 'rack/app/singleton_methods/middleware'
5
6
  require 'rack/app/singleton_methods/mounting'
6
7
  require 'rack/app/singleton_methods/params_validator'
7
8
  require 'rack/app/singleton_methods/rack_interface'
@@ -11,6 +12,7 @@ module Rack::App::SingletonMethods
11
12
 
12
13
  include Rack::App::SingletonMethods::HttpMethods
13
14
  include Rack::App::SingletonMethods::Inheritance
15
+ include Rack::App::SingletonMethods::Middleware
14
16
  include Rack::App::SingletonMethods::Mounting
15
17
  include Rack::App::SingletonMethods::ParamsValidator
16
18
  include Rack::App::SingletonMethods::RackInterface
@@ -10,9 +10,11 @@ module Rack::App::SingletonMethods::Extensions
10
10
  Rack::App::Extension.apply_extensions(self,applied_extensions,extension_names)
11
11
  end
12
12
 
13
+ alias apply_extension apply_extensions
14
+
13
15
  def extensions(*extensions_names)
14
16
  apply_extensions(*extensions_names)
15
17
  applied_extensions
16
18
  end
17
19
 
18
- end
20
+ end
@@ -0,0 +1,23 @@
1
+ module Rack::App::SingletonMethods::Middleware
2
+
3
+ protected
4
+
5
+ def middlewares(&block)
6
+ @middlewares ||= []
7
+ @middlewares << block unless block.nil?
8
+ @middlewares
9
+ end
10
+
11
+ alias middleware middlewares
12
+
13
+ def use(*args)
14
+ middlewares{ |b| b.use(*args) }
15
+ end
16
+
17
+ def only_next_endpoint_middlewares(&block)
18
+ @only_next_endpoint_middlewares ||= []
19
+ @only_next_endpoint_middlewares << block unless block.nil?
20
+ @only_next_endpoint_middlewares
21
+ end
22
+
23
+ end
@@ -35,18 +35,4 @@ module Rack::App::SingletonMethods::Settings
35
35
  return @error_handler
36
36
  end
37
37
 
38
- def middlewares(&block)
39
- @middlewares ||= []
40
- @middlewares << block unless block.nil?
41
- @middlewares
42
- end
43
-
44
- alias middleware middlewares
45
-
46
- def only_next_endpoint_middlewares(&block)
47
- @only_next_endpoint_middlewares ||= []
48
- @only_next_endpoint_middlewares << block unless block.nil?
49
- @only_next_endpoint_middlewares
50
- end
51
-
52
38
  end
@@ -1,4 +1,3 @@
1
- require "cgi"
2
1
  module Rack::App::Test::Utils
3
2
 
4
3
  extend self
@@ -23,7 +22,7 @@ module Rack::App::Test::Utils
23
22
  env = properties[:headers].reduce({}) { |m, (k, v)| m.merge("HTTP_#{k.to_s.gsub('-', '_').upcase}" => v.to_s) }
24
23
  payload = properties.delete(:payload)
25
24
  env["rack.input"]= ::Rack::Lint::InputWrapper.new(string_io_for(payload))
26
- env[::Rack::QUERY_STRING]= encode_www_form(properties[:params].to_a)
25
+ env[::Rack::QUERY_STRING]= Rack::App::Utils.encode_www_form(properties[:params].to_a)
27
26
  env.merge!(properties[:env] || {})
28
27
 
29
28
  env
@@ -44,28 +43,4 @@ module Rack::App::Test::Utils
44
43
  end
45
44
  end
46
45
 
47
- def encode_www_form(enum)
48
- enum.map do |k, v|
49
- if v.nil?
50
- encode_www_form_component(k)
51
- elsif v.respond_to?(:to_ary)
52
- v.to_ary.map do |w|
53
- str = encode_www_form_component(k)
54
- unless w.nil?
55
- str << '='
56
- str << encode_www_form_component(w)
57
- end
58
- end.join('&')
59
- else
60
- str = encode_www_form_component(k)
61
- str << '='
62
- str << encode_www_form_component(v)
63
- end
64
- end.join('&')
65
- end
66
-
67
- def encode_www_form_component(str)
68
- CGI.escape(str.to_s)
69
- end
70
-
71
46
  end
@@ -1,4 +1,5 @@
1
- require 'securerandom'
1
+ require "cgi"
2
+ require "securerandom"
2
3
  module Rack::App::Utils
3
4
  extend self
4
5
 
@@ -104,4 +105,28 @@ module Rack::App::Utils
104
105
  RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null'
105
106
  end
106
107
 
108
+ def encode_www_form(enum)
109
+ enum.map do |k, v|
110
+ if v.nil?
111
+ encode_www_form_component(k)
112
+ elsif v.respond_to?(:to_ary)
113
+ v.to_ary.map do |w|
114
+ str = encode_www_form_component(k)
115
+ unless w.nil?
116
+ str << '='
117
+ str << encode_www_form_component(w)
118
+ end
119
+ end.join('&')
120
+ else
121
+ str = encode_www_form_component(k)
122
+ str << '='
123
+ str << encode_www_form_component(v)
124
+ end
125
+ end.join('&')
126
+ end
127
+
128
+ def encode_www_form_component(str)
129
+ CGI.escape(str.to_s)
130
+ end
131
+
107
132
  end
@@ -0,0 +1,33 @@
1
+ require 'benchmark'
2
+
3
+ class TestSubject
4
+ def test
5
+ 'OK'
6
+ end
7
+ end
8
+
9
+ block = proc do
10
+ test
11
+ end
12
+
13
+ TestSubject.__send__(:define_method, :defined_method,&block)
14
+
15
+ TEST_AMOUNT = 1_000_000
16
+
17
+ Benchmark.bm(15) do |x|
18
+
19
+ x.report('method call') do
20
+ TEST_AMOUNT.times do
21
+ test_subject = TestSubject.new
22
+ test_subject.__send__(:defined_method)
23
+ end
24
+ end
25
+
26
+ x.report('instance_exec') do
27
+ TEST_AMOUNT.times do
28
+ test_subject = TestSubject.new
29
+ test_subject.instance_exec(&block)
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,45 @@
1
+ require 'benchmark'
2
+
3
+ class TestSubject
4
+
5
+ def tvalue
6
+ catch :value do
7
+ tget
8
+ end
9
+ end
10
+
11
+ def rvalue
12
+ catch :value do
13
+ rget
14
+ end
15
+ end
16
+
17
+ def tget
18
+ throw :value, 'hello'
19
+ end
20
+
21
+ def rget
22
+ return 'hello'
23
+ end
24
+
25
+ end
26
+
27
+ TEST_AMOUNT = 1_000_000
28
+
29
+ Benchmark.bm(15) do |x|
30
+
31
+ x.report('throw') do
32
+ test_subject = TestSubject.new
33
+ TEST_AMOUNT.times do
34
+ test_subject.tvalue
35
+ end
36
+ end
37
+
38
+ x.report('return') do
39
+ test_subject = TestSubject.new
40
+ TEST_AMOUNT.times do
41
+ test_subject.rvalue
42
+ end
43
+ end
44
+
45
+ end
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: 4.0.1
4
+ version: 5.0.0.rc1
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-07-31 00:00:00.000000000 Z
11
+ date: 2016-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,8 +89,11 @@ files:
89
89
  - VERSION
90
90
  - Vagrantfile
91
91
  - bin/rack-app
92
- - exec/rack-app
92
+ - dev/console
93
+ - dev/rack-app
93
94
  - lib/rack/app.rb
95
+ - lib/rack/app/bundled_extensions.rb
96
+ - lib/rack/app/bundled_extensions/logger.rb
94
97
  - lib/rack/app/cli.rb
95
98
  - lib/rack/app/cli/command.rb
96
99
  - lib/rack/app/cli/command/configurator.rb
@@ -110,7 +113,7 @@ files:
110
113
  - lib/rack/app/instance_methods/core.rb
111
114
  - lib/rack/app/instance_methods/http_control.rb
112
115
  - lib/rack/app/instance_methods/payload.rb
113
- - lib/rack/app/instance_methods/upload.rb
116
+ - lib/rack/app/instance_methods/serve_file.rb
114
117
  - lib/rack/app/middlewares.rb
115
118
  - lib/rack/app/middlewares/header_setter.rb
116
119
  - lib/rack/app/middlewares/params.rb
@@ -132,6 +135,7 @@ files:
132
135
  - lib/rack/app/singleton_methods/extensions.rb
133
136
  - lib/rack/app/singleton_methods/http_methods.rb
134
137
  - lib/rack/app/singleton_methods/inheritance.rb
138
+ - lib/rack/app/singleton_methods/middleware.rb
135
139
  - lib/rack/app/singleton_methods/mounting.rb
136
140
  - lib/rack/app/singleton_methods/params_validator.rb
137
141
  - lib/rack/app/singleton_methods/rack_interface.rb
@@ -154,6 +158,8 @@ files:
154
158
  - lib/rack/app/utils/parser/time.rb
155
159
  - lib/rack/app/version.rb
156
160
  - rack-app.gemspec
161
+ - spike/method_vs_instance_exec.rb
162
+ - spike/return_vs_throw.rb
157
163
  - src/Net__HTTP Cheat Sheet.pdf
158
164
  homepage: http://www.rack-app.com/
159
165
  licenses:
@@ -170,12 +176,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
170
176
  version: '0'
171
177
  required_rubygems_version: !ruby/object:Gem::Requirement
172
178
  requirements:
173
- - - ">="
179
+ - - ">"
174
180
  - !ruby/object:Gem::Version
175
- version: '0'
181
+ version: 1.3.1
176
182
  requirements: []
177
183
  rubyforge_project:
178
- rubygems_version: 2.4.8
184
+ rubygems_version: 2.5.1
179
185
  signing_key:
180
186
  specification_version: 4
181
187
  summary: Your next favourite, performance designed micro framework!