rubycut-sinatra-contrib 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +136 -0
  3. data/Rakefile +75 -0
  4. data/ideas.md +29 -0
  5. data/lib/sinatra/capture.rb +124 -0
  6. data/lib/sinatra/config_file.rb +167 -0
  7. data/lib/sinatra/content_for.rb +125 -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 +17 -0
  12. data/lib/sinatra/cookies.rb +331 -0
  13. data/lib/sinatra/decompile.rb +120 -0
  14. data/lib/sinatra/engine_tracking.rb +96 -0
  15. data/lib/sinatra/extension.rb +95 -0
  16. data/lib/sinatra/json.rb +130 -0
  17. data/lib/sinatra/link_header.rb +132 -0
  18. data/lib/sinatra/multi_route.rb +87 -0
  19. data/lib/sinatra/namespace.rb +284 -0
  20. data/lib/sinatra/reloader.rb +394 -0
  21. data/lib/sinatra/respond_with.rb +249 -0
  22. data/lib/sinatra/streaming.rb +267 -0
  23. data/lib/sinatra/test_helpers.rb +87 -0
  24. data/sinatra-contrib.gemspec +127 -0
  25. data/spec/capture_spec.rb +93 -0
  26. data/spec/config_file/key_value.yml +6 -0
  27. data/spec/config_file/key_value.yml.erb +6 -0
  28. data/spec/config_file/key_value_override.yml +2 -0
  29. data/spec/config_file/missing_env.yml +4 -0
  30. data/spec/config_file/with_envs.yml +7 -0
  31. data/spec/config_file/with_nested_envs.yml +11 -0
  32. data/spec/config_file_spec.rb +63 -0
  33. data/spec/content_for/different_key.erb +1 -0
  34. data/spec/content_for/different_key.erubis +1 -0
  35. data/spec/content_for/different_key.haml +2 -0
  36. data/spec/content_for/different_key.slim +2 -0
  37. data/spec/content_for/layout.erb +1 -0
  38. data/spec/content_for/layout.erubis +1 -0
  39. data/spec/content_for/layout.haml +1 -0
  40. data/spec/content_for/layout.slim +1 -0
  41. data/spec/content_for/multiple_blocks.erb +4 -0
  42. data/spec/content_for/multiple_blocks.erubis +4 -0
  43. data/spec/content_for/multiple_blocks.haml +8 -0
  44. data/spec/content_for/multiple_blocks.slim +8 -0
  45. data/spec/content_for/multiple_yields.erb +3 -0
  46. data/spec/content_for/multiple_yields.erubis +3 -0
  47. data/spec/content_for/multiple_yields.haml +3 -0
  48. data/spec/content_for/multiple_yields.slim +3 -0
  49. data/spec/content_for/passes_values.erb +1 -0
  50. data/spec/content_for/passes_values.erubis +1 -0
  51. data/spec/content_for/passes_values.haml +1 -0
  52. data/spec/content_for/passes_values.slim +1 -0
  53. data/spec/content_for/same_key.erb +1 -0
  54. data/spec/content_for/same_key.erubis +1 -0
  55. data/spec/content_for/same_key.haml +2 -0
  56. data/spec/content_for/same_key.slim +2 -0
  57. data/spec/content_for/takes_values.erb +1 -0
  58. data/spec/content_for/takes_values.erubis +1 -0
  59. data/spec/content_for/takes_values.haml +3 -0
  60. data/spec/content_for/takes_values.slim +3 -0
  61. data/spec/content_for_spec.rb +213 -0
  62. data/spec/cookies_spec.rb +802 -0
  63. data/spec/decompile_spec.rb +44 -0
  64. data/spec/extension_spec.rb +33 -0
  65. data/spec/json_spec.rb +117 -0
  66. data/spec/link_header_spec.rb +100 -0
  67. data/spec/multi_route_spec.rb +60 -0
  68. data/spec/namespace/foo.erb +1 -0
  69. data/spec/namespace/nested/foo.erb +1 -0
  70. data/spec/namespace_spec.rb +676 -0
  71. data/spec/okjson.rb +581 -0
  72. data/spec/reloader/app.rb.erb +40 -0
  73. data/spec/reloader_spec.rb +441 -0
  74. data/spec/respond_with/bar.erb +1 -0
  75. data/spec/respond_with/bar.json.erb +1 -0
  76. data/spec/respond_with/foo.html.erb +1 -0
  77. data/spec/respond_with/not_html.sass +2 -0
  78. data/spec/respond_with_spec.rb +297 -0
  79. data/spec/spec_helper.rb +7 -0
  80. data/spec/streaming_spec.rb +436 -0
  81. metadata +313 -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,127 @@
1
+ # Run `rake sinatra-contrib.gemspec` to update the gemspec.
2
+ Gem::Specification.new do |s|
3
+ s.name = "rubycut-sinatra-contrib"
4
+ s.version = "1.4.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/key_value.yml.erb",
60
+ "spec/config_file/key_value_override.yml",
61
+ "spec/config_file/missing_env.yml",
62
+ "spec/config_file/with_envs.yml",
63
+ "spec/config_file/with_nested_envs.yml",
64
+ "spec/config_file_spec.rb",
65
+ "spec/content_for/different_key.erb",
66
+ "spec/content_for/different_key.erubis",
67
+ "spec/content_for/different_key.haml",
68
+ "spec/content_for/different_key.slim",
69
+ "spec/content_for/layout.erb",
70
+ "spec/content_for/layout.erubis",
71
+ "spec/content_for/layout.haml",
72
+ "spec/content_for/layout.slim",
73
+ "spec/content_for/multiple_blocks.erb",
74
+ "spec/content_for/multiple_blocks.erubis",
75
+ "spec/content_for/multiple_blocks.haml",
76
+ "spec/content_for/multiple_blocks.slim",
77
+ "spec/content_for/multiple_yields.erb",
78
+ "spec/content_for/multiple_yields.erubis",
79
+ "spec/content_for/multiple_yields.haml",
80
+ "spec/content_for/multiple_yields.slim",
81
+ "spec/content_for/passes_values.erb",
82
+ "spec/content_for/passes_values.erubis",
83
+ "spec/content_for/passes_values.haml",
84
+ "spec/content_for/passes_values.slim",
85
+ "spec/content_for/same_key.erb",
86
+ "spec/content_for/same_key.erubis",
87
+ "spec/content_for/same_key.haml",
88
+ "spec/content_for/same_key.slim",
89
+ "spec/content_for/takes_values.erb",
90
+ "spec/content_for/takes_values.erubis",
91
+ "spec/content_for/takes_values.haml",
92
+ "spec/content_for/takes_values.slim",
93
+ "spec/content_for_spec.rb",
94
+ "spec/cookies_spec.rb",
95
+ "spec/decompile_spec.rb",
96
+ "spec/extension_spec.rb",
97
+ "spec/json_spec.rb",
98
+ "spec/link_header_spec.rb",
99
+ "spec/multi_route_spec.rb",
100
+ "spec/namespace/foo.erb",
101
+ "spec/namespace/nested/foo.erb",
102
+ "spec/namespace_spec.rb",
103
+ "spec/okjson.rb",
104
+ "spec/reloader/app.rb.erb",
105
+ "spec/reloader_spec.rb",
106
+ "spec/respond_with/bar.erb",
107
+ "spec/respond_with/bar.json.erb",
108
+ "spec/respond_with/foo.html.erb",
109
+ "spec/respond_with/not_html.sass",
110
+ "spec/respond_with_spec.rb",
111
+ "spec/spec_helper.rb",
112
+ "spec/streaming_spec.rb"
113
+ ]
114
+
115
+ s.add_dependency "sinatra", "~> 1.4.0"
116
+ s.add_dependency "backports", ">= 2.0"
117
+ s.add_dependency "tilt", "~> 1.3"
118
+ s.add_dependency "rack-test"
119
+ s.add_dependency "rack-protection"
120
+ s.add_dependency "eventmachine"
121
+
122
+ s.add_development_dependency "rspec", "~> 2.3"
123
+ s.add_development_dependency "haml"
124
+ s.add_development_dependency "erubis"
125
+ s.add_development_dependency "slim"
126
+ s.add_development_dependency "rake"
127
+ end
@@ -0,0 +1,93 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'backports'
3
+ require 'slim'
4
+ require_relative 'spec_helper'
5
+
6
+ describe Sinatra::Capture do
7
+ subject do
8
+ Sinatra.new do
9
+ enable :inline_templates
10
+ helpers Sinatra::Capture
11
+ end.new!
12
+ end
13
+ Tilt.prefer Tilt::ERBTemplate
14
+
15
+ extend Forwardable
16
+ def_delegators :subject, :capture, :capture_later
17
+
18
+ def render(engine, template)
19
+ subject.send(:render, engine, template.to_sym).strip.gsub(/\s+/, ' ')
20
+ end
21
+
22
+ shared_examples_for "a template language" do |engine|
23
+ lang = engine == :erubis ? :erb : engine
24
+ require "#{engine}"
25
+
26
+ it "captures content" do
27
+ render(engine, "simple_#{lang}").should == "Say Hello World!"
28
+ end
29
+
30
+ it "allows nested captures" do
31
+ render(engine, "nested_#{lang}").should == "Say Hello World!"
32
+ end
33
+ end
34
+
35
+ describe('haml') { it_behaves_like "a template language", :haml }
36
+ describe('slim') { it_behaves_like "a template language", :slim }
37
+ describe('erubis') { it_behaves_like "a template language", :erubis }
38
+
39
+ describe 'erb' do
40
+ it_behaves_like "a template language", :erb
41
+
42
+ it "handles utf-8 encoding" do
43
+ render(:erb, "utf_8").should == "UTF-8 –"
44
+ end
45
+ end
46
+ end
47
+
48
+ __END__
49
+
50
+ @@ simple_erb
51
+ Say
52
+ <% a = capture do %>World<% end %>
53
+ Hello <%= a %>!
54
+
55
+ @@ nested_erb
56
+ Say
57
+ <% a = capture do %>
58
+ <% b = capture do %>World<% end %>
59
+ <%= b %>!
60
+ <% end %>
61
+ Hello <%= a.strip %>
62
+
63
+ @@ simple_slim
64
+ | Say
65
+ - a = capture do
66
+ | World
67
+ | Hello #{a.strip}!
68
+
69
+ @@ nested_slim
70
+ | Say
71
+ - a = capture do
72
+ - b = capture do
73
+ | World
74
+ | #{b.strip}!
75
+ | Hello #{a.strip}
76
+
77
+ @@ simple_haml
78
+ Say
79
+ - a = capture do
80
+ World
81
+ Hello #{a.strip}!
82
+
83
+ @@ nested_haml
84
+ Say
85
+ - a = capture do
86
+ - b = capture do
87
+ World
88
+ #{b.strip}!
89
+ Hello #{a.strip}
90
+
91
+ @@ utf_8
92
+ <% a = capture do %>–<% end %>
93
+ UTF-8 <%= a %>
@@ -0,0 +1,6 @@
1
+ ---
2
+ foo: bar
3
+ something: 42
4
+ nested:
5
+ a: 1
6
+ b: 2
@@ -0,0 +1,6 @@
1
+ ---
2
+ foo: <%= "bar" %>
3
+ something: <%= 42 %>
4
+ nested:
5
+ a: <%= 1 %>
6
+ b: <%= 2 %>
@@ -0,0 +1,2 @@
1
+ ---
2
+ foo: foo
@@ -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,63 @@
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 render options in ERB tags' do
27
+ config_file 'key_value.yml.erb'
28
+ settings.foo.should == "bar"
29
+ settings.something.should == 42
30
+ settings.nested['a'].should == 1
31
+ settings.nested[:a].should == 1
32
+ settings.nested['b'].should == 2
33
+ settings.nested[:b].should == 2
34
+ end
35
+
36
+ it 'should recognize env specific settings per file' do
37
+ config_file 'with_envs.yml'
38
+ settings.foo.should == 'test'
39
+ end
40
+
41
+ it 'should recognize env specific settings per setting' do
42
+ config_file 'with_nested_envs.yml'
43
+ settings.database[:adapter].should == 'sqlite'
44
+ end
45
+
46
+ it 'should not set present values to nil if the current env is missing' do
47
+ # first let's check the test is actually working properly
48
+ config_file('missing_env.yml') { set :foo => 42, :environment => :production }
49
+ settings.foo.should == 10
50
+ # now test it
51
+ config_file('missing_env.yml') { set :foo => 42, :environment => :test }
52
+ settings.foo.should == 42
53
+ end
54
+
55
+ it 'should prioritize settings in latter files' do
56
+ # first let's check the test is actually working properly
57
+ config_file 'key_value.yml'
58
+ settings.foo.should == 'bar'
59
+ # now test it
60
+ config_file 'key_value_override.yml'
61
+ settings.foo.should == 'foo'
62
+ end
63
+ 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 %>