sinatra-contrib 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +135 -0
  3. data/Rakefile +75 -0
  4. data/ideas.md +29 -0
  5. data/lib/sinatra/capture.rb +42 -0
  6. data/lib/sinatra/config_file.rb +151 -0
  7. data/lib/sinatra/content_for.rb +111 -0
  8. data/lib/sinatra/contrib.rb +39 -0
  9. data/lib/sinatra/contrib/all.rb +2 -0
  10. data/lib/sinatra/contrib/setup.rb +53 -0
  11. data/lib/sinatra/contrib/version.rb +45 -0
  12. data/lib/sinatra/cookies.rb +331 -0
  13. data/lib/sinatra/decompile.rb +113 -0
  14. data/lib/sinatra/engine_tracking.rb +96 -0
  15. data/lib/sinatra/extension.rb +95 -0
  16. data/lib/sinatra/json.rb +134 -0
  17. data/lib/sinatra/link_header.rb +132 -0
  18. data/lib/sinatra/multi_route.rb +81 -0
  19. data/lib/sinatra/namespace.rb +282 -0
  20. data/lib/sinatra/reloader.rb +384 -0
  21. data/lib/sinatra/respond_with.rb +245 -0
  22. data/lib/sinatra/streaming.rb +267 -0
  23. data/lib/sinatra/test_helpers.rb +87 -0
  24. data/sinatra-contrib.gemspec +125 -0
  25. data/spec/capture_spec.rb +80 -0
  26. data/spec/config_file/key_value.yml +6 -0
  27. data/spec/config_file/missing_env.yml +4 -0
  28. data/spec/config_file/with_envs.yml +7 -0
  29. data/spec/config_file/with_nested_envs.yml +11 -0
  30. data/spec/config_file_spec.rb +44 -0
  31. data/spec/content_for/different_key.erb +1 -0
  32. data/spec/content_for/different_key.erubis +1 -0
  33. data/spec/content_for/different_key.haml +2 -0
  34. data/spec/content_for/different_key.slim +2 -0
  35. data/spec/content_for/layout.erb +1 -0
  36. data/spec/content_for/layout.erubis +1 -0
  37. data/spec/content_for/layout.haml +1 -0
  38. data/spec/content_for/layout.slim +1 -0
  39. data/spec/content_for/multiple_blocks.erb +4 -0
  40. data/spec/content_for/multiple_blocks.erubis +4 -0
  41. data/spec/content_for/multiple_blocks.haml +8 -0
  42. data/spec/content_for/multiple_blocks.slim +8 -0
  43. data/spec/content_for/multiple_yields.erb +3 -0
  44. data/spec/content_for/multiple_yields.erubis +3 -0
  45. data/spec/content_for/multiple_yields.haml +3 -0
  46. data/spec/content_for/multiple_yields.slim +3 -0
  47. data/spec/content_for/passes_values.erb +1 -0
  48. data/spec/content_for/passes_values.erubis +1 -0
  49. data/spec/content_for/passes_values.haml +1 -0
  50. data/spec/content_for/passes_values.slim +1 -0
  51. data/spec/content_for/same_key.erb +1 -0
  52. data/spec/content_for/same_key.erubis +1 -0
  53. data/spec/content_for/same_key.haml +2 -0
  54. data/spec/content_for/same_key.slim +2 -0
  55. data/spec/content_for/takes_values.erb +1 -0
  56. data/spec/content_for/takes_values.erubis +1 -0
  57. data/spec/content_for/takes_values.haml +3 -0
  58. data/spec/content_for/takes_values.slim +3 -0
  59. data/spec/content_for_spec.rb +201 -0
  60. data/spec/cookies_spec.rb +782 -0
  61. data/spec/decompile_spec.rb +44 -0
  62. data/spec/extension_spec.rb +33 -0
  63. data/spec/json_spec.rb +115 -0
  64. data/spec/link_header_spec.rb +100 -0
  65. data/spec/multi_route_spec.rb +45 -0
  66. data/spec/namespace/foo.erb +1 -0
  67. data/spec/namespace/nested/foo.erb +1 -0
  68. data/spec/namespace_spec.rb +623 -0
  69. data/spec/okjson.rb +581 -0
  70. data/spec/reloader/app.rb.erb +40 -0
  71. data/spec/reloader_spec.rb +441 -0
  72. data/spec/respond_with/bar.erb +1 -0
  73. data/spec/respond_with/bar.json.erb +1 -0
  74. data/spec/respond_with/foo.html.erb +1 -0
  75. data/spec/respond_with/not_html.sass +2 -0
  76. data/spec/respond_with_spec.rb +289 -0
  77. data/spec/spec_helper.rb +6 -0
  78. data/spec/streaming_spec.rb +436 -0
  79. metadata +256 -0
@@ -0,0 +1,87 @@
1
+ require 'sinatra/base'
2
+ require 'rack/test'
3
+ require 'rack'
4
+ require 'forwardable'
5
+
6
+ module Sinatra
7
+ Base.set :environment, :test
8
+
9
+ module TestHelpers
10
+ class Session < Rack::Test::Session
11
+ def global_env
12
+ @global_env ||= {}
13
+ end
14
+
15
+ private
16
+
17
+ def default_env
18
+ super.merge global_env
19
+ end
20
+ end
21
+
22
+ include Rack::Test::Methods
23
+ extend Forwardable
24
+ attr_accessor :settings
25
+
26
+ def_delegators :last_response, :body, :headers, :status, :errors
27
+ def_delegators :app, :configure, :set, :enable, :disable, :use, :helpers, :register
28
+ def_delegators :current_session, :env_for
29
+ def_delegators :rack_mock_session, :cookie_jar
30
+
31
+ def mock_app(base = Sinatra::Base, &block)
32
+ inner = nil
33
+ @app = Sinatra.new(base) do
34
+ inner = self
35
+ class_eval(&block)
36
+ end
37
+ @settings = inner
38
+ app
39
+ end
40
+
41
+ def app=(base)
42
+ @app = base
43
+ end
44
+
45
+ alias set_app app=
46
+
47
+ def app
48
+ @app ||= Class.new Sinatra::Base
49
+ Rack::Lint.new @app
50
+ end
51
+
52
+ unless method_defined? :options
53
+ def options(uri, params = {}, env = {}, &block)
54
+ env = env_for(uri, env.merge(:method => "OPTIONS", :params => params))
55
+ current_session.send(:process_request, uri, env, &block)
56
+ end
57
+ end
58
+
59
+ unless method_defined? :patch
60
+ def patch(uri, params = {}, env = {}, &block)
61
+ env = env_for(uri, env.merge(:method => "PATCH", :params => params))
62
+ current_session.send(:process_request, uri, env, &block)
63
+ end
64
+ end
65
+
66
+ def last_request?
67
+ last_request
68
+ true
69
+ rescue Rack::Test::Error
70
+ false
71
+ end
72
+
73
+ def session
74
+ return {} unless last_request?
75
+ raise Rack::Test:Error, "session not enabled for app" unless last_env["rack.session"] or app.session?
76
+ last_request.session
77
+ end
78
+
79
+ def last_env
80
+ last_request.env
81
+ end
82
+
83
+ def build_rack_test_session(name) # :nodoc:
84
+ Session.new rack_mock_session(name)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,125 @@
1
+ # Run `rake sinatra-contrib.gemspec` to update the gemspec.
2
+ Gem::Specification.new do |s|
3
+ s.name = "sinatra-contrib"
4
+ s.version = "1.3.0"
5
+ s.description = "Collection of useful Sinatra extensions"
6
+ s.homepage = "http://github.com/sinatra/sinatra-contrib"
7
+ s.summary = s.description
8
+
9
+ # generated from git shortlog -sn
10
+ s.authors = [
11
+ "Konstantin Haase",
12
+ "Gabriel Andretta",
13
+ "Nicolas Sanguinetti",
14
+ "Eliot Shepard",
15
+ "Andrew Crump",
16
+ "Matt Lyon",
17
+ "undr"
18
+ ]
19
+
20
+ # generated from git shortlog -sne
21
+ s.email = [
22
+ "konstantin.mailinglists@googlemail.com",
23
+ "ohhgabriel@gmail.com",
24
+ "contacto@nicolassanguinetti.info",
25
+ "eshepard@slower.net",
26
+ "andrew.crump@ieee.org",
27
+ "matt@flowerpowered.com",
28
+ "undr@yandex.ru"
29
+ ]
30
+
31
+ # generated from git ls-files
32
+ s.files = [
33
+ "LICENSE",
34
+ "README.md",
35
+ "Rakefile",
36
+ "ideas.md",
37
+ "lib/sinatra/capture.rb",
38
+ "lib/sinatra/config_file.rb",
39
+ "lib/sinatra/content_for.rb",
40
+ "lib/sinatra/contrib.rb",
41
+ "lib/sinatra/contrib/all.rb",
42
+ "lib/sinatra/contrib/setup.rb",
43
+ "lib/sinatra/contrib/version.rb",
44
+ "lib/sinatra/cookies.rb",
45
+ "lib/sinatra/decompile.rb",
46
+ "lib/sinatra/engine_tracking.rb",
47
+ "lib/sinatra/extension.rb",
48
+ "lib/sinatra/json.rb",
49
+ "lib/sinatra/link_header.rb",
50
+ "lib/sinatra/multi_route.rb",
51
+ "lib/sinatra/namespace.rb",
52
+ "lib/sinatra/reloader.rb",
53
+ "lib/sinatra/respond_with.rb",
54
+ "lib/sinatra/streaming.rb",
55
+ "lib/sinatra/test_helpers.rb",
56
+ "sinatra-contrib.gemspec",
57
+ "spec/capture_spec.rb",
58
+ "spec/config_file/key_value.yml",
59
+ "spec/config_file/missing_env.yml",
60
+ "spec/config_file/with_envs.yml",
61
+ "spec/config_file/with_nested_envs.yml",
62
+ "spec/config_file_spec.rb",
63
+ "spec/content_for/different_key.erb",
64
+ "spec/content_for/different_key.erubis",
65
+ "spec/content_for/different_key.haml",
66
+ "spec/content_for/different_key.slim",
67
+ "spec/content_for/layout.erb",
68
+ "spec/content_for/layout.erubis",
69
+ "spec/content_for/layout.haml",
70
+ "spec/content_for/layout.slim",
71
+ "spec/content_for/multiple_blocks.erb",
72
+ "spec/content_for/multiple_blocks.erubis",
73
+ "spec/content_for/multiple_blocks.haml",
74
+ "spec/content_for/multiple_blocks.slim",
75
+ "spec/content_for/multiple_yields.erb",
76
+ "spec/content_for/multiple_yields.erubis",
77
+ "spec/content_for/multiple_yields.haml",
78
+ "spec/content_for/multiple_yields.slim",
79
+ "spec/content_for/passes_values.erb",
80
+ "spec/content_for/passes_values.erubis",
81
+ "spec/content_for/passes_values.haml",
82
+ "spec/content_for/passes_values.slim",
83
+ "spec/content_for/same_key.erb",
84
+ "spec/content_for/same_key.erubis",
85
+ "spec/content_for/same_key.haml",
86
+ "spec/content_for/same_key.slim",
87
+ "spec/content_for/takes_values.erb",
88
+ "spec/content_for/takes_values.erubis",
89
+ "spec/content_for/takes_values.haml",
90
+ "spec/content_for/takes_values.slim",
91
+ "spec/content_for_spec.rb",
92
+ "spec/cookies_spec.rb",
93
+ "spec/decompile_spec.rb",
94
+ "spec/extension_spec.rb",
95
+ "spec/json_spec.rb",
96
+ "spec/link_header_spec.rb",
97
+ "spec/multi_route_spec.rb",
98
+ "spec/namespace/foo.erb",
99
+ "spec/namespace/nested/foo.erb",
100
+ "spec/namespace_spec.rb",
101
+ "spec/okjson.rb",
102
+ "spec/reloader/app.rb.erb",
103
+ "spec/reloader_spec.rb",
104
+ "spec/respond_with/bar.erb",
105
+ "spec/respond_with/bar.json.erb",
106
+ "spec/respond_with/foo.html.erb",
107
+ "spec/respond_with/not_html.sass",
108
+ "spec/respond_with_spec.rb",
109
+ "spec/spec_helper.rb",
110
+ "spec/streaming_spec.rb"
111
+ ]
112
+
113
+ s.add_dependency "sinatra", "~> 1.3.0"
114
+ s.add_dependency "backports", ">= 2.0"
115
+ s.add_dependency "tilt", "~> 1.3"
116
+ s.add_dependency "rack-test"
117
+ s.add_dependency "rack-protection"
118
+ s.add_dependency "eventmachine"
119
+
120
+ s.add_development_dependency "rspec", "~> 2.3"
121
+ s.add_development_dependency "haml"
122
+ s.add_development_dependency "erubis"
123
+ s.add_development_dependency "slim"
124
+ s.add_development_dependency "rake"
125
+ end
@@ -0,0 +1,80 @@
1
+ require 'backports'
2
+ require 'slim'
3
+ require_relative 'spec_helper'
4
+
5
+ describe Sinatra::Capture do
6
+ subject do
7
+ Sinatra.new do
8
+ enable :inline_templates
9
+ helpers Sinatra::Capture
10
+ end.new!
11
+ end
12
+ Tilt.prefer Tilt::ERBTemplate
13
+
14
+ extend Forwardable
15
+ def_delegators :subject, :capture, :capture_later
16
+
17
+ def render(engine, template)
18
+ subject.send(:render, engine, template.to_sym).strip.gsub(/\s+/, ' ')
19
+ end
20
+
21
+ shared_examples_for "a template language" do |engine|
22
+ lang = engine == :erubis ? :erb : engine
23
+
24
+ it "captures content" do
25
+ render(engine, "simple_#{lang}").should == "Say Hello World!"
26
+ end
27
+
28
+ it "allows nested captures" do
29
+ render(engine, "nested_#{lang}").should == "Say Hello World!"
30
+ end
31
+ end
32
+
33
+ describe('haml') { it_behaves_like "a template language", :haml }
34
+ describe('slim') { it_behaves_like "a template language", :slim }
35
+ describe('erb') { it_behaves_like "a template language", :erb }
36
+ describe('erubis') { it_behaves_like "a template language", :erubis }
37
+ end
38
+
39
+ __END__
40
+
41
+ @@ simple_erb
42
+ Say
43
+ <% a = capture do %>World<% end %>
44
+ Hello <%= a %>!
45
+
46
+ @@ nested_erb
47
+ Say
48
+ <% a = capture do %>
49
+ <% b = capture do %>World<% end %>
50
+ <%= b %>!
51
+ <% end %>
52
+ Hello <%= a.strip %>
53
+
54
+ @@ simple_slim
55
+ | Say
56
+ - a = capture do
57
+ | World
58
+ | Hello #{a.strip}!
59
+
60
+ @@ nested_slim
61
+ | Say
62
+ - a = capture do
63
+ - b = capture do
64
+ | World
65
+ | #{b.strip}!
66
+ | Hello #{a.strip}
67
+
68
+ @@ simple_haml
69
+ Say
70
+ - a = capture do
71
+ World
72
+ Hello #{a.strip}!
73
+
74
+ @@ nested_haml
75
+ Say
76
+ - a = capture do
77
+ - b = capture do
78
+ World
79
+ #{b.strip}!
80
+ Hello #{a.strip}
@@ -0,0 +1,6 @@
1
+ ---
2
+ foo: bar
3
+ something: 42
4
+ nested:
5
+ a: 1
6
+ b: 2
@@ -0,0 +1,4 @@
1
+ ---
2
+ foo:
3
+ production: 10
4
+ development: 20
@@ -0,0 +1,7 @@
1
+ ---
2
+ development:
3
+ foo: development
4
+ production:
5
+ foo: production
6
+ test:
7
+ foo: test
@@ -0,0 +1,11 @@
1
+ ---
2
+ database:
3
+ production:
4
+ adapter: postgresql
5
+ database: foo_production
6
+ development:
7
+ adapter: sqlite
8
+ database: db/development.db
9
+ test:
10
+ adapter: sqlite
11
+ database: db/test.db
@@ -0,0 +1,44 @@
1
+ require 'backports'
2
+ require_relative 'spec_helper'
3
+
4
+ describe Sinatra::ConfigFile do
5
+ def config_file(*args, &block)
6
+ mock_app do
7
+ register Sinatra::ConfigFile
8
+ set :root, File.expand_path('../config_file', __FILE__)
9
+ instance_eval(&block) if block
10
+ config_file(*args)
11
+ end
12
+ end
13
+
14
+ it 'should set options from a simple config_file' do
15
+ config_file 'key_value.yml'
16
+ settings.foo.should == 'bar'
17
+ settings.something.should == 42
18
+ end
19
+
20
+ it 'should create indifferent hashes' do
21
+ config_file 'key_value.yml'
22
+ settings.nested['a'].should == 1
23
+ settings.nested[:a].should == 1
24
+ end
25
+
26
+ it 'should recognize env specific settings per file' do
27
+ config_file 'with_envs.yml'
28
+ settings.foo.should == 'test'
29
+ end
30
+
31
+ it 'should recognize env specific settings per setting' do
32
+ config_file 'with_nested_envs.yml'
33
+ settings.database[:adapter].should == 'sqlite'
34
+ end
35
+
36
+ it 'should not set present values to nil if the current env is missing' do
37
+ # first let's check the test is actually working properly
38
+ config_file('missing_env.yml') { set :foo => 42, :environment => :production }
39
+ settings.foo.should == 10
40
+ # now test it
41
+ config_file('missing_env.yml') { set :foo => 42, :environment => :test }
42
+ settings.foo.should == 42
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ <% content_for :bar do %>bar<% end %>
@@ -0,0 +1 @@
1
+ <% content_for :bar do %>bar<% end %>
@@ -0,0 +1,2 @@
1
+ - content_for :bar do
2
+ bar
@@ -0,0 +1,2 @@
1
+ - content_for :bar do
2
+ | bar
@@ -0,0 +1 @@
1
+ <%= yield_content :foo %>
@@ -0,0 +1 @@
1
+ <%= yield_content :foo %>
@@ -0,0 +1 @@
1
+ = yield_content :foo
@@ -0,0 +1 @@
1
+ = yield_content :foo
@@ -0,0 +1,4 @@
1
+ <% content_for :foo do %>foo<% end %>
2
+ <% content_for :foo do %>bar<% end %>
3
+ <% content_for :baz do %>WON'T RENDER ME<% end %>
4
+ <% content_for :foo do %>baz<% end %>
@@ -0,0 +1,4 @@
1
+ <% content_for :foo do %>foo<% end %>
2
+ <% content_for :foo do %>bar<% end %>
3
+ <% content_for :baz do %>WON'T RENDER ME<% end %>
4
+ <% content_for :foo do %>baz<% end %>
@@ -0,0 +1,8 @@
1
+ - content_for :foo do
2
+ foo
3
+ - content_for :foo do
4
+ bar
5
+ - content_for :baz do
6
+ WON'T RENDER ME
7
+ - content_for :foo do
8
+ baz
@@ -0,0 +1,8 @@
1
+ - content_for :foo do
2
+ | foo
3
+ - content_for :foo do
4
+ | bar
5
+ - content_for :baz do
6
+ | WON'T RENDER ME
7
+ - content_for :foo do
8
+ | baz
@@ -0,0 +1,3 @@
1
+ <%= yield_content :foo %>
2
+ <%= yield_content :foo %>
3
+ <%= yield_content :foo %>
@@ -0,0 +1,3 @@
1
+ <%= yield_content :foo %>
2
+ <%= yield_content :foo %>
3
+ <%= yield_content :foo %>