renee-core 0.2.0 → 0.3.0
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.
- data/README.md +5 -5
- data/lib/renee_core.rb +91 -0
- data/lib/renee_core/chaining.rb +71 -0
- data/lib/renee_core/env_accessors.rb +72 -0
- data/lib/{renee-core → renee_core}/exceptions.rb +1 -1
- data/lib/{renee-core → renee_core}/matcher.rb +1 -1
- data/lib/renee_core/rack_interaction.rb +50 -0
- data/lib/renee_core/request_context.rb +25 -0
- data/lib/renee_core/responding.rb +110 -0
- data/lib/{renee-core → renee_core}/response.rb +2 -2
- data/lib/renee_core/routing.rb +322 -0
- data/lib/renee_core/transform.rb +18 -0
- data/lib/{renee-core → renee_core}/url_generation.rb +3 -3
- data/lib/renee_core/version.rb +6 -0
- data/renee-core.gemspec +1 -1
- data/test/env_accessors_test.rb +43 -0
- data/test/include_test.rb +1 -1
- data/test/responding_test.rb +1 -1
- data/test/routing_test.rb +5 -5
- data/test/test_helper.rb +1 -1
- data/test/url_generation_test.rb +8 -8
- metadata +19 -18
- data/lib/renee-core.rb +0 -78
- data/lib/renee-core/application.rb +0 -32
- data/lib/renee-core/application/chaining.rb +0 -73
- data/lib/renee-core/application/rack_interaction.rb +0 -45
- data/lib/renee-core/application/request_context.rb +0 -29
- data/lib/renee-core/application/responding.rb +0 -112
- data/lib/renee-core/application/routing.rb +0 -319
- data/lib/renee-core/application/transform.rb +0 -20
- data/lib/renee-core/settings.rb +0 -63
- data/lib/renee-core/version.rb +0 -6
data/test/responding_test.rb
CHANGED
data/test/routing_test.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require File.expand_path('../test_helper', __FILE__)
|
2
2
|
|
3
|
-
describe Renee::Core::
|
3
|
+
describe Renee::Core::Routing do
|
4
4
|
|
5
5
|
def renee_for(path, options = {}, &block)
|
6
|
-
Renee
|
6
|
+
Renee.core(&block).call(Rack::MockRequest.env_for(path, options))
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "with paths" do
|
@@ -118,7 +118,7 @@ describe Renee::Core::Application::Routing do
|
|
118
118
|
it "should not ignore trailing slashes if told not to" do
|
119
119
|
type = { 'Content-Type' => 'text/plain' }
|
120
120
|
mock_app do
|
121
|
-
|
121
|
+
path('test').empty { get { halt [200,type,['test']] } }
|
122
122
|
end
|
123
123
|
get '/test/'
|
124
124
|
assert_equal 404, response.status
|
@@ -403,7 +403,7 @@ describe Renee::Core::Application::Routing do
|
|
403
403
|
type = { 'Content-Type' => 'text/plain' }
|
404
404
|
mock_app do
|
405
405
|
path 'test' do
|
406
|
-
halt run Renee
|
406
|
+
halt run Renee.core {
|
407
407
|
path 'time' do
|
408
408
|
halt halt [200,type,['test']]
|
409
409
|
end
|
@@ -419,7 +419,7 @@ describe Renee::Core::Application::Routing do
|
|
419
419
|
type = { 'Content-Type' => 'text/plain' }
|
420
420
|
mock_app do
|
421
421
|
path 'test' do
|
422
|
-
run! Renee
|
422
|
+
run! Renee.core {
|
423
423
|
path 'time' do
|
424
424
|
halt halt [200,type,['test']]
|
425
425
|
end
|
data/test/test_helper.rb
CHANGED
data/test/url_generation_test.rb
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../test_helper', __FILE__)
|
|
4
4
|
|
5
5
|
describe Renee::Core::URLGeneration do
|
6
6
|
it "should allow registration and generation of paths" do
|
7
|
-
r = Renee
|
7
|
+
r = Renee.core()
|
8
8
|
r.register(:test, '/test/time')
|
9
9
|
r.register(:test_var, '/test/:id')
|
10
10
|
assert_equal '/test/time', r.path(:test)
|
@@ -13,41 +13,41 @@ describe Renee::Core::URLGeneration do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should allow registration and generation of urls" do
|
16
|
-
r = Renee
|
16
|
+
r = Renee.core()
|
17
17
|
r.register(:test, 'http://localhost:8080/test/:time')
|
18
18
|
assert_equal 'http://localhost:8080/test/123', r.url(:test, 123)
|
19
19
|
assert_equal 'http://localhost:8080/test/654', r.url(:test, :time => '654')
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should escape values when generating" do
|
23
|
-
r = Renee
|
23
|
+
r = Renee.core()
|
24
24
|
r.register(:test, '/:test')
|
25
25
|
assert_equal '/f%C3%B8%C3%B8', r.path(:test, "føø")
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should encode extra values as query string params" do
|
29
|
-
r = Renee
|
29
|
+
r = Renee.core()
|
30
30
|
r.register(:test, '/:test')
|
31
31
|
assert_equal '/foo?bar=baz', r.path(:test, 'foo', :bar => :baz)
|
32
32
|
assert_equal '/foo?bar=baz', r.path(:test, :test => 'foo', :bar => :baz)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should allow default values" do
|
36
|
-
r = Renee
|
36
|
+
r = Renee.core()
|
37
37
|
r.register(:test, '/:test', :test => 'foo')
|
38
38
|
assert_equal '/foo', r.path(:test)
|
39
39
|
assert_equal '/baz', r.path(:test, :test => 'baz')
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should include default vars as query string vars" do
|
43
|
-
r = Renee
|
43
|
+
r = Renee.core()
|
44
44
|
r.register(:test, '/:foo', :test => 'foo')
|
45
45
|
assert_equal '/foo?test=foo', r.path(:test, 'foo')
|
46
46
|
assert_equal '/foo?test=foo', r.path(:test, :foo => 'foo')
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should allow #prefix calls for nesting common path parts" do
|
50
|
-
r = Renee
|
50
|
+
r = Renee.core()
|
51
51
|
r.prefix('/foo') do
|
52
52
|
r.register(:foo_bar, '/bar')
|
53
53
|
end
|
@@ -55,7 +55,7 @@ describe Renee::Core::URLGeneration do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should allow passing defaults and overriding them on a per-register basis" do
|
58
|
-
r = Renee
|
58
|
+
r = Renee.core()
|
59
59
|
r.prefix('/foo', :bar => 'baz') do
|
60
60
|
register(:foo_bar, '/bar', :bar => 'bam')
|
61
61
|
register(:foo_baz, '/baz')
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: renee-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Hull
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-10-
|
20
|
+
date: 2011-10-25 00:00:00 Z
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
23
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -114,22 +114,22 @@ files:
|
|
114
114
|
- .yardopts
|
115
115
|
- README.md
|
116
116
|
- Rakefile
|
117
|
-
- lib/
|
118
|
-
- lib/
|
119
|
-
- lib/
|
120
|
-
- lib/
|
121
|
-
- lib/
|
122
|
-
- lib/
|
123
|
-
- lib/
|
124
|
-
- lib/
|
125
|
-
- lib/
|
126
|
-
- lib/
|
127
|
-
- lib/
|
128
|
-
- lib/
|
129
|
-
- lib/
|
130
|
-
- lib/renee-core/version.rb
|
117
|
+
- lib/renee_core.rb
|
118
|
+
- lib/renee_core/chaining.rb
|
119
|
+
- lib/renee_core/env_accessors.rb
|
120
|
+
- lib/renee_core/exceptions.rb
|
121
|
+
- lib/renee_core/matcher.rb
|
122
|
+
- lib/renee_core/rack_interaction.rb
|
123
|
+
- lib/renee_core/request_context.rb
|
124
|
+
- lib/renee_core/responding.rb
|
125
|
+
- lib/renee_core/response.rb
|
126
|
+
- lib/renee_core/routing.rb
|
127
|
+
- lib/renee_core/transform.rb
|
128
|
+
- lib/renee_core/url_generation.rb
|
129
|
+
- lib/renee_core/version.rb
|
131
130
|
- renee-core.gemspec
|
132
131
|
- test/chaining_test.rb
|
132
|
+
- test/env_accessors_test.rb
|
133
133
|
- test/include_test.rb
|
134
134
|
- test/responding_test.rb
|
135
135
|
- test/routing_test.rb
|
@@ -171,6 +171,7 @@ specification_version: 3
|
|
171
171
|
summary: The super-friendly rack helpers
|
172
172
|
test_files:
|
173
173
|
- test/chaining_test.rb
|
174
|
+
- test/env_accessors_test.rb
|
174
175
|
- test/include_test.rb
|
175
176
|
- test/responding_test.rb
|
176
177
|
- test/routing_test.rb
|
data/lib/renee-core.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'rack'
|
2
|
-
require 'renee-core/version'
|
3
|
-
require 'renee-core/matcher'
|
4
|
-
require 'renee-core/settings'
|
5
|
-
require 'renee-core/response'
|
6
|
-
require 'renee-core/application'
|
7
|
-
require 'renee-core/url_generation'
|
8
|
-
require 'renee-core/exceptions'
|
9
|
-
|
10
|
-
# Top-level Renee constant
|
11
|
-
class Renee
|
12
|
-
# The top-level class for creating core application.
|
13
|
-
# For convience you can also used a method named #Renee
|
14
|
-
# for decalaring new instances.
|
15
|
-
#
|
16
|
-
# @example
|
17
|
-
# Renee::Core.new { path('/hello') { halt :ok } }
|
18
|
-
#
|
19
|
-
class Core
|
20
|
-
include URLGeneration
|
21
|
-
|
22
|
-
# The application block used to initialize this application.
|
23
|
-
attr_reader :application_block
|
24
|
-
# The {Settings} object used to initialize this application.
|
25
|
-
attr_reader :settings
|
26
|
-
|
27
|
-
# @param [Proc] application_block The block of code that will be executed on each invocation of call #call.
|
28
|
-
# Each time #call is called, a new instance of {Renee::Core::Application} will
|
29
|
-
# be created. The block given will be #instance_eval 'd within
|
30
|
-
# the context of that new instance.
|
31
|
-
#
|
32
|
-
def initialize(base_application_class = Application, &application_block)
|
33
|
-
@base_application_class = base_application_class
|
34
|
-
@application_block = application_block
|
35
|
-
@settings = Settings.new
|
36
|
-
end
|
37
|
-
|
38
|
-
# This is a rack-compliant `Rack#call`.
|
39
|
-
#
|
40
|
-
# @param [Hash] env The environment hash.
|
41
|
-
#
|
42
|
-
# @return [Array] A rack compliant return.
|
43
|
-
#
|
44
|
-
# @see http://rack.rubyforge.org/doc/SPEC.html
|
45
|
-
#
|
46
|
-
def call(env)
|
47
|
-
application_class.new(settings, &application_block).call(env)
|
48
|
-
end
|
49
|
-
|
50
|
-
##
|
51
|
-
# Configure settings for your Renee application. Accepts a settings file path
|
52
|
-
# or a block containing the configuration settings.
|
53
|
-
#
|
54
|
-
# @example
|
55
|
-
# Renee::Core.new { ... }.setup { views_path "./views" }
|
56
|
-
#
|
57
|
-
# @api public
|
58
|
-
def setup(path = nil, &blk)
|
59
|
-
raise "You cannot supply both an argument and a block to the method." unless path.nil? ^ blk.nil?
|
60
|
-
case path
|
61
|
-
when nil then settings.instance_eval(&blk)
|
62
|
-
when Settings then @settings = path
|
63
|
-
when String then File.exist?(path) ? settings.instance_eval(File.read(path), path, 1) : raise("The settings file #{path} does not exist")
|
64
|
-
else raise "Could not setup with #{path.inspect}"
|
65
|
-
end
|
66
|
-
self
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
def application_class
|
71
|
-
@application_class ||= begin
|
72
|
-
app_cls = Class.new(@base_application_class)
|
73
|
-
settings.includes.each { |inc| app_cls.send(:include, inc) }
|
74
|
-
app_cls
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "renee-core/application/request_context"
|
2
|
-
require "renee-core/application/chaining"
|
3
|
-
require "renee-core/application/routing"
|
4
|
-
require "renee-core/application/responding"
|
5
|
-
require "renee-core/application/rack_interaction"
|
6
|
-
require "renee-core/application/transform"
|
7
|
-
|
8
|
-
class Renee
|
9
|
-
class Core
|
10
|
-
# This is the main class used to do the respond to requests. {Routing} provides route helpers.
|
11
|
-
# {RequestContext} adds the RequestContext#call method to respond to Rack applications.
|
12
|
-
# {Responding} defines the method Responding#halt which stops processing within a {Renee::Application}.
|
13
|
-
# It also has methods to interpret arguments to #halt and redirection response helpers.
|
14
|
-
# {RackInteraction} adds methods for interacting with Rack.
|
15
|
-
class Application
|
16
|
-
include Chaining
|
17
|
-
include RequestContext
|
18
|
-
include Routing
|
19
|
-
include Responding
|
20
|
-
include RackInteraction
|
21
|
-
include Transform
|
22
|
-
|
23
|
-
attr_reader :application_block, :settings
|
24
|
-
|
25
|
-
# @param [Proc] application_block The block that will be #instance_eval'd on each invocation to #call
|
26
|
-
def initialize(settings, &application_block)
|
27
|
-
@settings = settings
|
28
|
-
@application_block = application_block
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
class Renee
|
4
|
-
class Core
|
5
|
-
class Application
|
6
|
-
# Module for creating chainable methods. To use this within your own modules, first `include Chaining`, then,
|
7
|
-
# mark methods you want to be available with `chain_method :method_name`.
|
8
|
-
# @example
|
9
|
-
# module MoreRoutingMethods
|
10
|
-
# include Chaining
|
11
|
-
# def other_routing_method
|
12
|
-
# # ..
|
13
|
-
# end
|
14
|
-
# chain_method :other_routing_method
|
15
|
-
#
|
16
|
-
module Chaining
|
17
|
-
# @private
|
18
|
-
class ChainingProxy
|
19
|
-
def initialize(target, proxy_blk)
|
20
|
-
@target, @proxy_blk, @calls = target, proxy_blk, []
|
21
|
-
end
|
22
|
-
|
23
|
-
def method_missing(m, *args, &blk)
|
24
|
-
@calls << [m, *args]
|
25
|
-
if blk.nil? && @target.class.private_method_defined?(:"#{m}_without_chain")
|
26
|
-
self
|
27
|
-
else
|
28
|
-
ret = nil
|
29
|
-
@proxy_blk.call(proc do |*inner_args|
|
30
|
-
callback = proc do |*callback_args|
|
31
|
-
inner_args.concat(callback_args)
|
32
|
-
if @calls.size == 0
|
33
|
-
return blk.call(*inner_args) if blk
|
34
|
-
else
|
35
|
-
call = @calls.shift
|
36
|
-
ret = @target.send(call.at(0), *call.at(1), &callback)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
call = @calls.shift
|
40
|
-
ret = @target.send(call.at(0), *call.at(1), &callback)
|
41
|
-
end)
|
42
|
-
ret
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# @private
|
48
|
-
module ClassMethods
|
49
|
-
def chain_method(*methods)
|
50
|
-
methods.each do |m|
|
51
|
-
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
52
|
-
alias_method :#{m}_without_chain, :#{m}
|
53
|
-
def #{m}(*args, &blk)
|
54
|
-
chain(blk) { |subblk| #{m}_without_chain(*args, &subblk) }
|
55
|
-
end
|
56
|
-
private :#{m}_without_chain
|
57
|
-
EOT
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
def chain(blk, &proxy)
|
64
|
-
blk ? yield(blk) : ChainingProxy.new(self, proxy)
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.included(o)
|
68
|
-
o.extend(ClassMethods)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
class Renee
|
2
|
-
class Core
|
3
|
-
class Application
|
4
|
-
# A module that defines useful Rack interaction methods.
|
5
|
-
module RackInteraction
|
6
|
-
|
7
|
-
# Creates an ad-hoc Rack application within the context of a Rack::Builder.
|
8
|
-
# @example
|
9
|
-
# get { halt build { use Rack::ContentLength; run proc { |env| Rack::Response.new("Hello!").finish } } }
|
10
|
-
#
|
11
|
-
def build(&blk)
|
12
|
-
run Rack::Builder.new(&blk).to_app
|
13
|
-
end
|
14
|
-
|
15
|
-
# Creates an ad-hoc Rack application within the context of a Rack::Builder that immediately halts when done.
|
16
|
-
# @see #run!
|
17
|
-
# @example
|
18
|
-
# get { halt build { use Rack::ContentLength; run proc { |env| Rack::Response.new("Hello!").finish } } }
|
19
|
-
#
|
20
|
-
def build!(&blk)
|
21
|
-
run! build(&blk)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Runs a rack application
|
25
|
-
# @example
|
26
|
-
# get { halt run proc { |env| Renee::Core::Response.new("Hello!").finish } }
|
27
|
-
#
|
28
|
-
def run(app = nil, &blk)
|
29
|
-
raise "You cannot supply both a block and an app" unless app.nil? ^ blk.nil?
|
30
|
-
(app || blk).call(env)
|
31
|
-
end
|
32
|
-
|
33
|
-
# Runs a rack application and halts immediately.
|
34
|
-
#
|
35
|
-
# @see #run
|
36
|
-
# @example
|
37
|
-
# get { run proc { |env| Renee::Core::Response.new("Hello!").finish } }
|
38
|
-
#
|
39
|
-
def run!(*args)
|
40
|
-
halt run(*args)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
class Renee
|
2
|
-
class Core
|
3
|
-
class Application
|
4
|
-
# This module deals with the Rack#call compilance. It defines #call and also defines several critical methods
|
5
|
-
# used by interaction by other application modules.
|
6
|
-
module RequestContext
|
7
|
-
attr_reader :env, :request, :detected_extension, :is_index_request
|
8
|
-
alias_method :is_index_request?, :is_index_request
|
9
|
-
|
10
|
-
# Provides a rack interface compliant call method.
|
11
|
-
# @param[Hash] env The rack environment.
|
12
|
-
def call(env)
|
13
|
-
@env, @request = env, Rack::Request.new(env)
|
14
|
-
@detected_extension = env['PATH_INFO'][/\.([^\.\/]+)$/, 1]
|
15
|
-
@is_index_request = env['PATH_INFO'][/^\/?$/]
|
16
|
-
# TODO clear template cache in development? `template_cache.clear`
|
17
|
-
catch(:halt) do
|
18
|
-
begin
|
19
|
-
instance_eval(&application_block)
|
20
|
-
rescue ClientError => e
|
21
|
-
e.response ? instance_eval(&e.response) : halt("There was an error with your request", 400)
|
22
|
-
end
|
23
|
-
Renee::Core::Response.new("Not found", 404).finish
|
24
|
-
end
|
25
|
-
end # call
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|