nyny 2.2.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,19 @@
1
- module NYNY
2
- module Runner
3
- def optimal_runner
4
- return Rack::Handler::WEBrick if RUBY_PLATFORM == 'java'
5
-
6
- begin
7
- Rack::Handler::Thin
8
- rescue LoadError
9
- Rack::Handler::WEBrick
10
- end
11
- end
12
-
13
- def run! port=9292
14
- middlewares.unshift Rack::CommonLogger
15
- middlewares.unshift Rack::ShowExceptions unless NYNY.env.production?
16
- optimal_runner.run new, :Port => port
17
- end
18
- end
19
- end
1
+ module NYNY
2
+ module Runner
3
+ def optimal_runner
4
+ return Rack::Handler::WEBrick if RUBY_PLATFORM == 'java'
5
+
6
+ begin
7
+ Rack::Handler::Thin
8
+ rescue LoadError
9
+ Rack::Handler::WEBrick
10
+ end
11
+ end
12
+
13
+ def run! port=9292
14
+ use Rack::CommonLogger
15
+ use Rack::ShowExceptions unless NYNY.env.production?
16
+ optimal_runner.run new, :Port => port
17
+ end
18
+ end
19
+ end
@@ -1,19 +1,21 @@
1
- module NYNY
2
- module Templates
3
- module Helpers
4
- def render template, locals = {}, options = {}, &block
5
- template_cache.fetch(template) do
6
- Tilt.new(template, options)
7
- end.render(self, locals, &block)
8
- end
9
-
10
- def template_cache
11
- Thread.current[:template_cache] ||= Tilt::Cache.new
12
- end
13
- end
14
-
15
- def self.registered app
16
- app.helpers Helpers
17
- end
18
- end
1
+ require 'tilt'
2
+
3
+ module NYNY
4
+ module Templates
5
+ module Helpers
6
+ def render template, locals = {}, options = {}, &block
7
+ template_cache.fetch(template) do
8
+ Tilt.new(template, options)
9
+ end.render(self, locals, &block)
10
+ end
11
+
12
+ def template_cache
13
+ Thread.current[:template_cache] ||= Tilt::Cache.new
14
+ end
15
+ end
16
+
17
+ def self.registered app
18
+ app.helpers Helpers
19
+ end
20
+ end
19
21
  end
@@ -1,25 +1,25 @@
1
- module NYNY
2
- class Request < Rack::Request
3
- end
4
-
5
- class Response < Rack::Response
6
- attr_reader :raw_body
7
-
8
- def initialize body=[], status=200, header={}
9
- @raw_body = body
10
- super body.to_s, status, header
11
- end
12
-
13
- def body= value
14
- @raw_body = value
15
- @body = []
16
- @length = 0
17
-
18
- if value.respond_to? :to_str
19
- write value.to_str
20
- elsif value.respond_to?(:each)
21
- value.each {|part| write part.to_s }
22
- end
23
- end
24
- end
25
- end
1
+ module NYNY
2
+ class Request < Rack::Request
3
+ end
4
+
5
+ class Response < Rack::Response
6
+ attr_reader :raw_body
7
+
8
+ def initialize body=[], status=200, header={}
9
+ @raw_body = body
10
+ super body.to_s, status, header
11
+ end
12
+
13
+ def body= value
14
+ @raw_body = value
15
+ @body = []
16
+ @length = 0
17
+
18
+ if value.respond_to? :to_str
19
+ write value.to_str
20
+ elsif value.respond_to?(:each)
21
+ value.each {|part| write part.to_s }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,52 +1,43 @@
1
- module NYNY
2
- class RequestScope
3
- attr_reader :request, :response
4
-
5
- def self.add_helper_module m
6
- include m
7
- end
8
-
9
- def initialize request
10
- @request = request
11
- @response = Response.new '', 200, {'Content-Type' => 'text/html'}
12
- end
13
-
14
- def params
15
- request.params
16
- end
17
-
18
- def headers hash={}
19
- response.headers.merge! hash
20
- end
21
-
22
- def session
23
- request.session
24
- end
25
-
26
- def cookies
27
- request.cookies
28
- end
29
-
30
- def status code
31
- response.status = code
32
- end
33
-
34
- def halt status, headers={}, body=''
35
- response.status = status
36
- response.headers.merge! headers
37
- response.body = body
38
- throw :halt, response
39
- end
40
-
41
- def redirect_to uri, status=302
42
- halt status, {'Location' => uri}
43
- end
44
- alias_method :redirect, :redirect_to
45
-
46
- def apply_to &handler
47
- response.body = instance_eval(&handler)
48
- cookies.each {|k,v| response.set_cookie k,v }
49
- response
50
- end
51
- end
52
- end
1
+ require 'forwardable'
2
+ require 'rack/contrib/cookies'
3
+
4
+ module NYNY
5
+ class RequestScope
6
+ extend Forwardable
7
+
8
+ attr_reader :request, :response
9
+ def_delegators :request, :params, :session
10
+ def_delegators :response, :headers
11
+
12
+ def initialize request
13
+ @request = request
14
+ @response = Response.new '', 200, {'Content-Type' => 'text/html'}
15
+ end
16
+
17
+ def cookies
18
+ @cookies ||= Rack::Cookies::CookieJar.new(request.cookies)
19
+ end
20
+
21
+ def status code
22
+ response.status = code
23
+ end
24
+
25
+ def halt status, headers={}, body=''
26
+ response.status = status
27
+ response.headers.merge! headers
28
+ response.body = body
29
+ throw :halt, response.finish
30
+ end
31
+
32
+ def redirect_to uri, status=302
33
+ halt status, {'Location' => uri}
34
+ end
35
+ alias_method :redirect, :redirect_to
36
+
37
+ def apply_to &handler
38
+ response.body = instance_eval(&handler)
39
+ cookies.finish!(response)
40
+ response.finish
41
+ end
42
+ end
43
+ end
data/lib/nyny/route.rb CHANGED
@@ -1,40 +1,40 @@
1
- module NYNY
2
- class Route
3
- NAME_PATTERN = /:(\S+)/
4
-
5
- attr_reader :pattern, :handler, :method
6
- def initialize method, signature, &block
7
- @pattern = pattern_for signature
8
- @handler = Proc.new(&block)
9
- @method = method.to_s.upcase
10
- end
11
-
12
- def pattern_for signature
13
- return signature if signature.is_a? Regexp
14
- build_regex(signature.start_with?('/') ? signature : "/#{signature}")
15
- end
16
-
17
- def build_regex signature
18
- return %r(^#{signature}$) unless signature.include?(':')
19
-
20
- groups = signature.split('/').map do |part|
21
- next part if part.empty?
22
- next part unless part.start_with? ':'
23
- name = NAME_PATTERN.match(part)[1]
24
- %Q{(?<#{name}>\\S+)}
25
- end.select {|s| !s.empty? }.join('\/')
26
-
27
- %r(^\/#{groups}$)
28
- end
29
-
30
- def match? env
31
- return false unless method == env['REQUEST_METHOD']
32
- not pattern.match(env['PATH_INFO']).nil?
33
- end
34
-
35
- def url_params env
36
- data = pattern.match(env['PATH_INFO'])
37
- Hash[data.names.map {|n| [n.to_sym, URI.unescape(data[n])]}]
38
- end
39
- end
40
- end
1
+ module NYNY
2
+ class Route
3
+ NAME_PATTERN = /:(\S+)/
4
+
5
+ attr_reader :pattern, :handler, :method
6
+ def initialize method, signature, &block
7
+ @pattern = pattern_for signature
8
+ @handler = Proc.new(&block)
9
+ @method = method.to_s.upcase
10
+ end
11
+
12
+ def pattern_for signature
13
+ return signature if signature.is_a? Regexp
14
+ build_regex(signature.start_with?('/') ? signature : "/#{signature}")
15
+ end
16
+
17
+ def build_regex signature
18
+ return %r(^#{signature}$) unless signature.include?(':')
19
+
20
+ groups = signature.split('/').map do |part|
21
+ next part if part.empty?
22
+ next part unless part.start_with? ':'
23
+ name = NAME_PATTERN.match(part)[1]
24
+ %Q{(?<#{name}>\\S+)}
25
+ end.select {|s| !s.empty? }.join('\/')
26
+
27
+ %r(^\/#{groups}$)
28
+ end
29
+
30
+ def match? env
31
+ return false unless method == env['REQUEST_METHOD']
32
+ not pattern.match(env['PATH_INFO']).nil?
33
+ end
34
+
35
+ def url_params env
36
+ data = pattern.match(env['PATH_INFO'])
37
+ Hash[data.names.map {|n| [n.to_sym, URI.unescape(data[n])]}]
38
+ end
39
+ end
40
+ end
data/lib/nyny/router.rb CHANGED
@@ -1,43 +1,44 @@
1
- module NYNY
2
- class Router
3
- attr_reader :fallback, :routes, :before_hooks, :after_hooks
4
- def initialize options
5
- @fallback = options[:fallback]
6
- @routes = options[:routes]
7
- @before_hooks = options[:before_hooks]
8
- @after_hooks = options[:after_hooks]
9
- end
10
-
11
- def call env
12
- env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
13
- route = routes.find {|route| route.match? env }
14
-
15
- if route
16
- process route, env
17
- else
18
- fallback.call env
19
- end
20
- end
21
-
22
- def process route, env
23
- request = Request.new(env)
24
- request.params.merge! route.url_params(env)
25
- request.params.default_proc = proc {|h,k| h[k.to_s] || h[k.to_sym]}
26
-
27
- eval_response RequestScope.new(request), route.handler
28
- end
29
-
30
- def eval_response scope, handler
31
- response = catch (:halt) do
32
- before_hooks.each {|h| scope.instance_eval &h }
33
- scope.apply_to &handler
34
- end
35
-
36
- catch (:halt) do
37
- after_hooks.each {|h| scope.instance_eval &h }
38
- end
39
-
40
- response
41
- end
42
- end
43
- end
1
+ module NYNY
2
+ class Router
3
+ attr_reader :fallback, :routes, :before_hooks, :after_hooks, :scope_class
4
+ def initialize options
5
+ @fallback = options[:fallback]
6
+ @routes = options[:routes]
7
+ @before_hooks = options[:before_hooks]
8
+ @after_hooks = options[:after_hooks]
9
+ @scope_class = options[:scope_class]
10
+ end
11
+
12
+ def call env
13
+ env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
14
+ route = routes.find {|route| route.match? env }
15
+
16
+ if route
17
+ process route, env
18
+ else
19
+ fallback.call env
20
+ end
21
+ end
22
+
23
+ def process route, env
24
+ request = Request.new(env)
25
+ request.params.merge! route.url_params(env)
26
+ request.params.default_proc = proc {|h,k| h[k.to_s] || h[k.to_sym]}
27
+
28
+ eval_response scope_class.new(request), route.handler
29
+ end
30
+
31
+ def eval_response scope, handler
32
+ response = catch (:halt) do
33
+ before_hooks.each {|h| scope.instance_eval &h }
34
+ scope.apply_to &handler
35
+ end
36
+
37
+ catch (:halt) do
38
+ after_hooks.each {|h| scope.instance_eval &h }
39
+ end
40
+
41
+ response
42
+ end
43
+ end
44
+ end
data/lib/nyny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module NYNY
2
- VERSION = "2.2.1"
3
- end
1
+ module NYNY
2
+ VERSION = "3.0.0"
3
+ end
data/nyny.gemspec CHANGED
@@ -1,26 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'nyny/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "nyny"
8
- spec.version = NYNY::VERSION
9
- spec.authors = ["Andrei Lisnic"]
10
- spec.email = ["andrei.lisnic@gmail.com"]
11
- spec.description = %q{New York, New York - (very) small Sinatra clone.}
12
- spec.summary = %q{New York, New York.}
13
- spec.homepage = "http://alisnic.github.io/nyny/"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/).select {|f| !f.start_with?('examples')}
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_dependency "rack"
22
- spec.add_dependency "tilt"
23
- spec.add_development_dependency "bundler", "~> 1.3"
24
- spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec"
26
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nyny/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nyny"
8
+ spec.version = NYNY::VERSION
9
+ spec.authors = ["Andrei Lisnic"]
10
+ spec.email = ["andrei.lisnic@gmail.com"]
11
+ spec.description = %q{New York, New York - (very) small Sinatra clone.}
12
+ spec.summary = %q{New York, New York.}
13
+ spec.homepage = "http://alisnic.github.io/nyny/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/).select {|f| !f.start_with?('examples')}
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack", "~> 1.5.2"
22
+ spec.add_dependency "rack-contrib", "~> 1.1.0"
23
+ spec.add_dependency "tilt", "~> 1.4.1"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end